c4ccad499898dc6b56d37adf0368c88693df57c3
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/>.
18 from tempfile
import NamedTemporaryFile
21 from mediagoblin
import mg_globals
as mgg
22 from mediagoblin
.processing
import (create_pub_filepath
, BadMediaFail
,
23 FilenameBuilder
, ProgressCallback
)
25 from mediagoblin
.media_types
.audio
.transcoders
import (AudioTranscoder
,
28 _log
= logging
.getLogger(__name__
)
31 def sniff_handler(media_file
, **kw
):
33 transcoder
= AudioTranscoder()
34 data
= transcoder
.discover(media_file
.name
)
36 _log
.debug('Audio discovery raised BadMediaFail')
39 if data
.is_audio
== True and data
.is_video
== False:
45 def process_audio(entry
):
46 audio_config
= mgg
.global_config
['media_type:mediagoblin.media_types.audio']
48 workbench
= mgg
.workbench_manager
.create_workbench()
50 queued_filepath
= entry
.queued_media_file
51 queued_filename
= workbench
.localized_file(
52 mgg
.queue_store
, queued_filepath
,
54 name_builder
= FilenameBuilder(queued_filename
)
56 webm_audio_filepath
= create_pub_filepath(
58 '{original}.webm'.format(
59 original
=os
.path
.splitext(
60 queued_filepath
[-1])[0]))
62 if audio_config
['keep_original']:
63 with
open(queued_filename
, 'rb') as queued_file
:
64 original_filepath
= create_pub_filepath(
65 entry
, name_builder
.fill('{basename}{ext}'))
67 with mgg
.public_store
.get_file(original_filepath
, 'wb') as \
69 _log
.debug('Saving original...')
70 original_file
.write(queued_file
.read())
72 entry
.media_files
['original'] = original_filepath
74 transcoder
= AudioTranscoder()
76 with
NamedTemporaryFile(dir=workbench
.dir) as webm_audio_tmp
:
77 progress_callback
= ProgressCallback(entry
)
82 quality
=audio_config
['quality'],
83 progress_callback
=progress_callback
)
85 transcoder
.discover(webm_audio_tmp
.name
)
87 _log
.debug('Saving medium...')
88 mgg
.public_store
.get_file(webm_audio_filepath
, 'wb').write(
89 webm_audio_tmp
.read())
91 entry
.media_files
['webm_audio'] = webm_audio_filepath
93 # entry.media_data_init(length=int(data.audiolength))
95 if audio_config
['create_spectrogram']:
96 spectrogram_filepath
= create_pub_filepath(
98 '{original}-spectrogram.jpg'.format(
99 original
=os
.path
.splitext(
100 queued_filepath
[-1])[0]))
102 with
NamedTemporaryFile(dir=workbench
.dir, suffix
='.ogg') as wav_tmp
:
103 _log
.info('Creating OGG source for spectrogram')
104 transcoder
.transcode(
107 mux_string
='vorbisenc quality={0} ! oggmux'.format(
108 audio_config
['quality']))
110 thumbnailer
= AudioThumbnailer()
112 with
NamedTemporaryFile(dir=workbench
.dir, suffix
='.jpg') as spectrogram_tmp
:
113 thumbnailer
.spectrogram(
115 spectrogram_tmp
.name
,
116 width
=mgg
.global_config
['media:medium']['max_width'],
117 fft_size
=audio_config
['spectrogram_fft_size'])
119 _log
.debug('Saving spectrogram...')
120 mgg
.public_store
.get_file(spectrogram_filepath
, 'wb').write(
121 spectrogram_tmp
.read())
123 entry
.media_files
['spectrogram'] = spectrogram_filepath
125 with
NamedTemporaryFile(dir=workbench
.dir, suffix
='.jpg') as thumb_tmp
:
126 thumbnailer
.thumbnail_spectrogram(
127 spectrogram_tmp
.name
,
129 (mgg
.global_config
['media:thumb']['max_width'],
130 mgg
.global_config
['media:thumb']['max_height']))
132 thumb_filepath
= create_pub_filepath(
134 '{original}-thumbnail.jpg'.format(
135 original
=os
.path
.splitext(
136 queued_filepath
[-1])[0]))
138 mgg
.public_store
.get_file(thumb_filepath
, 'wb').write(
141 entry
.media_files
['thumb'] = thumb_filepath
143 entry
.media_files
['thumb'] = ['fake', 'thumb', 'path.jpg']
145 mgg
.queue_store
.delete_file(queued_filepath
)
148 workbench
.destroy_self()