Fixed Copyright Headers
[mediagoblin.git] / mediagoblin / media_types / video / models.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
18 from mediagoblin.db.base import Base
19
20 from sqlalchemy import (
21 Column, Integer, SmallInteger, ForeignKey)
22 from sqlalchemy.orm import relationship, backref
23 from mediagoblin.db.extratypes import JSONEncoded
24 from mediagoblin.media_types import video
25
26
27 BACKREF_NAME = "video__media_data"
28
29
30 class VideoData(Base):
31 """
32 Attributes:
33 - media_entry: the originating media entry (of course)
34 - width: width of the transcoded video
35 - height: height of the transcoded video
36 - orig_metadata: A loose json structure containing metadata gstreamer
37 pulled from the original video.
38 This field is NOT GUARANTEED to exist!
39
40 Likely metadata extracted:
41 "videoheight", "videolength", "videowidth",
42 "audiorate", "audiolength", "audiochannels", "audiowidth",
43 "mimetype", "tags"
44
45 TODO: document the above better.
46 """
47 __tablename__ = "video__mediadata"
48
49 # The primary key *and* reference to the main media_entry
50 media_entry = Column(Integer, ForeignKey('core__media_entries.id'),
51 primary_key=True)
52 get_media_entry = relationship("MediaEntry",
53 backref=backref(BACKREF_NAME, uselist=False,
54 cascade="all, delete-orphan"))
55
56 width = Column(SmallInteger)
57 height = Column(SmallInteger)
58
59 orig_metadata = Column(JSONEncoded)
60
61 def source_type(self):
62 """
63 Construct a useful type=... that is to say, used like:
64 <video><source type="{{ entry.media_data.source_type() }}" /></video>
65
66 Try to construct it out of self.orig_metadata... if we fail we
67 just dope'ily fall back on DEFAULT_WEBM_TYPE
68 """
69 orig_metadata = self.orig_metadata or {}
70
71 if ("webm_video" not in self.get_media_entry.media_files
72 and "mimetype" in orig_metadata['common']['tags']
73 and "codec" in orig_metadata['audio']
74 and "codec" in orig_metadata['video']):
75 if orig_metadata['mimetype'] == 'application/ogg':
76 # stupid ambiguous .ogg extension
77 mimetype = "video/ogg"
78 else:
79 mimetype = orig_metadata['common']['tags']['mimetype']
80
81 video_codec = orig_metadata["video"]["codec"].lower()
82 audio_codec = orig_metadata["audio"]["codec"].lower()
83
84 # We don't want the "video" at the end of vp8...
85 # not sure of a nicer way to be cleaning this stuff
86 if video_codec == "vp8 video":
87 video_codec = "vp8"
88
89 return '%s; codecs="%s, %s"' % (
90 mimetype, video_codec, audio_codec)
91 else:
92 return video.VideoMediaManager.default_webm_type
93
94
95 DATA_MODEL = VideoData
96 MODELS = [VideoData]