The mg.media.video application

Requires The mg.core.element application.

This application is responsible for handling video files in a gallery. It provides a cache of recompressed video files in order to provide galleries to different devices or plugins.

Django models

mg.media.video Application models

digraph video {
element [shape = "box", label="Element\nmg.core.element"];
video [shape = "box", label="Video"];
video_format [shape = "box", label="VideoFormat"];
resized_video [shape = "box", label="ResizedVideo"];

video -> element [label = "inheritance", style="dotted"];
resized_video -> video_format [headlabel = "1", taillabel = "*"];
resized_video -> video [headlabel = "1", taillabel = "*"];
}

The Video model

class mg.media.video.models.Video(mg.core.element.models.Element)

A video is the authoritative model for storing a video. All other classes only store caches of the data stored herein.

A video is a file of a given container format (avi, ogg, mov, etc.), with a video stream and an audio stream, each with specific attributes (codec, bitrate, etc.). Only one stream of each type is supported for now.

file

The file containing the original stream.

width

Width of the original video stream image. In pixels.

height

Height of the original video stream image. In pixels.

ratio

width/height ratio, to help find pictures by format.

dar

Display aspect ratio. The width/height ratio the image should have when displayed to user. This is present as a medatada field in most container streams.

largest

max((width, height)), here for query efficiency.

cfmt

Format of the original container. This is the container around the streams, line mov, avi, ts, etc.

afmt

Format of the original audio stream.

vfmt

Format of the original video stream.

bottom_side

The side of the original video stream that is the bottom of the scene. This defines the orientation of the original picture. This works exactly link photo’s Bottom side. This is most useful for videos authored with compact cameras where users don’t think rotation is not given for free :).

key_time

A time (in seconds) from the start of the stream where the image is interesting. This is where the snapshot will be taken for thumbnail generation.

spool_filename()

Retrieves the absolute path to saved file in spool.

Returns:An absolute path in file system

The VideoFormat model

class mg.media.video.models.VideoFormat

This defines a format for resized video streams. This actually defines the codecs to use, the bitrates, and the container.

width

Maximal width of the video stream (pixels).

height

Maximal height of the video stream (pixels).

afmt

Audio format. This is a format name (like mp3, aac or pcm16le). Codec to use for this format will be looked-up through mg.utils.video.StreamInfo.format_map.

vfmt

Video format.

cfmt

Container format.

fps

Video frame per second to encode (float). This may be a non-integer value.

vbr

Video bitrate (in bit/second). Only used for lossy formats.

arate

Audio sampling rate (in Hz).

achannels

Count of audio channels in audio stream. Most values will be 1 (mono) or 2 (stereo).

add_flags

Additional flags to pass to the compression utility. By default, this is ffmpeg flags. See mg.utils.video.VideoHandler.

wh

A 2-tuple of (width, height) matching video size.

preset(orig_size)

Yields a dict with four keys:

container:
A container format string.
vs_info:
A mg.utils.video.VideoStreamInfo object containing all constraints about the video stream.
as_info:
A mg.utils.video.AudioStreamInfo object containing all constraints about the ausio stream.
add_flags:
Additional flags to pass in.

The ResizedVideo model

class mg.media.video.models.ResizedVideo
video

Pointer to a Video: the original video.

size

Pointer to a VideoFormat: The format the video got recompressed to.

file

A file where the resized video is stored.

bottom_side

The side of the original video used to generate the resized video. If the bottom_side value does not match the one of the original video, updaters will recompute the resized video. See Bottom side.

filename_gen()

Creates a name matching the referenced element and size. This can be used as base path for saving the thumbnail file.

Returns:A relative path string

Updaters

CreateResizedVideo

Rehash the resized videos, if missing or in wrong orientation.

Templatetags

The mg_video templatetag library

In order to use these templatetags, you must use the following code in your template:

{% load mg_video %}

video_resized_get

This tag retrieves a ResizedVideo fitting the user’s preferences. Example:

{% video_resized_get video_element as resized %}
<!-- ugly <embed/object/script /> code using {{resized.file.url}},
     {{resized.size.width}}, {{resized.size.height}}, etc -->

Views

mg.media.video provides some views. They are not directly usable to display the video streams, though.

mg.media.video.views.prefer_size(request)

A view used by the video_prefer_size_links template tag.

URLs

mg.media.video.urls exports mg.media.video.views.prefer_size() to the prefer_size/ url. This should not be used directly, but mg.media.video.urls should be included in your root urls.py if you use the template tag.