Ticket #716: do-not-use-NamedTemporaryFile-video-processing.patch

File do-not-use-NamedTemporaryFile-video-processing.patch, 6.1 KB (added by Kushal Kumaran, 11 years ago)

patch that removes use of tempfile.NamedTemporaryFile in video processing

  • mediagoblin/media_types/video/processing.py

    diff --git a/mediagoblin/media_types/video/processing.py b/mediagoblin/media_types/video/processing.py
    index ff2c94a..554b0c8 100644
    a b  
    1414# You should have received a copy of the GNU Affero General Public License
    1515# along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1616
    17 from tempfile import NamedTemporaryFile
     17import os.path
    1818import logging
    1919import datetime
    2020
    def process_video(proc_state):  
    7070    queued_filename = proc_state.get_queued_filename()
    7171    name_builder = FilenameBuilder(queued_filename)
    7272
    73     medium_filepath = create_pub_filepath(
    74         entry, name_builder.fill('{basename}-640p.webm'))
     73    medium_basename = name_builder.fill('{basename}-640p.webm')
     74    medium_filepath = create_pub_filepath(entry, medium_basename)
    7575
    76     thumbnail_filepath = create_pub_filepath(
    77         entry, name_builder.fill('{basename}.thumbnail.jpg'))
     76    thumbnail_basename = name_builder.fill('{basename}.thumbnail.jpg')
     77    thumbnail_filepath = create_pub_filepath(entry, thumbnail_basename)
    7878
    7979    # Create a temporary file for the video destination (cleaned up with workbench)
    80     tmp_dst = NamedTemporaryFile(dir=workbench.dir, delete=False)
    81     with tmp_dst:
    82         # Transcode queued file to a VP8/vorbis file that fits in a 640x640 square
    83         progress_callback = ProgressCallback(entry)
     80    tmp_dst = os.path.join(workbench.dir, medium_basename)
     81    # Transcode queued file to a VP8/vorbis file that fits in a 640x640 square
     82    progress_callback = ProgressCallback(entry)
    8483
    85         dimensions = (
    86             mgg.global_config['media:medium']['max_width'],
    87             mgg.global_config['media:medium']['max_height'])
     84    dimensions = (
     85        mgg.global_config['media:medium']['max_width'],
     86        mgg.global_config['media:medium']['max_height'])
    8887
    89         # Extract metadata and keep a record of it
    90         metadata = transcoders.VideoTranscoder().discover(queued_filename)
    91         store_metadata(entry, metadata)
     88    # Extract metadata and keep a record of it
     89    metadata = transcoders.VideoTranscoder().discover(queued_filename)
     90    store_metadata(entry, metadata)
    9291
    93         # Figure out whether or not we need to transcode this video or
    94         # if we can skip it
    95         if skip_transcode(metadata):
    96             _log.debug('Skipping transcoding')
     92    # Figure out whether or not we need to transcode this video or
     93    # if we can skip it
     94    if skip_transcode(metadata):
     95        _log.debug('Skipping transcoding')
    9796
    98             dst_dimensions = metadata['videowidth'], metadata['videoheight']
     97        dst_dimensions = metadata['videowidth'], metadata['videoheight']
    9998
    10099            # Push original file to public storage
    101             _log.debug('Saving original...')
    102             proc_state.copy_original(queued_filepath[-1])
     100        _log.debug('Saving original...')
     101        proc_state.copy_original(queued_filepath[-1])
    103102
    104             did_transcode = False
    105         else:
    106             transcoder = transcoders.VideoTranscoder()
     103        did_transcode = False
     104    else:
     105        transcoder = transcoders.VideoTranscoder()
    107106
    108             transcoder.transcode(queued_filename, tmp_dst.name,
    109                     vp8_quality=video_config['vp8_quality'],
    110                     vp8_threads=video_config['vp8_threads'],
    111                     vorbis_quality=video_config['vorbis_quality'],
    112                     progress_callback=progress_callback,
    113                     dimensions=dimensions)
     107        transcoder.transcode(queued_filename, tmp_dst,
     108                vp8_quality=video_config['vp8_quality'],
     109                vp8_threads=video_config['vp8_threads'],
     110                vorbis_quality=video_config['vorbis_quality'],
     111                progress_callback=progress_callback,
     112                dimensions=dimensions)
    114113
    115             dst_dimensions = transcoder.dst_data.videowidth,\
    116                     transcoder.dst_data.videoheight
     114        dst_dimensions = transcoder.dst_data.videowidth,\
     115            transcoder.dst_data.videoheight
    117116
    118             # Push transcoded video to public storage
    119             _log.debug('Saving medium...')
    120             mgg.public_store.copy_local_to_storage(tmp_dst.name, medium_filepath)
    121             _log.debug('Saved medium')
     117        # Push transcoded video to public storage
     118        _log.debug('Saving medium...')
     119        mgg.public_store.copy_local_to_storage(tmp_dst, medium_filepath)
     120        _log.debug('Saved medium')
    122121
    123             entry.media_files['webm_640'] = medium_filepath
     122        entry.media_files['webm_640'] = medium_filepath
    124123
    125             did_transcode = True
     124        did_transcode = True
    126125
    127         # Save the width and height of the transcoded video
    128         entry.media_data_init(
    129             width=dst_dimensions[0],
    130             height=dst_dimensions[1])
     126    # Save the width and height of the transcoded video
     127    entry.media_data_init(
     128        width=dst_dimensions[0],
     129        height=dst_dimensions[1])
    131130
    132131    # Temporary file for the video thumbnail (cleaned up with workbench)
    133     tmp_thumb = NamedTemporaryFile(dir=workbench.dir, suffix='.jpg', delete=False)
    134 
    135     with tmp_thumb:
    136         # Create a thumbnail.jpg that fits in a 180x180 square
    137         transcoders.VideoThumbnailerMarkII(
    138                 queued_filename,
    139                 tmp_thumb.name,
    140                 180)
    141 
    142         # Push the thumbnail to public storage
    143         _log.debug('Saving thumbnail...')
    144         mgg.public_store.copy_local_to_storage(tmp_thumb.name, thumbnail_filepath)
    145         entry.media_files['thumb'] = thumbnail_filepath
     132    tmp_thumb = os.path.join(workbench.dir, thumbnail_basename)
     133
     134    # Create a thumbnail.jpg that fits in a 180x180 square
     135    transcoders.VideoThumbnailerMarkII(
     136        queued_filename,
     137        tmp_thumb,
     138        180)
     139
     140    # Push the thumbnail to public storage
     141    _log.debug('Saving thumbnail...')
     142    mgg.public_store.copy_local_to_storage(tmp_thumb, thumbnail_filepath)
     143    entry.media_files['thumb'] = thumbnail_filepath
    146144
    147145    # save the original... but only if we did a transcoding
    148146    # (if we skipped transcoding and just kept the original anyway as the main