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/>.
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
28 _log
= logging
.getLogger(__name__
)
29 _log
.setLevel(logging
.DEBUG
)
31 def sniff_handler(media_file
, **kw
):
34 def process_video(entry
):
36 Process a video entry, transcode the queued media files (originals) and
37 create a thumbnail for the entry.
39 video_config
= mgg
.global_config
['media_type:mediagoblin.media_types.video']
41 workbench
= mgg
.workbench_manager
.create_workbench()
43 queued_filepath
= entry
.queued_media_file
44 queued_filename
= workbench
.localized_file(
45 mgg
.queue_store
, queued_filepath
,
48 medium_filepath
= create_pub_filepath(
50 '{original}-640p.webm'.format(
51 original
=os
.path
.splitext(
52 queued_filepath
[-1])[0] # Select the file name without .ext
55 thumbnail_filepath
= create_pub_filepath(
56 entry
, 'thumbnail.jpg')
59 # Create a temporary file for the video destination
60 tmp_dst
= tempfile
.NamedTemporaryFile()
63 # Transcode queued file to a VP8/vorbis file that fits in a 640x640 square
64 transcoder
= transcoders
.VideoTranscoder(queued_filename
, tmp_dst
.name
)
66 # Push transcoded video to public storage
67 _log
.debug('Saving medium...')
68 mgg
.public_store
.get_file(medium_filepath
, 'wb').write(
70 _log
.debug('Saved medium')
72 entry
.media_files
['webm_640'] = medium_filepath
74 # Save the width and height of the transcoded video
75 entry
.media_data
['video'] = {
76 u
'width': transcoder
.dst_data
.videowidth
,
77 u
'height': transcoder
.dst_data
.videoheight
}
79 # Create a temporary file for the video thumbnail
80 tmp_thumb
= tempfile
.NamedTemporaryFile()
83 # Create a thumbnail.jpg that fits in a 180x180 square
84 transcoders
.VideoThumbnailer(queued_filename
, tmp_thumb
.name
)
86 # Push the thumbnail to public storage
87 _log
.debug('Saving thumbnail...')
88 mgg
.public_store
.get_file(thumbnail_filepath
, 'wb').write(
90 _log
.debug('Saved thumbnail')
92 entry
.media_files
['thumb'] = thumbnail_filepath
94 if video_config
['keep_original']:
95 # Push original file to public storage
96 queued_file
= file(queued_filename
, 'rb')
99 original_filepath
= create_pub_filepath(
103 with mgg
.public_store
.get_file(original_filepath
, 'wb') as \
105 _log
.debug('Saving original...')
106 original_file
.write(queued_file
.read())
107 _log
.debug('Saved original')
109 entry
.media_files
['original'] = original_filepath
111 mgg
.queue_store
.delete_file(queued_filepath
)
113 # Save the MediaEntry