Commit | Line | Data |
---|---|---|
5a34a80d JW |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. | |
3 | # | |
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. | |
8 | # | |
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. | |
13 | # | |
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/>. | |
16 | ||
5ac1fe80 | 17 | import argparse |
5a34a80d | 18 | import logging |
5a34a80d JW |
19 | import os |
20 | ||
21 | from mediagoblin import mg_globals as mgg | |
5ac1fe80 | 22 | from mediagoblin.processing import ( |
440e33aa | 23 | BadMediaFail, FilenameBuilder, |
5ac1fe80 | 24 | ProgressCallback, MediaProcessor, ProcessingManager, |
1cefccc7 | 25 | request_from_args, get_process_filename, |
5ac1fe80 | 26 | store_public, copy_original) |
5a34a80d | 27 | |
5ac1fe80 | 28 | from mediagoblin.media_types.audio.transcoders import ( |
440e33aa | 29 | AudioTranscoder, AudioThumbnailer) |
5a34a80d | 30 | |
10085b77 | 31 | _log = logging.getLogger(__name__) |
5a34a80d | 32 | |
df68438a RE |
33 | MEDIA_TYPE = 'mediagoblin.media_types.audio' |
34 | ||
64712915 | 35 | |
ec4261a4 | 36 | def sniff_handler(media_file, **kw): |
df68438a | 37 | _log.info('Sniffing {0}'.format(MEDIA_TYPE)) |
196a5181 | 38 | try: |
4f4f2531 | 39 | transcoder = AudioTranscoder() |
ec4261a4 | 40 | data = transcoder.discover(media_file.name) |
4f4f2531 JW |
41 | except BadMediaFail: |
42 | _log.debug('Audio discovery raised BadMediaFail') | |
df68438a | 43 | return None |
ec4261a4 | 44 | |
440e33aa | 45 | if data.is_audio is True and data.is_video is False: |
df68438a | 46 | return MEDIA_TYPE |
10085b77 | 47 | |
df68438a | 48 | return None |
5a34a80d | 49 | |
64712915 | 50 | |
5ac1fe80 RE |
51 | class CommonAudioProcessor(MediaProcessor): |
52 | """ | |
53 | Provides a base for various audio processing steps | |
54 | """ | |
1cefccc7 | 55 | acceptable_files = ['original', 'webm_audio'] |
5ac1fe80 RE |
56 | |
57 | def common_setup(self): | |
58 | """ | |
440e33aa RE |
59 | Setup the workbench directory and pull down the original file, add |
60 | the audio_config, transcoder, thumbnailer and spectrogram_tmp path | |
5ac1fe80 RE |
61 | """ |
62 | self.audio_config = mgg \ | |
63 | .global_config['media_type:mediagoblin.media_types.audio'] | |
64 | ||
1cefccc7 RE |
65 | # Pull down and set up the processing file |
66 | self.process_filename = get_process_filename( | |
67 | self.entry, self.workbench, self.acceptable_files) | |
68 | self.name_builder = FilenameBuilder(self.process_filename) | |
5ac1fe80 | 69 | |
5ac1fe80 RE |
70 | self.transcoder = AudioTranscoder() |
71 | self.thumbnailer = AudioThumbnailer() | |
72 | ||
73 | def copy_original(self): | |
74 | if self.audio_config['keep_original']: | |
75 | copy_original( | |
1cefccc7 | 76 | self.entry, self.process_filename, |
5ac1fe80 RE |
77 | self.name_builder.fill('{basename}{ext}')) |
78 | ||
79 | def transcode(self, quality=None): | |
80 | if not quality: | |
81 | quality = self.audio_config['quality'] | |
82 | ||
83 | progress_callback = ProgressCallback(self.entry) | |
84 | webm_audio_tmp = os.path.join(self.workbench.dir, | |
85 | self.name_builder.fill( | |
86 | '{basename}{ext}')) | |
87 | ||
5ac1fe80 | 88 | self.transcoder.transcode( |
1cefccc7 | 89 | self.process_filename, |
5ac1fe80 RE |
90 | webm_audio_tmp, |
91 | quality=quality, | |
92 | progress_callback=progress_callback) | |
93 | ||
94 | self.transcoder.discover(webm_audio_tmp) | |
95 | ||
96 | _log.debug('Saving medium...') | |
9448a98e RE |
97 | store_public(self.entry, 'webm_audio', webm_audio_tmp, |
98 | self.name_builder.fill('{basename}.medium.webm')) | |
5ac1fe80 | 99 | |
ad80fc8a | 100 | def create_spectrogram(self, max_width=None, fft_size=None): |
5ac1fe80 RE |
101 | if not max_width: |
102 | max_width = mgg.global_config['media:medium']['max_width'] | |
103 | if not fft_size: | |
104 | fft_size = self.audio_config['spectrogram_fft_size'] | |
105 | ||
5ac1fe80 RE |
106 | wav_tmp = os.path.join(self.workbench.dir, self.name_builder.fill( |
107 | '{basename}.ogg')) | |
108 | ||
109 | _log.info('Creating OGG source for spectrogram') | |
110 | self.transcoder.transcode( | |
1cefccc7 | 111 | self.process_filename, |
5ac1fe80 | 112 | wav_tmp, |
ad80fc8a RE |
113 | mux_string='vorbisenc quality={0} ! oggmux'.format( |
114 | self.audio_config['quality'])) | |
5ac1fe80 | 115 | |
d8f886dc | 116 | spectrogram_tmp = os.path.join(self.workbench.dir, |
347ef583 RE |
117 | self.name_builder.fill( |
118 | '{basename}-spectrogram.jpg')) | |
d8f886dc | 119 | |
5ac1fe80 RE |
120 | self.thumbnailer.spectrogram( |
121 | wav_tmp, | |
d8f886dc | 122 | spectrogram_tmp, |
5ac1fe80 RE |
123 | width=max_width, |
124 | fft_size=fft_size) | |
125 | ||
126 | _log.debug('Saving spectrogram...') | |
d8f886dc | 127 | store_public(self.entry, 'spectrogram', spectrogram_tmp, |
c6eaa555 | 128 | self.name_builder.fill('{basename}.spectrogram.jpg')) |
5ac1fe80 RE |
129 | |
130 | def generate_thumb(self, size=None): | |
131 | if not size: | |
776e4d7a RE |
132 | max_width = mgg.global_config['media:thumb']['max_width'] |
133 | max_height = mgg.global_config['media:thumb']['max_height'] | |
5ac1fe80 RE |
134 | size = (max_width, max_height) |
135 | ||
136 | thumb_tmp = os.path.join(self.workbench.dir, self.name_builder.fill( | |
137 | '{basename}-thumbnail.jpg')) | |
138 | ||
d8f886dc RE |
139 | # We need the spectrogram to create a thumbnail |
140 | spectrogram = self.entry.media_files.get('spectrogram') | |
141 | if not spectrogram: | |
142 | _log.info('No spectrogram found, we will create one.') | |
143 | self.create_spectrogram() | |
144 | spectrogram = self.entry.media_files['spectrogram'] | |
145 | ||
146 | spectrogram_filepath = mgg.public_store.get_local_path(spectrogram) | |
147 | ||
5ac1fe80 | 148 | self.thumbnailer.thumbnail_spectrogram( |
d8f886dc | 149 | spectrogram_filepath, |
5ac1fe80 | 150 | thumb_tmp, |
b95cc59b | 151 | tuple(size)) |
5ac1fe80 | 152 | |
c6eaa555 RE |
153 | store_public(self.entry, 'thumb', thumb_tmp, |
154 | self.name_builder.fill('{basename}.thumbnail.jpg')) | |
5ac1fe80 RE |
155 | |
156 | ||
157 | class InitialProcessor(CommonAudioProcessor): | |
158 | """ | |
159 | Initial processing steps for new audio | |
160 | """ | |
161 | name = "initial" | |
162 | description = "Initial processing" | |
163 | ||
164 | @classmethod | |
165 | def media_is_eligible(cls, entry=None, state=None): | |
166 | """ | |
167 | Determine if this media type is eligible for processing | |
168 | """ | |
169 | if not state: | |
170 | state = entry.state | |
171 | return state in ( | |
172 | "unprocessed", "failed") | |
173 | ||
174 | @classmethod | |
175 | def generate_parser(cls): | |
176 | parser = argparse.ArgumentParser( | |
177 | description=cls.description, | |
178 | prog=cls.name) | |
179 | ||
180 | parser.add_argument( | |
181 | '--quality', | |
52e97704 | 182 | type=float, |
d8f886dc | 183 | help='vorbisenc quality. Range: -0.1..1') |
5ac1fe80 RE |
184 | |
185 | parser.add_argument( | |
186 | '--fft_size', | |
187 | type=int, | |
188 | help='spectrogram fft size') | |
189 | ||
190 | parser.add_argument( | |
191 | '--thumb_size', | |
757376e3 | 192 | nargs=2, |
5ac1fe80 | 193 | metavar=('max_width', 'max_height'), |
7674b9c0 RE |
194 | type=int, |
195 | help='minimum size is 100 x 100') | |
5ac1fe80 RE |
196 | |
197 | parser.add_argument( | |
198 | '--medium_width', | |
199 | type=int, | |
200 | help='The width of the spectogram') | |
201 | ||
202 | parser.add_argument( | |
203 | '--create_spectrogram', | |
204 | action='store_true', | |
b95cc59b | 205 | help='Create spectogram and thumbnail, will default to config') |
5ac1fe80 RE |
206 | |
207 | return parser | |
208 | ||
209 | @classmethod | |
210 | def args_to_request(cls, args): | |
211 | return request_from_args( | |
212 | args, ['create_spectrogram', 'quality', 'fft_size', | |
213 | 'thumb_size', 'medium_width']) | |
214 | ||
215 | def process(self, quality=None, fft_size=None, thumb_size=None, | |
216 | create_spectrogram=None, medium_width=None): | |
550af89f RE |
217 | self.common_setup() |
218 | ||
5ac1fe80 RE |
219 | if not create_spectrogram: |
220 | create_spectrogram = self.audio_config['create_spectrogram'] | |
221 | ||
5ac1fe80 RE |
222 | self.transcode(quality=quality) |
223 | self.copy_original() | |
224 | ||
225 | if create_spectrogram: | |
ad80fc8a | 226 | self.create_spectrogram(max_width=medium_width, fft_size=fft_size) |
5ac1fe80 RE |
227 | self.generate_thumb(size=thumb_size) |
228 | self.delete_queue_file() | |
229 | ||
230 | ||
2e50e4b5 RE |
231 | class Resizer(CommonAudioProcessor): |
232 | """ | |
233 | Thumbnail and spectogram resizing process steps for processed audio | |
234 | """ | |
235 | name = 'resize' | |
ad80fc8a | 236 | description = 'Resize thumbnail or spectogram' |
3225008f | 237 | thumb_size = 'thumb_size' |
2e50e4b5 RE |
238 | |
239 | @classmethod | |
240 | def media_is_eligible(cls, entry=None, state=None): | |
241 | """ | |
242 | Determine if this media entry is eligible for processing | |
243 | """ | |
244 | if not state: | |
245 | state = entry.state | |
246 | return state in 'processed' | |
247 | ||
248 | @classmethod | |
249 | def generate_parser(cls): | |
250 | parser = argparse.ArgumentParser( | |
251 | description=cls.description, | |
252 | prog=cls.name) | |
253 | ||
2e50e4b5 RE |
254 | parser.add_argument( |
255 | '--fft_size', | |
256 | type=int, | |
257 | help='spectrogram fft size') | |
258 | ||
259 | parser.add_argument( | |
260 | '--thumb_size', | |
261 | nargs=2, | |
262 | metavar=('max_width', 'max_height'), | |
7674b9c0 RE |
263 | type=int, |
264 | help='minimum size is 100 x 100') | |
2e50e4b5 RE |
265 | |
266 | parser.add_argument( | |
267 | '--medium_width', | |
268 | type=int, | |
269 | help='The width of the spectogram') | |
270 | ||
271 | parser.add_argument( | |
272 | 'file', | |
273 | choices=['thumb', 'spectrogram']) | |
274 | ||
275 | return parser | |
276 | ||
277 | @classmethod | |
278 | def args_to_request(cls, args): | |
279 | return request_from_args( | |
ad80fc8a | 280 | args, ['thumb_size', 'file', 'fft_size', 'medium_width']) |
2e50e4b5 | 281 | |
0c509b1b | 282 | def process(self, file, thumb_size=None, fft_size=None, |
2e50e4b5 RE |
283 | medium_width=None): |
284 | self.common_setup() | |
ad80fc8a | 285 | |
2e50e4b5 RE |
286 | if file == 'thumb': |
287 | self.generate_thumb(size=thumb_size) | |
288 | elif file == 'spectrogram': | |
ad80fc8a | 289 | self.create_spectrogram(max_width=medium_width, fft_size=fft_size) |
2e50e4b5 RE |
290 | |
291 | ||
0c509b1b RE |
292 | class Transcoder(CommonAudioProcessor): |
293 | """ | |
294 | Transcoding processing steps for processed audio | |
295 | """ | |
296 | name = 'transcode' | |
297 | description = 'Re-transcode audio' | |
298 | ||
299 | @classmethod | |
300 | def media_is_eligible(cls, entry=None, state=None): | |
301 | if not state: | |
302 | state = entry.state | |
303 | return state in 'processed' | |
304 | ||
305 | @classmethod | |
306 | def generate_parser(cls): | |
307 | parser = argparse.ArgumentParser( | |
308 | description=cls.description, | |
309 | prog=cls.name) | |
310 | ||
311 | parser.add_argument( | |
312 | '--quality', | |
313 | help='vorbisenc quality. Range: -0.1..1') | |
314 | ||
315 | return parser | |
316 | ||
317 | @classmethod | |
318 | def args_to_request(cls, args): | |
319 | return request_from_args( | |
320 | args, ['quality']) | |
321 | ||
322 | def process(self, quality=None): | |
323 | self.common_setup() | |
324 | self.transcode(quality=quality) | |
325 | ||
326 | ||
5ac1fe80 RE |
327 | class AudioProcessingManager(ProcessingManager): |
328 | def __init__(self): | |
329 | super(self.__class__, self).__init__() | |
330 | self.add_processor(InitialProcessor) | |
2e50e4b5 | 331 | self.add_processor(Resizer) |
0c509b1b | 332 | self.add_processor(Transcoder) |