Media type refractors, pep8, lint
[mediagoblin.git] / mediagoblin / media_types / video / processing.py
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
17 import tempfile
18 import logging
19 import os
20
21 from mediagoblin import mg_globals as mgg
22 from mediagoblin.processing import create_pub_filepath
23 from . import transcoders
24
25 logging.basicConfig()
26
27 _log = logging.getLogger(__name__)
28 _log.setLevel(logging.DEBUG)
29
30
31 def sniff_handler(media_file, **kw):
32 transcoder = transcoders.VideoTranscoder()
33 data = transcoder.discover(media_file.name)
34
35 _log.debug('Discovered: {0}'.format(data))
36
37 if not data:
38 _log.error('Could not discover {0}'.format(
39 kw.get('media')))
40 return False
41
42 if data['is_video'] == True:
43 return True
44
45 return False
46
47
48 def process_video(entry):
49 """
50 Process a video entry, transcode the queued media files (originals) and
51 create a thumbnail for the entry.
52 """
53 video_config = mgg.global_config['media_type:mediagoblin.media_types.video']
54
55 workbench = mgg.workbench_manager.create_workbench()
56
57 queued_filepath = entry.queued_media_file
58 queued_filename = workbench.localized_file(
59 mgg.queue_store, queued_filepath,
60 'source')
61
62 medium_filepath = create_pub_filepath(
63 entry,
64 '{original}-640p.webm'.format(
65 original=os.path.splitext(
66 queued_filepath[-1])[0] # Select the file name without .ext
67 ))
68
69 thumbnail_filepath = create_pub_filepath(
70 entry, 'thumbnail.jpg')
71
72 # Create a temporary file for the video destination
73 tmp_dst = tempfile.NamedTemporaryFile()
74
75 with tmp_dst:
76 # Transcode queued file to a VP8/vorbis file that fits in a 640x640 square
77 transcoder = transcoders.VideoTranscoder()
78 transcoder.transcode(queued_filename, tmp_dst.name)
79
80 # Push transcoded video to public storage
81 _log.debug('Saving medium...')
82 mgg.public_store.get_file(medium_filepath, 'wb').write(
83 tmp_dst.read())
84 _log.debug('Saved medium')
85
86 entry.media_files['webm_640'] = medium_filepath
87
88 # Save the width and height of the transcoded video
89 entry.media_data['video'] = {
90 u'width': transcoder.dst_data.videowidth,
91 u'height': transcoder.dst_data.videoheight}
92
93 # Create a temporary file for the video thumbnail
94 tmp_thumb = tempfile.NamedTemporaryFile()
95
96 with tmp_thumb:
97 # Create a thumbnail.jpg that fits in a 180x180 square
98 transcoders.VideoThumbnailer(queued_filename, tmp_thumb.name)
99
100 # Push the thumbnail to public storage
101 _log.debug('Saving thumbnail...')
102 mgg.public_store.get_file(thumbnail_filepath, 'wb').write(
103 tmp_thumb.read())
104 _log.debug('Saved thumbnail')
105
106 entry.media_files['thumb'] = thumbnail_filepath
107
108 if video_config['keep_original']:
109 # Push original file to public storage
110 queued_file = file(queued_filename, 'rb')
111
112 with queued_file:
113 original_filepath = create_pub_filepath(
114 entry,
115 queued_filepath[-1])
116
117 with mgg.public_store.get_file(original_filepath, 'wb') as \
118 original_file:
119 _log.debug('Saving original...')
120 original_file.write(queued_file.read())
121 _log.debug('Saved original')
122
123 entry.media_files['original'] = original_filepath
124
125 mgg.queue_store.delete_file(queued_filepath)
126
127 # Save the MediaEntry
128 entry.save()