0b52c53f326c13623b9d98227333dd4c2c9f4ed8
[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_data: 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_640" not in self.get_media_entry.media_files \
72 and "mimetype" in orig_metadata \
73 and "tags" in orig_metadata \
74 and "audio-codec" in orig_metadata["tags"] \
75 and "video-codec" in orig_metadata["tags"]:
76 if orig_metadata['mimetype'] == 'application/ogg':
77 # stupid ambiguous .ogg extension
78 mimetype = "video/ogg"
79 else:
80 mimetype = orig_metadata['mimetype']
81
82 video_codec = orig_metadata["tags"]["video-codec"].lower()
83 audio_codec = orig_metadata["tags"]["audio-codec"].lower()
84
85 # We don't want the "video" at the end of vp8...
86 # not sure of a nicer way to be cleaning this stuff
87 if video_codec == "vp8 video":
88 video_codec = "vp8"
89
90 return '%s; codecs="%s, %s"' % (
91 mimetype, video_codec, audio_codec)
92 else:
93 return video.VideoMediaManager.default_webm_type
94
95
96 DATA_MODEL = VideoData
97 MODELS = [VideoData]