1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 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/>.
21 from mediagoblin
import mg_globals
as mgg
22 from mediagoblin
.processing
import create_pub_filepath
24 from mediagoblin
.media_types
.audio
.transcoders
import AudioTranscoder
, \
27 _log
= logging
.getLogger(__name__
)
29 def sniff_handler(media_file
, **kw
):
30 transcoder
= AudioTranscoder()
32 data
= transcoder
.discover(media_file
.name
)
34 if data
.is_audio
== True and data
.is_video
== False:
41 def process_audio(entry
):
42 audio_config
= mgg
.global_config
['media_type:mediagoblin.media_types.audio']
44 workbench
= mgg
.workbench_manager
.create_workbench()
46 queued_filepath
= entry
.queued_media_file
47 queued_filename
= workbench
.localized_file(
48 mgg
.queue_store
, queued_filepath
,
51 ogg_filepath
= create_pub_filepath(
53 '{original}.webm'.format(
54 original
=os
.path
.splitext(
55 queued_filepath
[-1])[0]))
57 transcoder
= AudioTranscoder()
59 with tempfile
.NamedTemporaryFile() as ogg_tmp
:
64 quality
=audio_config
['quality'])
66 data
= transcoder
.discover(ogg_tmp
.name
)
68 _log
.debug('Saving medium...')
69 mgg
.public_store
.get_file(ogg_filepath
, 'wb').write(
72 entry
.media_files
['ogg'] = ogg_filepath
74 entry
.media_data
['audio'] = {
75 u
'length': int(data
.audiolength
)}
77 if audio_config
['create_spectrogram']:
78 spectrogram_filepath
= create_pub_filepath(
80 '{original}-spectrogram.jpg'.format(
81 original
=os
.path
.splitext(
82 queued_filepath
[-1])[0]))
84 with tempfile
.NamedTemporaryFile(suffix
='.wav') as wav_tmp
:
85 _log
.info('Creating WAV source for spectrogram')
91 thumbnailer
= AudioThumbnailer()
93 with tempfile
.NamedTemporaryFile(suffix
='.jpg') as spectrogram_tmp
:
94 thumbnailer
.spectrogram(
97 width
=mgg
.global_config
['media:medium']['max_width'])
99 _log
.debug('Saving spectrogram...')
100 mgg
.public_store
.get_file(spectrogram_filepath
, 'wb').write(
101 spectrogram_tmp
.read())
103 entry
.media_files
['spectrogram'] = spectrogram_filepath
105 with tempfile
.NamedTemporaryFile(suffix
='.jpg') as thumb_tmp
:
106 thumbnailer
.thumbnail_spectrogram(
107 spectrogram_tmp
.name
,
109 (mgg
.global_config
['media:thumb']['max_width'],
110 mgg
.global_config
['media:thumb']['max_height']))
112 thumb_filepath
= create_pub_filepath(
114 '{original}-thumbnail.jpg'.format(
115 original
=os
.path
.splitext(
116 queued_filepath
[-1])[0]))
118 mgg
.public_store
.get_file(thumb_filepath
, 'wb').write(
121 entry
.media_files
['thumb'] = thumb_filepath
123 entry
.media_files
['thumb'] = ['fake', 'thumb', 'path.jpg']
125 mgg
.queue_store
.delete_file(queued_filepath
)