64cacb5f0a2ff879f1750c549c9f77d7fc2e23a3
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/>.
25 from celery
import group
, chord
26 from mediagoblin
import mg_globals
as mgg
27 from mediagoblin
.processing
import (
28 FilenameBuilder
, BaseProcessingFail
,
29 ProgressCallback
, MediaProcessor
,
30 ProcessingManager
, request_from_args
,
31 get_process_filename
, store_public
,
33 from mediagoblin
.processing
.task
import ProcessMedia
34 from mediagoblin
.tools
.translate
import lazy_pass_to_ugettext
as _
35 from mediagoblin
.media_types
import MissingComponents
37 from . import transcoders
38 from .util
import skip_transcode
, ACCEPTED_RESOLUTIONS
40 _log
= logging
.getLogger(__name__
)
41 _log
.setLevel(logging
.DEBUG
)
43 MEDIA_TYPE
= 'mediagoblin.media_types.video'
46 class VideoTranscodingFail(BaseProcessingFail
):
48 Error raised if video transcoding fails
50 general_message
= _(u
'Video transcoding failed')
53 def sniffer(media_file
):
54 '''New style sniffer, used in two-steps check; requires to have .name'''
55 _log
.info('Sniffing {0}'.format(MEDIA_TYPE
))
57 data
= transcoders
.discover(media_file
.name
)
58 except Exception as e
:
59 # this is usually GLib.GError, but we don't really care which one
60 _log
.warning(u
'GStreamer: {0}'.format(six
.text_type(e
)))
61 raise MissingComponents(u
'GStreamer: {0}'.format(six
.text_type(e
)))
62 _log
.debug('Discovered: {0}'.format(data
))
64 if not data
.get_video_streams():
65 raise MissingComponents('No video streams found in this video')
67 if data
.get_result() != 0: # it's 0 if success
69 missing
= data
.get_misc().get_string('name')
70 _log
.warning('GStreamer: missing {0}'.format(missing
))
71 except AttributeError as e
:
72 # AttributeError happens here on gstreamer >1.4, when get_misc
73 # returns None. There is a special function to get info about
74 # missing plugin. This info should be printed to logs for admin and
75 # showed to the user in a short and nice version
76 details
= data
.get_missing_elements_installer_details()
77 _log
.warning('GStreamer: missing: {0}'.format(', '.join(details
)))
78 missing
= u
', '.join([u
'{0} ({1})'.format(*d
.split('|')[3:])
80 raise MissingComponents(u
'{0} is missing'.format(missing
))
85 def sniff_handler(media_file
, filename
):
87 return sniffer(media_file
)
89 _log
.error('Could not discover {0}'.format(filename
))
92 def get_tags(stream_info
):
93 'gets all tags and their values from stream info'
94 taglist
= stream_info
.get_tags()
99 lambda list, tag
: tags
.append((tag
, list.get_value_index(tag
, 0))))
102 # date/datetime should be converted from GDate/GDateTime to strings
105 tags
['date'] = "%s-%s-%s" % (
106 date
.year
, date
.month
, date
.day
)
108 if 'datetime' in tags
:
109 # TODO: handle timezone info; gst.get_time_zone_offset +
110 # python's tzinfo should help
111 dt
= tags
['datetime']
112 tags
['datetime'] = datetime
.datetime(
113 dt
.get_year(), dt
.get_month(), dt
.get_day(), dt
.get_hour(),
114 dt
.get_minute(), dt
.get_second(),
115 dt
.get_microsecond()).isoformat()
116 for k
, v
in tags
.copy().items():
117 # types below are accepted by json; others must not present
118 if not isinstance(v
, (dict, list, six
.string_types
, int, float, bool,
123 def store_metadata(media_entry
, metadata
):
125 Store metadata from this video for this media entry.
127 stored_metadata
= dict()
128 audio_info_list
= metadata
.get_audio_streams()
130 stored_metadata
['audio'] = []
131 for audio_info
in audio_info_list
:
132 stored_metadata
['audio'].append(
134 'channels': audio_info
.get_channels(),
135 'bitrate': audio_info
.get_bitrate(),
136 'depth': audio_info
.get_depth(),
137 'languange': audio_info
.get_language(),
138 'sample_rate': audio_info
.get_sample_rate(),
139 'tags': get_tags(audio_info
)
142 video_info_list
= metadata
.get_video_streams()
144 stored_metadata
['video'] = []
145 for video_info
in video_info_list
:
146 stored_metadata
['video'].append(
148 'width': video_info
.get_width(),
149 'height': video_info
.get_height(),
150 'bitrate': video_info
.get_bitrate(),
151 'depth': video_info
.get_depth(),
152 'videorate': [video_info
.get_framerate_num(),
153 video_info
.get_framerate_denom()],
154 'tags': get_tags(video_info
)
157 stored_metadata
['common'] = {
158 'duration': metadata
.get_duration(),
159 'tags': get_tags(metadata
),
161 # Only save this field if there's something to save
162 if len(stored_metadata
):
163 media_entry
.media_data_init(orig_metadata
=stored_metadata
)
165 # =====================
169 def main_task(resolution
, medium_size
, **process_info
):
170 processor
= CommonVideoProcessor(process_info
['manager'], process_info
['entry'])
171 processor
.common_setup(resolution
)
172 processor
.transcode(medium_size
=medium_size
, vp8_quality
=process_info
['vp8_quality'],
173 vp8_threads
=process_info
['vp8_threads'], vorbis_quality
=process_info
['vorbis_quality'])
174 processor
.generate_thumb(thumb_size
=process_info
['thumb_size'])
175 processor
.store_orig_metadata()
179 def complimentary_task(resolution
, medium_size
, **process_info
):
180 processor
= CommonVideoProcessor(process_info
['manager'], process_info
['entry'])
181 processor
.common_setup(resolution
)
182 processor
.transcode(medium_size
=medium_size
, vp8_quality
=process_info
['vp8_quality'],
183 vp8_threads
=process_info
['vp8_threads'], vorbis_quality
=process_info
['vorbis_quality'])
187 def processing_cleanup(entry
, manager
):
188 processor
= CommonVideoProcessor(manager
, entry
)
189 processor
.delete_queue_file()
191 # =====================
194 class CommonVideoProcessor(MediaProcessor
):
196 Provides a base for various video processing steps
198 acceptable_files
= ['original, best_quality', 'webm_144p', 'webm_360p',
199 'webm_480p', 'webm_720p', 'webm_1080p', 'webm_video']
201 def common_setup(self
, resolution
=None):
202 self
.video_config
= mgg \
203 .global_config
['plugins'][MEDIA_TYPE
]
205 # Pull down and set up the processing file
206 self
.process_filename
= get_process_filename(
207 self
.entry
, self
.workbench
, self
.acceptable_files
)
208 self
.name_builder
= FilenameBuilder(self
.process_filename
)
210 self
.transcoder
= transcoders
.VideoTranscoder()
211 self
.did_transcode
= False
214 self
.curr_file
= 'webm_' + str(resolution
)
215 self
.part_filename
= (self
.name_builder
.fill('{basename}.' +
216 str(resolution
) + '.webm'))
218 self
.curr_file
= 'webm_video'
219 self
.part_filename
= self
.name_builder
.fill('{basename}.medium.webm')
221 def copy_original(self
):
222 # If we didn't transcode, then we need to keep the original
223 raise NotImplementedError
225 def _keep_best(self
):
227 If there is no original, keep the best file that we have
229 raise NotImplementedError
231 def _skip_processing(self
, keyname
, **kwargs
):
232 file_metadata
= self
.entry
.get_file_metadata(keyname
)
234 if not file_metadata
:
238 if 'webm' in keyname
:
239 if kwargs
.get('medium_size') != file_metadata
.get('medium_size'):
241 elif kwargs
.get('vp8_quality') != file_metadata
.get('vp8_quality'):
243 elif kwargs
.get('vp8_threads') != file_metadata
.get('vp8_threads'):
245 elif kwargs
.get('vorbis_quality') != \
246 file_metadata
.get('vorbis_quality'):
248 elif keyname
== 'thumb':
249 if kwargs
.get('thumb_size') != file_metadata
.get('thumb_size'):
255 def transcode(self
, medium_size
=None, vp8_quality
=None, vp8_threads
=None,
256 vorbis_quality
=None):
257 progress_callback
= ProgressCallback(self
.entry
)
258 tmp_dst
= os
.path
.join(self
.workbench
.dir, self
.part_filename
)
262 mgg
.global_config
['media:medium']['max_width'],
263 mgg
.global_config
['media:medium']['max_height'])
265 vp8_quality
= self
.video_config
['vp8_quality']
267 vp8_threads
= self
.video_config
['vp8_threads']
268 if not vorbis_quality
:
269 vorbis_quality
= self
.video_config
['vorbis_quality']
271 file_metadata
= {'medium_size': medium_size
,
272 'vp8_threads': vp8_threads
,
273 'vp8_quality': vp8_quality
,
274 'vorbis_quality': vorbis_quality
}
276 if self
._skip
_processing
(self
.curr_file
, **file_metadata
):
279 metadata
= transcoders
.discover(self
.process_filename
)
280 orig_dst_dimensions
= (metadata
.get_video_streams()[0].get_width(),
281 metadata
.get_video_streams()[0].get_height())
283 # Figure out whether or not we need to transcode this video or
285 if skip_transcode(metadata
, medium_size
):
286 _log
.debug('Skipping transcoding')
288 # If there is an original and transcoded, delete the transcoded
289 # since it must be of lower quality then the original
290 if self
.entry
.media_files
.get('original') and \
291 self
.entry
.media_files
.get(self
.curr_file
):
292 self
.entry
.media_files
[self
.curr_file
].delete()
295 self
.transcoder
.transcode(self
.process_filename
, tmp_dst
,
296 vp8_quality
=vp8_quality
,
297 vp8_threads
=vp8_threads
,
298 vorbis_quality
=vorbis_quality
,
299 progress_callback
=progress_callback
,
300 dimensions
=tuple(medium_size
))
301 if self
.transcoder
.dst_data
:
302 # Push transcoded video to public storage
303 _log
.debug('Saving medium...')
304 store_public(self
.entry
, 'webm_video', tmp_dst
,
305 self
.name_builder
.fill('{basename}.medium.webm'))
306 _log
.debug('Saved medium')
308 # Is this the file_metadata that paroneayea was talking about?
309 self
.entry
.set_file_metadata(self
.curr_file
, **file_metadata
)
311 self
.did_transcode
= True
313 def generate_thumb(self
, thumb_size
=None):
314 # Temporary file for the video thumbnail (cleaned up with workbench)
315 tmp_thumb
= os
.path
.join(self
.workbench
.dir,
316 self
.name_builder
.fill(
317 '{basename}.thumbnail.jpg'))
320 thumb_size
= (mgg
.global_config
['media:thumb']['max_width'],)
322 if self
._skip
_processing
('thumb', thumb_size
=thumb_size
):
325 # We will only use the width so that the correct scale is kept
326 transcoders
.capture_thumb(
327 self
.process_filename
,
331 # Checking if the thumbnail was correctly created. If it was not,
333 if not os
.path
.exists (tmp_thumb
):
336 # Push the thumbnail to public storage
337 _log
.debug('Saving thumbnail...')
338 store_public(self
.entry
, 'thumb', tmp_thumb
,
339 self
.name_builder
.fill('{basename}.thumbnail.jpg'))
341 self
.entry
.set_file_metadata('thumb', thumb_size
=thumb_size
)
343 def store_orig_metadata(self
):
345 # Extract metadata and keep a record of it
346 metadata
= transcoders
.discover(self
.process_filename
)
348 # metadata's stream info here is a DiscovererContainerInfo instance,
349 # it gets split into DiscovererAudioInfo and DiscovererVideoInfo;
350 # metadata itself has container-related data in tags, like video-codec
351 store_metadata(self
.entry
, metadata
)
354 class InitialProcessor(CommonVideoProcessor
):
356 Initial processing steps for new video
359 description
= "Initial processing"
362 def media_is_eligible(cls
, entry
=None, state
=None):
366 "unprocessed", "failed")
369 def generate_parser(cls
):
370 parser
= argparse
.ArgumentParser(
371 description
=cls
.description
,
377 metavar
=('max_width', 'max_height'),
388 help='0 means number_of_CPUs - 1')
393 help='Range -0.1..1')
398 metavar
=('max_width', 'max_height'),
404 def args_to_request(cls
, args
):
405 return request_from_args(
406 args
, ['medium_size', 'vp8_quality', 'vp8_threads',
407 'vorbis_quality', 'thumb_size'])
409 def process(self
, medium_size
=None, vp8_threads
=None, vp8_quality
=None,
410 vorbis_quality
=None, thumb_size
=None, resolution
=None):
411 self
.common_setup(resolution
=resolution
)
412 self
.store_orig_metadata()
413 self
.transcode(medium_size
=medium_size
, vp8_quality
=vp8_quality
,
414 vp8_threads
=vp8_threads
, vorbis_quality
=vorbis_quality
)
416 self
.generate_thumb(thumb_size
=thumb_size
)
417 self
.delete_queue_file()
420 class Resizer(CommonVideoProcessor
):
422 Video thumbnail resizing process steps for processed media
425 description
= 'Resize thumbnail'
426 thumb_size
= 'thumb_size'
429 def media_is_eligible(cls
, entry
=None, state
=None):
432 return state
in 'processed'
435 def generate_parser(cls
):
436 parser
= argparse
.ArgumentParser(
437 description
=cls
.description
,
443 metavar
=('max_width', 'max_height'),
446 # Needed for gmg reprocess thumbs to work
456 def args_to_request(cls
, args
):
457 return request_from_args(
458 args
, ['thumb_size', 'file'])
460 def process(self
, thumb_size
=None, file=None):
462 self
.generate_thumb(thumb_size
=thumb_size
)
465 class Transcoder(CommonVideoProcessor
):
467 Transcoding processing steps for processed video
470 description
= 'Re-transcode video'
473 def media_is_eligible(cls
, entry
=None, state
=None):
476 return state
in 'processed'
479 def generate_parser(cls
):
480 parser
= argparse
.ArgumentParser(
481 description
=cls
.description
,
487 metavar
=('max_width', 'max_height'),
498 help='0 means number_of_CPUs - 1')
503 help='Range -0.1..1')
508 def args_to_request(cls
, args
):
509 return request_from_args(
510 args
, ['medium_size', 'vp8_threads', 'vp8_quality',
513 def process(self
, medium_size
=None, vp8_quality
=None, vp8_threads
=None,
514 vorbis_quality
=None):
516 self
.transcode(medium_size
=medium_size
, vp8_threads
=vp8_threads
,
517 vp8_quality
=vp8_quality
, vorbis_quality
=vorbis_quality
)
520 class VideoProcessingManager(ProcessingManager
):
522 super(VideoProcessingManager
, self
).__init
__()
523 self
.add_processor(InitialProcessor
)
524 self
.add_processor(Resizer
)
525 self
.add_processor(Transcoder
)
527 def workflow(self
, entry
, manager
, feed_url
, reprocess_action
,
528 reprocess_info
=None):
530 reprocess_info
['entry'] = entry
531 reprocess_info
['manager'] = manager
533 transcoding_tasks
= group(
534 main_task
.signature(args
=('480p', ACCEPTED_RESOLUTIONS
['480p']),
535 kwargs
=reprocess_info
, queue
='default',
536 priority
=5, immutable
=True),
537 complimentary_task
.signature(args
=('360p', ACCEPTED_RESOLUTIONS
['360p']),
538 kwargs
=reprocess_info
, queue
='default',
539 priority
=4, immutable
=True),
540 complimentary_task
.signature(args
=('720p', ACCEPTED_RESOLUTIONS
['720p']),
541 kwargs
=reprocess_info
, queue
='default',
542 priority
=3, immutable
=True),
545 cleanup_task
= processing_cleanup
.signature(args
=(entry
, manager
),
546 queue
='default', immutable
=True)
548 chord(transcoding_tasks
)(cleanup_task
)