Video file manipulation

The mg.utils.video module contains utility classes using ffmpeg to:

  • parse video files,
  • generate snapshot still images (thumbnails),
  • transcode video.

The main entry point is the VideoHandler class.

class mg.utils.video.VideoHandler[source]

A Video handler class. This object can help manipulate a video file.

__init__(video_filename)[source]

Creates a new handler.

Parameters:
  • video_filename (str) – File name containing the source video.
streams[source]

list of StreamInfo object containing definition of all the streams contained in the file.

container[source]

Container type name (eg “avi” or “mov”).

bit_rate[source]

File’s global bitrate (float), in bits/sec

duration[source]

File run length (float), in seconds

get_streams(type)[source]

Retrieve the streams of a given type.

Parameters:
  • type (str) – Type of stream to get (eg “video” or “audio”)
Returns:

a list of StreamInfo objects.

has_video()[source]

Returns whether this file contains at least one video stream.

Returns:a boolean
snapshot(output_file, image_size=None, time_offset=None, rot_count=0)[source]

This function calls a video thumbnailer tool to create a thumbnail of the video as a still image file. The only supported output format is JPEG.

Parameters:
  • output_file (str) – File name of the image file to create. This file name must contain a valid JPEG file extension.
  • image_size (tuple or None) – A (width, height) couple of integer sizes. Leave to None to get an image of original size.
  • time_offset (int or float or None) – A number of seconds to take the snapshot at, from the start of stream. If offset is beyond end of file or None, set it at 1/4th of the file.
Raises :

RuntimeError if thumbnail generation failed.

Raises :

ValueError if the input file has no video stream

transcode(output_file, container_name, vs_info, as_info, rot_count=0, add_flags='')[source]

This function calls a video transcoding tool to create another version of the same video.

Parameters:
  • output_file (str) – File name of the image file to create.
  • container_name (str) – Name of the container format for output_file.
  • vs_info (VideoStreamInfo) – Video stream format
  • as_info (AudioStreamInfo) – Audio stream format
Raises :

RuntimeError if thumbnail generation failed.

Raises :

ValueError if the input file has no video stream

class mg.utils.video.StreamInfo[source]

A stream information base class.

type

Type string (eg “video” or “audio”)

format

A format short-name

format_map

Static member, dict.

A dictionary mapping format names to codec to use for encoding.

dest_args()[source]

Creates an argument list of ffmpeg command line for this stream.

Returns:a list of string
class mg.utils.video.AudioStreamInfo[source]

An Audio stream information class. Inherits StreamInfo.

__init__(format, samplerate, channels, precision)[source]
samplerate

Sample rate (float), in samples/sec

channels

Count of simultaneous channels in this stream.

precision

integer, in bits per sample.

class mg.utils.video.VideoStreamInfo[source]

A Video stream information class. Inherits StreamInfo.

__init__(format, size, fps, bitrate=None, dar=None, **kwargs)[source]
size

Image size in a (width, height) couple

fps

Frames per second (float)

dar

Display aspect ratio (float). width/height ratio in visual size.

bitrate

Bitrate of the video stream. in bit/sec.