Port of audio to GStreamer 1.0
[larjonas-mediagoblin.git] / mediagoblin / tests / test_audio.py
blob740d9cdd0af0bc41c7a2b45d479700e5bef6b674
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2013 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 import tempfile
18 import shutil
19 import os
20 import pytest
21 from contextlib import contextmanager
22 import logging
23 import imghdr
25 #os.environ['GST_DEBUG'] = '4,python:4'
27 #TODO: this should be skipped if video plugin is not enabled
28 import gi
29 gi.require_version('Gst', '1.0')
30 from gi.repository import Gst
31 Gst.init(None)
33 from mediagoblin.media_types.audio.transcoders import (AudioTranscoder,
34 AudioThumbnailer)
35 from mediagoblin.media_types.tools import discover
38 @contextmanager
39 def create_audio():
40 audio = tempfile.NamedTemporaryFile()
41 src = Gst.ElementFactory.make('audiotestsrc', None)
42 src.set_property('num-buffers', 50)
43 enc = Gst.ElementFactory.make('flacenc', None)
44 dst = Gst.ElementFactory.make('filesink', None)
45 dst.set_property('location', audio.name)
46 pipeline = Gst.Pipeline()
47 pipeline.add(src)
48 pipeline.add(enc)
49 pipeline.add(dst)
50 src.link(enc)
51 enc.link(dst)
52 pipeline.set_state(Gst.State.PLAYING)
53 state = pipeline.get_state(3 * Gst.SECOND)
54 assert state[0] == Gst.StateChangeReturn.SUCCESS
55 bus = pipeline.get_bus()
56 bus.timed_pop_filtered(
57 3 * Gst.SECOND,
58 Gst.MessageType.ERROR | Gst.MessageType.EOS)
59 pipeline.set_state(Gst.State.NULL)
60 yield (audio.name)
63 @contextmanager
64 def create_data_for_test():
65 with create_audio() as audio_name:
66 second_file = tempfile.NamedTemporaryFile()
67 yield (audio_name, second_file.name)
70 def test_transcoder():
71 '''
72 Tests AudioTransocder's transcode method
73 '''
74 transcoder = AudioTranscoder()
75 with create_data_for_test() as (audio_name, result_name):
76 transcoder.transcode(audio_name, result_name, quality=0.3,
77 progress_callback=None)
78 info = discover(result_name)
79 assert len(info.get_audio_streams()) == 1
80 transcoder.transcode(audio_name, result_name, quality=0.3,
81 mux_name='oggmux', progress_callback=None)
82 info = discover(result_name)
83 assert len(info.get_audio_streams()) == 1
86 def test_thumbnails():
87 '''Test thumbnails generation.
89 The code below heavily repeats
90 audio.processing.CommonAudioProcessor.create_spectrogram
91 1. Create test audio
92 2. Convert it to OGG source for spectogram using transcoder
93 3. Create spectogram in jpg
95 '''
96 thumbnailer = AudioThumbnailer()
97 transcoder = AudioTranscoder()
98 with create_data_for_test() as (audio_name, new_name):
99 transcoder.transcode(audio_name, new_name, mux_name='oggmux')
100 thumbnail = tempfile.NamedTemporaryFile(suffix='.jpg')
101 # fft_size below is copypasted from config_spec.ini
102 thumbnailer.spectrogram(new_name, thumbnail.name, width=100,
103 fft_size=4096)
104 assert imghdr.what(thumbnail.name) == 'jpeg'