Check tags for existence before using them
[mediagoblin.git] / mediagoblin / media_types / video / util.py
CommitLineData
5c754fda
JW
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
17import logging
18
19from mediagoblin import mg_globals as mgg
20
21_log = logging.getLogger(__name__)
22
23
8bb0df62 24def skip_transcode(metadata, size):
5c754fda
JW
25 '''
26 Checks video metadata against configuration values for skip_transcode.
27
28 Returns True if the video matches the requirements in the configuration.
29 '''
9a6741d7 30 config = mgg.global_config['plugins']['mediagoblin.media_types.video']\
5c754fda
JW
31 ['skip_transcode']
32
fec916df 33 # XXX: how were we supposed to use it?
5c754fda
JW
34 medium_config = mgg.global_config['media:medium']
35
36 _log.debug('skip_transcode config: {0}'.format(config))
fec916df 37
91f5f5e7 38 tags = metadata.get_tags()
fec916df
BB
39 if not tags:
40 return False
41
4522ecef
BB
42 if config['mime_types'] and tags.get_string('mimetype')[0]:
43 if not tags.get_string('mimetype')[1] in config['mime_types']:
5c754fda
JW
44 return False
45
4522ecef
BB
46 if config['container_formats'] and tags.get_string('container-format')[0]:
47 if not (tags.get_string('container-format')[1] in
91f5f5e7 48 config['container_formats']):
5c754fda
JW
49 return False
50
2d1e8905
BB
51 if config['video_codecs']:
52 for video_info in metadata.get_video_streams():
4522ecef 53 if not (video_info.get_tags().get_string('video-codec')[1] in
2d1e8905
BB
54 config['video_codecs']):
55 return False
5c754fda 56
2d1e8905
BB
57 if config['audio_codecs']:
58 for audio_info in metadata.get_audio_streams():
4522ecef 59 if not (audio_info.get_tags().get_string('audio-codec')[1] in
2d1e8905
BB
60 config['audio_codecs']):
61 return False
5c754fda
JW
62
63 if config['dimensions_match']:
2d1e8905
BB
64 for video_info in metadata.get_video_streams():
65 if not video_info.get_height() <= size[1]:
66 return False
67 if not video_info.get_width() <= size[0]:
68 return False
5c754fda
JW
69
70 return True