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 RE |
24 | ProgressCallback, MediaProcessor, ProcessingManager, |
25 | request_from_args, get_orig_filename, | |
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 | """ | |
55 | ||
56 | def common_setup(self): | |
57 | """ | |
440e33aa RE |
58 | Setup the workbench directory and pull down the original file, add |
59 | the audio_config, transcoder, thumbnailer and spectrogram_tmp path | |
5ac1fe80 RE |
60 | """ |
61 | self.audio_config = mgg \ | |
62 | .global_config['media_type:mediagoblin.media_types.audio'] | |
63 | ||
64 | # Pull down and set up the original file | |
65 | self.orig_filename = get_orig_filename( | |
66 | self.entry, self.workbench) | |
67 | self.name_builder = FilenameBuilder(self.orig_filename) | |
68 | ||
5ac1fe80 RE |
69 | self.transcoder = AudioTranscoder() |
70 | self.thumbnailer = AudioThumbnailer() | |
71 | ||
72 | def copy_original(self): | |
73 | if self.audio_config['keep_original']: | |
74 | copy_original( | |
75 | self.entry, self.orig_filename, | |
76 | self.name_builder.fill('{basename}{ext}')) | |
77 | ||
78 | def transcode(self, quality=None): | |
79 | if not quality: | |
80 | quality = self.audio_config['quality'] | |
81 | ||
82 | progress_callback = ProgressCallback(self.entry) | |
83 | webm_audio_tmp = os.path.join(self.workbench.dir, | |
84 | self.name_builder.fill( | |
85 | '{basename}{ext}')) | |
86 | ||
5ac1fe80 RE |
87 | self.transcoder.transcode( |
88 | self.orig_filename, | |
89 | webm_audio_tmp, | |
90 | quality=quality, | |
91 | progress_callback=progress_callback) | |
92 | ||
93 | self.transcoder.discover(webm_audio_tmp) | |
94 | ||
95 | _log.debug('Saving medium...') | |
9448a98e RE |
96 | store_public(self.entry, 'webm_audio', webm_audio_tmp, |
97 | self.name_builder.fill('{basename}.medium.webm')) | |
5ac1fe80 RE |
98 | |
99 | def create_spectrogram(self, quality=None, max_width=None, fft_size=None): | |
100 | if not quality: | |
101 | quality = self.audio_config['quality'] | |
102 | if not max_width: | |
103 | max_width = mgg.global_config['media:medium']['max_width'] | |
104 | if not fft_size: | |
105 | fft_size = self.audio_config['spectrogram_fft_size'] | |
106 | ||
5ac1fe80 RE |
107 | wav_tmp = os.path.join(self.workbench.dir, self.name_builder.fill( |
108 | '{basename}.ogg')) | |
109 | ||
110 | _log.info('Creating OGG source for spectrogram') | |
111 | self.transcoder.transcode( | |
112 | self.orig_filename, | |
113 | wav_tmp, | |
114 | mux_string='vorbisenc quality={0} ! oggmux'.format(quality)) | |
115 | ||
d8f886dc RE |
116 | spectrogram_tmp = os.path.join(self.workbench.dir, |
117 | self.name_builder.fill( | |
118 | '{basename}-spectrogram.jpg')) | |
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 RE |
150 | thumb_tmp, |
151 | size) | |
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', | |
d8f886dc | 182 | help='vorbisenc quality. Range: -0.1..1') |
5ac1fe80 RE |
183 | |
184 | parser.add_argument( | |
185 | '--fft_size', | |
186 | type=int, | |
187 | help='spectrogram fft size') | |
188 | ||
189 | parser.add_argument( | |
190 | '--thumb_size', | |
757376e3 | 191 | nargs=2, |
5ac1fe80 RE |
192 | metavar=('max_width', 'max_height'), |
193 | type=int) | |
194 | ||
195 | parser.add_argument( | |
196 | '--medium_width', | |
197 | type=int, | |
198 | help='The width of the spectogram') | |
199 | ||
200 | parser.add_argument( | |
201 | '--create_spectrogram', | |
202 | action='store_true', | |
203 | help='Create spectogram and thumbnail') | |
204 | ||
205 | return parser | |
206 | ||
207 | @classmethod | |
208 | def args_to_request(cls, args): | |
209 | return request_from_args( | |
210 | args, ['create_spectrogram', 'quality', 'fft_size', | |
211 | 'thumb_size', 'medium_width']) | |
212 | ||
213 | def process(self, quality=None, fft_size=None, thumb_size=None, | |
214 | create_spectrogram=None, medium_width=None): | |
550af89f RE |
215 | self.common_setup() |
216 | ||
5ac1fe80 RE |
217 | if not create_spectrogram: |
218 | create_spectrogram = self.audio_config['create_spectrogram'] | |
219 | ||
5ac1fe80 RE |
220 | self.transcode(quality=quality) |
221 | self.copy_original() | |
222 | ||
223 | if create_spectrogram: | |
224 | self.create_spectrogram(quality=quality, max_width=medium_width, | |
225 | fft_size=fft_size) | |
226 | self.generate_thumb(size=thumb_size) | |
227 | self.delete_queue_file() | |
228 | ||
229 | ||
2e50e4b5 RE |
230 | class Resizer(CommonAudioProcessor): |
231 | """ | |
232 | Thumbnail and spectogram resizing process steps for processed audio | |
233 | """ | |
234 | name = 'resize' | |
235 | description = 'Resize audio thumbnail or spectogram' | |
236 | ||
237 | @classmethod | |
238 | def media_is_eligible(cls, entry=None, state=None): | |
239 | """ | |
240 | Determine if this media entry is eligible for processing | |
241 | """ | |
242 | if not state: | |
243 | state = entry.state | |
244 | return state in 'processed' | |
245 | ||
246 | @classmethod | |
247 | def generate_parser(cls): | |
248 | parser = argparse.ArgumentParser( | |
249 | description=cls.description, | |
250 | prog=cls.name) | |
251 | ||
252 | parser.add_argument( | |
253 | '--quality', | |
254 | help='vorbisenc quality. Range: -0.1..1') | |
255 | ||
256 | parser.add_argument( | |
257 | '--fft_size', | |
258 | type=int, | |
259 | help='spectrogram fft size') | |
260 | ||
261 | parser.add_argument( | |
262 | '--thumb_size', | |
263 | nargs=2, | |
264 | metavar=('max_width', 'max_height'), | |
265 | type=int) | |
266 | ||
267 | parser.add_argument( | |
268 | '--medium_width', | |
269 | type=int, | |
270 | help='The width of the spectogram') | |
271 | ||
272 | parser.add_argument( | |
273 | 'file', | |
274 | choices=['thumb', 'spectrogram']) | |
275 | ||
276 | return parser | |
277 | ||
278 | @classmethod | |
279 | def args_to_request(cls, args): | |
280 | return request_from_args( | |
281 | args, ['thumb_size', 'file', 'quality', 'fft_size', 'medium_width']) | |
282 | ||
283 | def process(self, thumb_size=None, file=None, quality=None, fft_size=None, | |
284 | medium_width=None): | |
285 | self.common_setup() | |
286 | if file == 'thumb': | |
287 | self.generate_thumb(size=thumb_size) | |
288 | elif file == 'spectrogram': | |
289 | self.create_spectrogram(quality=quality, max_width=medium_width, | |
290 | fft_size=fft_size) | |
291 | ||
292 | ||
5ac1fe80 RE |
293 | class AudioProcessingManager(ProcessingManager): |
294 | def __init__(self): | |
295 | super(self.__class__, self).__init__() | |
296 | self.add_processor(InitialProcessor) | |
2e50e4b5 | 297 | self.add_processor(Resizer) |