ef98e533fed38cfbcfb1b293d30eba75e4ace66f
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
,
25 from mediagoblin
.media_types
.audio
.transcoders
import (AudioTranscoder
,
28 _log
= logging
.getLogger(__name__
)
30 def sniff_handler(media_file
, **kw
):
32 transcoder
= AudioTranscoder()
33 data
= transcoder
.discover(media_file
.name
)
35 _log
.debug('Audio discovery raised BadMediaFail')
38 if data
.is_audio
== True and data
.is_video
== False:
43 def process_audio(entry
):
44 audio_config
= mgg
.global_config
['media_type:mediagoblin.media_types.audio']
46 workbench
= mgg
.workbench_manager
.create_workbench()
48 queued_filepath
= entry
.queued_media_file
49 queued_filename
= workbench
.localized_file(
50 mgg
.queue_store
, queued_filepath
,
52 name_builder
= FilenameBuilder(queued_filename
)
54 webm_audio_filepath
= create_pub_filepath(
56 '{original}.webm'.format(
57 original
=os
.path
.splitext(
58 queued_filepath
[-1])[0]))
60 if audio_config
['keep_original']:
61 with
open(queued_filename
, 'rb') as queued_file
:
62 original_filepath
= create_pub_filepath(
63 entry
, name_builder
.fill('{basename}{ext}'))
65 with mgg
.public_store
.get_file(original_filepath
, 'wb') as \
67 _log
.debug('Saving original...')
68 original_file
.write(queued_file
.read())
70 entry
.media_files
['original'] = original_filepath
72 transcoder
= AudioTranscoder()
74 with tempfile
.NamedTemporaryFile() as webm_audio_tmp
:
79 quality
=audio_config
['quality'])
81 data
= transcoder
.discover(webm_audio_tmp
.name
)
83 _log
.debug('Saving medium...')
84 mgg
.public_store
.get_file(webm_audio_filepath
, 'wb').write(
85 webm_audio_tmp
.read())
87 entry
.media_files
['webm_audio'] = webm_audio_filepath
89 # entry.media_data_init(length=int(data.audiolength))
91 if audio_config
['create_spectrogram']:
92 spectrogram_filepath
= create_pub_filepath(
94 '{original}-spectrogram.jpg'.format(
95 original
=os
.path
.splitext(
96 queued_filepath
[-1])[0]))
98 with tempfile
.NamedTemporaryFile(suffix
='.wav') as wav_tmp
:
99 _log
.info('Creating WAV source for spectrogram')
100 transcoder
.transcode(
105 thumbnailer
= AudioThumbnailer()
107 with tempfile
.NamedTemporaryFile(suffix
='.jpg') as spectrogram_tmp
:
108 thumbnailer
.spectrogram(
110 spectrogram_tmp
.name
,
111 width
=mgg
.global_config
['media:medium']['max_width'],
112 fft_size
=audio_config
['spectrogram_fft_size'])
114 _log
.debug('Saving spectrogram...')
115 mgg
.public_store
.get_file(spectrogram_filepath
, 'wb').write(
116 spectrogram_tmp
.read())
118 entry
.media_files
['spectrogram'] = spectrogram_filepath
120 with tempfile
.NamedTemporaryFile(suffix
='.jpg') as thumb_tmp
:
121 thumbnailer
.thumbnail_spectrogram(
122 spectrogram_tmp
.name
,
124 (mgg
.global_config
['media:thumb']['max_width'],
125 mgg
.global_config
['media:thumb']['max_height']))
127 thumb_filepath
= create_pub_filepath(
129 '{original}-thumbnail.jpg'.format(
130 original
=os
.path
.splitext(
131 queued_filepath
[-1])[0]))
133 mgg
.public_store
.get_file(thumb_filepath
, 'wb').write(
136 entry
.media_files
['thumb'] = thumb_filepath
138 entry
.media_files
['thumb'] = ['fake', 'thumb', 'path.jpg']
140 mgg
.queue_store
.delete_file(queued_filepath
)
145 workbench
.destroy_self()