Convert media processing backends to delete the queue directory (#254)
[mediagoblin.git] / mediagoblin / media_types / video / processing.py
index 4cae1fd8280f1d4afd85c6f1985f64e886f82bdb..68d14148af97aa281fd5f6ed4796cda2548e960d 100644 (file)
@@ -1,5 +1,5 @@
 # GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import Image
-import tempfile
-import pkg_resources
-
-from celery.task import Task
-from celery import registry
-
-from mediagoblin.db.util import ObjectId
-from mediagoblin import mg_globals as mgg
-
-from mediagoblin.util import lazy_pass_to_ugettext as _
-
-import mediagoblin.media_types.video
-
-import gobject
-gobject.threads_init()
-
-import gst
-import arista
+from tempfile import NamedTemporaryFile
 import logging
 
-from arista.transcoder import TranscoderOptions
-
-THUMB_SIZE = 180, 180
-MEDIUM_SIZE = 640, 640
-ARISTA_DEVICE_KEY = 'web'
-
-
-loop = None
-logger = logging.getLogger(__name__)
-logging.basicConfig()
-logger.setLevel(logging.DEBUG)
-
-
-def process_video(entry):
-    """
-    Code to process a video
-    """
-    global loop
-    loop = None
-    info = {}
-    workbench = mgg.workbench_manager.create_workbench()
-
-    queued_filepath = entry['queued_media_file']
-    queued_filename = workbench.localized_file(
-        mgg.queue_store, queued_filepath,
-        'source')
-
-    arista.init()
-
-
-    web_advanced_preset = pkg_resources.resource_filename(
-        __name__,
-        'presets/web-advanced.json')
-    device = arista.presets.load(web_advanced_preset)
-
-    queue = arista.queue.TranscodeQueue()
-    
-    info['tmp_file'] = tmp_file = tempfile.NamedTemporaryFile()
-
-    info['medium_filepath'] = medium_filepath = create_pub_filepath(entry, 'video.webm')
-
-    output = tmp_file.name
-
-    uri = 'file://' + queued_filename
-
-    preset = device.presets[device.default]
-
-    logger.debug('preset: {0}'.format(preset))
-
-    opts = TranscoderOptions(uri, preset, output)
-
-    queue.append(opts)
-
-    info['entry'] = entry
-
-    queue.connect("entry-start", _transcoding_start, info)
-    queue.connect("entry-pass-setup", _transcoding_pass_setup, info)
-    queue.connect("entry-error", _transcoding_error, info)
-    queue.connect("entry-complete", _transcoding_complete, info)
-
-    info['loop'] = loop = gobject.MainLoop()
-    info['queued_filename'] = queued_filename
-    info['queued_filepath'] = queued_filepath
-    info['workbench'] = workbench
-
-    logger.debug('info: {0}'.format(info))
-
-    loop.run()
-    
-    '''
-    try:
-        #thumb = Image.open(mediagoblin.media_types.video.MEDIA_MANAGER['default_thumb'])
-    except IOError:
-        raise BadMediaFail()
+from mediagoblin import mg_globals as mgg
+from mediagoblin.decorators import get_workbench
+from mediagoblin.processing import \
+    create_pub_filepath, FilenameBuilder, BaseProcessingFail, ProgressCallback
+from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
 
-    thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
-    # ensure color mode is compatible with jpg
-    if thumb.mode != "RGB":
-        thumb = thumb.convert("RGB")
+from . import transcoders
 
-    thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg')
-    thumb_file = mgg.public_store.get_file(thumb_filepath, 'w')
+_log = logging.getLogger(__name__)
+_log.setLevel(logging.DEBUG)
 
-    with thumb_file:
-        thumb.save(thumb_file, "JPEG", quality=90)
-    '''
 
-def __close_processing(queue, qentry, info, error=False):
+class VideoTranscodingFail(BaseProcessingFail):
     '''
-    Update MediaEntry, move files, handle errors
+    Error raised if video transcoding fails
     '''
-    if not error:
-        qentry.transcoder.stop()
-        gobject.idle_add(info['loop'].quit)
-        info['loop'].quit()
+    general_message = _(u'Video transcoding failed')
 
-        print('\n-> Saving video...\n')
 
-        with info['tmp_file'] as tmp_file:
-            mgg.public_store.get_file(info['medium_filepath'], 'wb').write(
-                tmp_file.read())
-            info['entry']['media_files']['medium'] = info['medium_filepath']
+def sniff_handler(media_file, **kw):
+    transcoder = transcoders.VideoTranscoder()
+    data = transcoder.discover(media_file.name)
 
-        print('\n=== DONE! ===\n')
+    _log.debug('Discovered: {0}'.format(data))
 
-        # we have to re-read because unlike PIL, not everything reads
-        # things in string representation :)
-        queued_file = file(info['queued_filename'], 'rb')
-
-        with queued_file:
-            original_filepath = create_pub_filepath(info['entry'], info['queued_filepath'][-1])
-
-            with mgg.public_store.get_file(original_filepath, 'wb') as original_file:
-                original_file.write(queued_file.read())
-
-        mgg.queue_store.delete_file(info['queued_filepath'])
-        info['entry']['queued_media_file'] = []
-        media_files_dict = info['entry'].setdefault('media_files', {})
-        media_files_dict['original'] = original_filepath
-        # media_files_dict['thumb'] = thumb_filepath
-
-        info['entry']['state'] = u'processed'
-        info['entry'].save()
-
-    else:
-        qentry.transcoder.stop()
-        gobject.idle_add(info['loop'].quit)
-        info['loop'].quit()
-        info['entry']['state'] = u'failed'
-        info['entry'].save()
-
-    # clean up workbench
-    info['workbench'].destroy_self()
-
-
-def _transcoding_start(queue, qentry, info):
-    logger.info('-> Starting transcoding')
-    logger.debug(queue, qentry, info)
-
-def _transcoding_complete(*args):
-    __close_processing(*args)
-    print(args)
-
-def _transcoding_error(*args):
-    logger.info('-> Error')
-    __close_processing(*args, error=True)
-    logger.debug(*args)
-
-def _transcoding_pass_setup(queue, qentry, options):
-    logger.info('-> Pass setup')
-    logger.debug(queue, qentry, options)
-
-
-def check_interrupted():
-    """
-        Check whether we have been interrupted by Ctrl-C and stop the
-        transcoder.
-    """
-    if interrupted:
-        try:
-            source = transcoder.pipe.get_by_name("source")
-            source.send_event(gst.event_new_eos())
-        except:
-            # Something pretty bad happened... just exit!
-            gobject.idle_add(loop.quit)
-            
+    if not data:
+        _log.error('Could not discover {0}'.format(
+                kw.get('media')))
         return False
-    return True
-    
 
-def create_pub_filepath(entry, filename):
-    return mgg.public_store.get_unique_filepath(
-            ['media_entries',
-             unicode(entry['_id']),
-             filename])
+    if data['is_video'] == True:
+        return True
 
+    return False
 
-class BaseProcessingFail(Exception):
+@get_workbench
+def process_video(entry, workbench=None):
     """
-    Base exception that all other processing failure messages should
-    subclass from.
-  
-    You shouldn't call this itself; instead you should subclass it
-    and provid the exception_path and general_message applicable to
-    this error.
-    """
-    general_message = u''
-  
-    @property
-    def exception_path(self):
-        return u"%s:%s" % (
-            self.__class__.__module__, self.__class__.__name__)
+    Process a video entry, transcode the queued media files (originals) and
+    create a thumbnail for the entry.
 
-    def __init__(self, **metadata):
-        self.metadata = metadata or {}
-  
-  
-class BadMediaFail(BaseProcessingFail):
-    """
-    Error that should be raised when an inappropriate file was given
-    for the media type specified.
+    A Workbench() represents a local tempory dir. It is automatically
+    cleaned up when this function exits.
     """
-    general_message = _(u'Invalid file given for media type.')
-
-
-################################
-# Media processing initial steps
-################################
-
-class ProcessMedia(Task):
-    """
-    Pass this entry off for processing.
-    """
-    def run(self, media_id):
-        """
-        Pass the media entry off to the appropriate processing function
-        (for now just process_image...)
-        """
-        entry = mgg.database.MediaEntry.one(
-            {'_id': ObjectId(media_id)})
-
-        # Try to process, and handle expected errors.
-        try:
-            process_video(entry)
-        except BaseProcessingFail, exc:
-            mark_entry_failed(entry[u'_id'], exc)
-            return
-
-    def on_failure(self, exc, task_id, args, kwargs, einfo):
-        """
-        If the processing failed we should mark that in the database.
-
-        Assuming that the exception raised is a subclass of BaseProcessingFail,
-        we can use that to get more information about the failure and store that
-        for conveying information to users about the failure, etc.
-        """
-        entry_id = args[0]
-        mark_entry_failed(entry_id, exc)
-
-
-process_media = registry.tasks[ProcessMedia.name]
-
-
-def mark_entry_failed(entry_id, exc):
-    """
-    Mark a media entry as having failed in its conversion.
-
-    Uses the exception that was raised to mark more information.  If the
-    exception is a derivative of BaseProcessingFail then we can store extra
-    information that can be useful for users telling them why their media failed
-    to process.
-
-    Args:
-     - entry_id: The id of the media entry
-
-    """
-    # Was this a BaseProcessingFail?  In other words, was this a
-    # type of error that we know how to handle?
-    if isinstance(exc, BaseProcessingFail):
-        # Looks like yes, so record information about that failure and any
-        # metadata the user might have supplied.
-        mgg.database['media_entries'].update(
-            {'_id': entry_id},
-            {'$set': {u'state': u'failed',
-                      u'fail_error': exc.exception_path,
-                      u'fail_metadata': exc.metadata}})
-    else:
-        # Looks like no, so just mark it as failed and don't record a
-        # failure_error (we'll assume it wasn't handled) and don't record
-        # metadata (in fact overwrite it if somehow it had previous info
-        # here)
-        mgg.database['media_entries'].update(
-            {'_id': entry_id},
-            {'$set': {u'state': u'failed',
-                      u'fail_error': None,
-                      u'fail_metadata': {}}})
+    video_config = mgg.global_config['media_type:mediagoblin.media_types.video']
 
+    queued_filepath = entry.queued_media_file
+    queued_filename = workbench.localized_file(
+        mgg.queue_store, queued_filepath,
+        'source')
+    name_builder = FilenameBuilder(queued_filename)
+
+    medium_filepath = create_pub_filepath(
+        entry, name_builder.fill('{basename}-640p.webm'))
+
+    thumbnail_filepath = create_pub_filepath(
+        entry, name_builder.fill('{basename}.thumbnail.jpg'))
+
+    # Create a temporary file for the video destination (cleaned up with workbench)
+    tmp_dst = NamedTemporaryFile(dir=workbench.dir, delete=False)
+    with tmp_dst:
+        # Transcode queued file to a VP8/vorbis file that fits in a 640x640 square
+        progress_callback = ProgressCallback(entry)
+        transcoder = transcoders.VideoTranscoder()
+        transcoder.transcode(queued_filename, tmp_dst.name,
+                vp8_quality=video_config['vp8_quality'],
+                vp8_threads=video_config['vp8_threads'],
+                vorbis_quality=video_config['vorbis_quality'],
+                progress_callback=progress_callback)
+
+    # Push transcoded video to public storage
+    _log.debug('Saving medium...')
+    mgg.public_store.copy_local_to_storage(tmp_dst.name, medium_filepath)
+    _log.debug('Saved medium')
+
+    entry.media_files['webm_640'] = medium_filepath
+
+    # Save the width and height of the transcoded video
+    entry.media_data_init(
+        width=transcoder.dst_data.videowidth,
+        height=transcoder.dst_data.videoheight)
+
+    # Temporary file for the video thumbnail (cleaned up with workbench)
+    tmp_thumb = NamedTemporaryFile(dir=workbench.dir, suffix='.jpg', delete=False)
+
+    with tmp_thumb:
+        # Create a thumbnail.jpg that fits in a 180x180 square
+        transcoders.VideoThumbnailerMarkII(
+                queued_filename,
+                tmp_thumb.name,
+                180)
+
+    # Push the thumbnail to public storage
+    _log.debug('Saving thumbnail...')
+    mgg.public_store.copy_local_to_storage(tmp_thumb.name, thumbnail_filepath)
+    entry.media_files['thumb'] = thumbnail_filepath
+
+    if video_config['keep_original']:
+        # Push original file to public storage
+        _log.debug('Saving original...')
+        original_filepath = create_pub_filepath(entry, queued_filepath[-1])
+        mgg.public_store.copy_local_to_storage(queued_filename, original_filepath)
+        entry.media_files['original'] = original_filepath
+
+    # Remove queued media file from storage and database.
+    # queued_filepath is in the task_id directory which should
+    # be removed too, but fail if the directory is not empty to be on
+    # the super-safe side.
+    mgg.queue_store.delete_file(queued_filepath)      # rm file
+    mgg.queue_store.delete_dir(queued_filepath[:-1])  # rm dir
+    entry.queued_media_file = []