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(proc_state
):
46 """Code to process uploaded audio. Will be run by celery.
48 A Workbench() represents a local tempory dir. It is automatically
49 cleaned up when this function exits.
51 entry
= proc_state
.entry
52 workbench
= proc_state
.workbench
53 audio_config
= mgg
.global_config
['media_type:mediagoblin.media_types.audio']
55 queued_filepath
= entry
.queued_media_file
56 queued_filename
= workbench
.localized_file(
57 mgg
.queue_store
, queued_filepath
,
59 name_builder
= FilenameBuilder(queued_filename
)
61 webm_audio_filepath
= create_pub_filepath(
63 '{original}.webm'.format(
64 original
=os
.path
.splitext(
65 queued_filepath
[-1])[0]))
67 if audio_config
['keep_original']:
68 with
open(queued_filename
, 'rb') as queued_file
:
69 original_filepath
= create_pub_filepath(
70 entry
, name_builder
.fill('{basename}{ext}'))
72 with mgg
.public_store
.get_file(original_filepath
, 'wb') as \
74 _log
.debug('Saving original...')
75 original_file
.write(queued_file
.read())
77 entry
.media_files
['original'] = original_filepath
79 transcoder
= AudioTranscoder()
81 with
NamedTemporaryFile(dir=workbench
.dir) as webm_audio_tmp
:
82 progress_callback
= ProgressCallback(entry
)
87 quality
=audio_config
['quality'],
88 progress_callback
=progress_callback
)
90 transcoder
.discover(webm_audio_tmp
.name
)
92 _log
.debug('Saving medium...')
93 mgg
.public_store
.get_file(webm_audio_filepath
, 'wb').write(
94 webm_audio_tmp
.read())
96 entry
.media_files
['webm_audio'] = webm_audio_filepath
98 # entry.media_data_init(length=int(data.audiolength))
100 if audio_config
['create_spectrogram']:
101 spectrogram_filepath
= create_pub_filepath(
103 '{original}-spectrogram.jpg'.format(
104 original
=os
.path
.splitext(
105 queued_filepath
[-1])[0]))
107 with
NamedTemporaryFile(dir=workbench
.dir, suffix
='.ogg') as wav_tmp
:
108 _log
.info('Creating OGG source for spectrogram')
109 transcoder
.transcode(
112 mux_string
='vorbisenc quality={0} ! oggmux'.format(
113 audio_config
['quality']))
115 thumbnailer
= AudioThumbnailer()
117 with
NamedTemporaryFile(dir=workbench
.dir, suffix
='.jpg') as spectrogram_tmp
:
118 thumbnailer
.spectrogram(
120 spectrogram_tmp
.name
,
121 width
=mgg
.global_config
['media:medium']['max_width'],
122 fft_size
=audio_config
['spectrogram_fft_size'])
124 _log
.debug('Saving spectrogram...')
125 mgg
.public_store
.get_file(spectrogram_filepath
, 'wb').write(
126 spectrogram_tmp
.read())
128 entry
.media_files
['spectrogram'] = spectrogram_filepath
130 with
NamedTemporaryFile(dir=workbench
.dir, suffix
='.jpg') as thumb_tmp
:
131 thumbnailer
.thumbnail_spectrogram(
132 spectrogram_tmp
.name
,
134 (mgg
.global_config
['media:thumb']['max_width'],
135 mgg
.global_config
['media:thumb']['max_height']))
137 thumb_filepath
= create_pub_filepath(
139 '{original}-thumbnail.jpg'.format(
140 original
=os
.path
.splitext(
141 queued_filepath
[-1])[0]))
143 mgg
.public_store
.get_file(thumb_filepath
, 'wb').write(
146 entry
.media_files
['thumb'] = thumb_filepath
148 entry
.media_files
['thumb'] = ['fake', 'thumb', 'path.jpg']
150 # Remove queued media file from storage and database.
151 # queued_filepath is in the task_id directory which should
152 # be removed too, but fail if the directory is not empty to be on
153 # the super-safe side.
154 mgg
.queue_store
.delete_file(queued_filepath
) # rm file
155 mgg
.queue_store
.delete_dir(queued_filepath
[:-1]) # rm dir
156 entry
.queued_media_file
= []