62daf412309aa872d20028c472285368d5f1a3ae
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
, BadMediaFail
24 from mediagoblin
.media_types
.audio
.transcoders
import AudioTranscoder
, \
27 _log
= logging
.getLogger(__name__
)
29 def sniff_handler(media_file
, **kw
):
31 transcoder
= AudioTranscoder()
32 data
= transcoder
.discover(media_file
.name
)
34 _log
.debug('Audio discovery raised BadMediaFail')
37 if data
.is_audio
== True and data
.is_video
== False:
42 def process_audio(entry
):
43 audio_config
= mgg
.global_config
['media_type:mediagoblin.media_types.audio']
45 workbench
= mgg
.workbench_manager
.create_workbench()
47 queued_filepath
= entry
.queued_media_file
48 queued_filename
= workbench
.localized_file(
49 mgg
.queue_store
, queued_filepath
,
52 ogg_filepath
= create_pub_filepath(
54 '{original}.webm'.format(
55 original
=os
.path
.splitext(
56 queued_filepath
[-1])[0]))
58 transcoder
= AudioTranscoder()
60 with tempfile
.NamedTemporaryFile() as ogg_tmp
:
65 quality
=audio_config
['quality'])
67 data
= transcoder
.discover(ogg_tmp
.name
)
69 _log
.debug('Saving medium...')
70 mgg
.public_store
.get_file(ogg_filepath
, 'wb').write(
73 entry
.media_files
['ogg'] = ogg_filepath
75 entry
.media_data
['audio'] = {
76 u
'length': int(data
.audiolength
)}
78 if audio_config
['create_spectrogram']:
79 spectrogram_filepath
= create_pub_filepath(
81 '{original}-spectrogram.jpg'.format(
82 original
=os
.path
.splitext(
83 queued_filepath
[-1])[0]))
85 with tempfile
.NamedTemporaryFile(suffix
='.wav') as wav_tmp
:
86 _log
.info('Creating WAV source for spectrogram')
92 thumbnailer
= AudioThumbnailer()
94 with tempfile
.NamedTemporaryFile(suffix
='.jpg') as spectrogram_tmp
:
95 thumbnailer
.spectrogram(
98 width
=mgg
.global_config
['media:medium']['max_width'])
100 _log
.debug('Saving spectrogram...')
101 mgg
.public_store
.get_file(spectrogram_filepath
, 'wb').write(
102 spectrogram_tmp
.read())
104 entry
.media_files
['spectrogram'] = spectrogram_filepath
106 with tempfile
.NamedTemporaryFile(suffix
='.jpg') as thumb_tmp
:
107 thumbnailer
.thumbnail_spectrogram(
108 spectrogram_tmp
.name
,
110 (mgg
.global_config
['media:thumb']['max_width'],
111 mgg
.global_config
['media:thumb']['max_height']))
113 thumb_filepath
= create_pub_filepath(
115 '{original}-thumbnail.jpg'.format(
116 original
=os
.path
.splitext(
117 queued_filepath
[-1])[0]))
119 mgg
.public_store
.get_file(thumb_filepath
, 'wb').write(
122 entry
.media_files
['thumb'] = thumb_filepath
124 entry
.media_files
['thumb'] = ['fake', 'thumb', 'path.jpg']
126 mgg
.queue_store
.delete_file(queued_filepath
)
131 workbench
.destroy_self()