madam.ffmpeg module

class madam.ffmpeg.FFmpegMetadataProcessor(config: Optional[Mapping[str, Any]] = None)[source]

Bases: madam.core.MetadataProcessor

Represents a metadata processor that uses FFmpeg.

__init__(config: Optional[Mapping[str, Any]] = None) → None[source]

Initializes a new FFmpegMetadataProcessor.

Parameters:config – Mapping with settings.
combine(file: IO, metadata_by_type: Mapping[str, Mapping[KT, VT_co]]) → IO[source]

Returns a byte stream whose contents represent the specified file where the specified metadata was added.

Parameters:
  • metadata (Mapping) – Mapping of the metadata format to the metadata dict
  • file (IO) – Container file
Returns:

file-like object with combined content

Return type:

IO

formats

The metadata formats which are supported.

Returns:supported metadata formats
Return type:set[str]
read(file: IO) → Mapping[str, Mapping[KT, VT_co]][source]

Reads the file and returns the metadata.

The metadata that is returned is grouped by type. The keys are specified by format.

Parameters:file (IO) – File-like object to be read
Returns:Metadata contained in the file
Return type:Mapping
Raises:UnsupportedFormatError – if the data is corrupt or its format is not supported
strip(file: IO) → IO[source]

Removes all metadata of the supported type from the specified file.

Parameters:file (IO) – file-like that should get stripped of the metadata
Returns:file-like object without metadata
Return type:IO
class madam.ffmpeg.FFmpegProcessor(config: Optional[Mapping[str, Any]] = None)[source]

Bases: madam.core.Processor

Represents a processor that uses FFmpeg to read audio and video data.

The minimum version of FFmpeg required is v3.3.

__init__(config: Optional[Mapping[str, Any]] = None) → None[source]

Initializes a new FFmpegProcessor.

Parameters:config – Mapping with settings.
Raises:EnvironmentError – if the installed version of ffprobe does not match the minimum version requirement
can_read(file: IO) → bool[source]

Returns whether the specified MIME type is supported by this processor.

Parameters:file (IO) – file-like object to be tested
Returns:whether the data format of the specified file is supported or not
Return type:bool
convert(asset: madam.core.Asset, mime_type: Union[madam.mime.MimeType, str], video: Optional[Mapping[str, Any]] = None, audio: Optional[Mapping[str, Any]] = None, subtitle: Optional[Mapping[str, Any]] = None) → madam.core.Asset[source]

Creates a new asset of the specified MIME type from the essence of the specified asset.

Additional options can be specified for video, audio, and subtitle streams. Options are passed as dictionary instances and can contain various keys for each stream type.

Options for video streams:

  • codec – Processor-specific name of the video codec as string
  • bitrate – Target bitrate in kBit/s as float number

Options for audio streams:

  • codec – Processor-specific name of the audio codec as string
  • bitrate – Target bitrate in kBit/s as float number

Options for subtitle streams:

  • codec – Processor-specific name of the subtitle format as string
Parameters:
  • asset (Asset) – Asset whose contents will be converted
  • mime_type (MimeType or str) – MIME type of the video container
  • video (dict or None) – Dictionary with options for video streams.
  • audio (dict or None) – Dictionary with options for audio streams.
  • subtitle (dict or None) – Dictionary with the options for subtitle streams.
Returns:

New asset with converted essence

Return type:

Asset

crop(asset: madam.core.Asset, x: int, y: int, width: int, height: int) → madam.core.Asset[source]

Creates a cropped video asset whose essence is cropped to the specified rectangular area.

Parameters:
  • asset (Asset) – Video asset whose contents will be cropped
  • x (int) – Horizontal offset of the cropping area from left
  • y (int) – Vertical offset of the cropping area from top
  • width (int) – Width of the cropping area
  • height (int) – Height of the cropping area
Returns:

New asset with cropped essence

Return type:

Asset

extract_frame(asset: madam.core.Asset, mime_type: Union[madam.mime.MimeType, str], seconds: float = 0) → madam.core.Asset[source]

Creates a new image asset of the specified MIME type from the essence of the specified video asset.

Parameters:
  • asset (Asset) – Video asset which will serve as the source for the frame
  • mime_type (MimeType or str) – MIME type of the destination image
  • seconds (float) – Offset of the frame in seconds
Returns:

New image asset with converted essence

Return type:

Asset

read(file: IO) → madam.core.Asset[source]

Returns an Asset object whose essence is identical to the contents of the specified file.

Parameters:file (IO) – file-like object to be read
Returns:Asset with essence
Return type:Asset
Raises:UnsupportedFormatError – if the specified data format is not supported
resize(asset: madam.core.Asset, width: int, height: int) → madam.core.Asset[source]

Creates a new image or video asset of the specified width and height from the essence of the specified image or video asset.

Width and height must be positive numbers.

Parameters:
  • asset (Asset) – Video asset that will serve as the source for the frame
  • width (int) – Width of the resized asset
  • height (int) – Height of the resized asset
Returns:

New asset with specified width and height

Return type:

Asset

rotate(asset: madam.core.Asset, angle: float, expand: bool = False) → madam.core.Asset[source]

Creates an asset whose essence is rotated by the specified angle in degrees.

Parameters:
  • asset (Asset) – Asset whose contents will be rotated
  • angle (float) – Angle in degrees, counter clockwise
  • expand (bool) – If true, changes the dimensions of the new asset so it can hold the entire rotated essence, otherwise the dimensions of the original asset will be used.
Returns:

New asset with rotated essence

Return type:

Asset

trim(asset: madam.core.Asset, from_seconds: float = 0, to_seconds: float = 0) → madam.core.Asset[source]

Creates a trimmed audio or video asset that only contains the data between from_seconds and to_seconds.

Parameters:
  • asset (Asset) – Audio or video asset, which will serve as the source
  • from_seconds (float) – Start time of the clip in seconds
  • to_seconds (float) – End time of the clip in seconds
Returns:

New asset with trimmed essence

Return type:

Asset