Bug 681 - Comments from reviewing the new video merge
[mediagoblin.git] / mediagoblin / media_types / video / processing.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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
17 import tempfile
18 import logging
19 import os
20
21 from mediagoblin import mg_globals as mgg
22 from mediagoblin.processing import mark_entry_failed, \
23 THUMB_SIZE, MEDIUM_SIZE, create_pub_filepath
24 from . import transcoders
25
26 logging.basicConfig()
27
28 _log = logging.getLogger(__name__)
29 _log.setLevel(logging.DEBUG)
30
31
32 def process_video(entry):
33 """
34 Code to process a video
35
36 Much of this code is derived from the arista-transcoder script in
37 the arista PyPI package and changed to match the needs of
38 MediaGoblin
39
40 This function sets up the arista video encoder in some kind of new thread
41 and attaches callbacks to that child process, hopefully, the
42 entry-complete callback will be called when the video is done.
43 """
44 workbench = mgg.workbench_manager.create_workbench()
45
46 queued_filepath = entry['queued_media_file']
47 queued_filename = workbench.localized_file(
48 mgg.queue_store, queued_filepath,
49 'source')
50
51 medium_filepath = create_pub_filepath(
52 entry,
53 '{original}-640p.webm'.format(
54 original=os.path.splitext(
55 queued_filepath[-1])[0] # Select the
56 ))
57
58 thumbnail_filepath = create_pub_filepath(
59 entry, 'thumbnail.jpg')
60
61
62 # Create a temporary file for the video destination
63 tmp_dst = tempfile.NamedTemporaryFile()
64
65 with tmp_dst:
66 # Transcode queued file to a VP8/vorbis file that fits in a 640x640 square
67 transcoder = transcoders.VideoTranscoder(queued_filename, tmp_dst.name)
68
69 # Push transcoded video to public storage
70 _log.debug('Saving medium...')
71 mgg.public_store.get_file(medium_filepath, 'wb').write(
72 tmp_dst.read())
73 _log.debug('Saved medium')
74
75 entry['media_files']['webm_640'] = medium_filepath
76
77 # Save the width and height of the transcoded video
78 entry['media_data']['video'] = {
79 u'width': transcoder.dst_data.videowidth,
80 u'height': transcoder.dst_data.videoheight}
81
82 # Create a temporary file for the video thumbnail
83 tmp_thumb = tempfile.NamedTemporaryFile()
84
85 with tmp_thumb:
86 # Create a thumbnail.jpg that fits in a 180x180 square
87 transcoders.VideoThumbnailer(queued_filename, tmp_thumb.name)
88
89 # Push the thumbnail to public storage
90 _log.debug('Saving thumbnail...')
91 mgg.public_store.get_file(thumbnail_filepath, 'wb').write(
92 tmp_thumb.read())
93 _log.debug('Saved thumbnail')
94
95 entry['media_files']['thumb'] = thumbnail_filepath
96
97
98 # Push original file to public storage
99 queued_file = file(queued_filename, 'rb')
100
101 with queued_file:
102 original_filepath = create_pub_filepath(
103 entry,
104 queued_filepath[-1])
105
106 with mgg.public_store.get_file(original_filepath, 'wb') as \
107 original_file:
108 _log.debug('Saving original...')
109 original_file.write(queued_file.read())
110 _log.debug('Saved original')
111
112 entry['media_files']['original'] = original_filepath
113
114 mgg.queue_store.delete_file(queued_filepath)
115
116
117 # Save the MediaEntry
118 entry.save()