madam.ffmpeg module
- class madam.ffmpeg.AudioCodec[source]
Bases:
objectNamed constants for audio codec strings accepted by
FFmpegProcessor.convert().Use these instead of raw FFmpeg codec names to avoid depending on FFmpeg internals:
processor.convert(mime_type='audio/mpeg', audio={'codec': AudioCodec.MP3})
- class madam.ffmpeg.FFmpegMetadataProcessor(config: Mapping[str, Any] | None = None)[source]
Bases:
MetadataProcessorRepresents a metadata processor that uses FFmpeg.
- __init__(config: Mapping[str, Any] | None = None) None[source]
Initializes a new FFmpegMetadataProcessor.
- Parameters:
config – Mapping with settings.
- combine(file: IO, metadata: Mapping[str, Mapping]) 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
- read(file: IO) Mapping[str, Mapping][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
- class madam.ffmpeg.FFmpegProcessor(config: Mapping[str, Any] | None = None)[source]
Bases:
ProcessorRepresents a processor that uses FFmpeg to read audio and video data.
The minimum version of FFmpeg required is v3.3.
- __init__(config: Mapping[str, Any] | None = None) None[source]
Initializes a new FFmpegProcessor.
- Parameters:
config – Mapping with settings.
- Raises:
EnvironmentError – if ffprobe is not found, times out, or its version is below the minimum requirement (3.3).
- 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:
- convert(asset: Asset, mime_type: MimeType | str, video: Mapping[str, Any] | None = None, audio: Mapping[str, Any] | None = None, subtitle: Mapping[str, Any] | None = None, progress_callback: Callable[[dict[str, str]], None] | None = None) 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:
- crop(asset: Asset, x: int, y: int, width: int, height: int) Asset[source]
Creates a cropped video asset whose essence is cropped to the specified rectangular area.
- Parameters:
- Returns:
New asset with cropped essence
- Return type:
- extract_frame(asset: Asset, mime_type: MimeType | str, seconds: float = 0) Asset[source]
Creates a new image asset of the specified MIME type from the essence of the specified video asset.
- normalize_audio(asset: Asset, target_lufs: float = -23.0) Asset[source]
Creates a new asset whose audio stream is loudness-normalized to target_lufs LUFS (EBU R128).
Uses a two-pass approach with the FFmpeg
loudnormfilter. The first pass measures integrated loudness, LRA, and true peak; the second pass applies a linear gain correction using those measurements for accurate normalization without re-quantizing the signal unnecessarily.- Parameters:
- Returns:
New asset with normalized audio
- Return type:
- Raises:
UnsupportedFormatError – If the asset type is not supported
OperatorError – If loudness measurement or normalization fails
- overlay(asset: Asset, overlay_asset: Asset, x: int = 0, y: int = 0, gravity: str = 'north_west', from_seconds: float | None = None, to_seconds: float | None = None) Asset[source]
Composites overlay_asset on top of asset at the specified position.
Position can be set explicitly with x and y pixel offsets from the top-left corner, or implicitly via gravity (same nine-point vocabulary as
pad()). When both x/y and gravity are meaningful, x and y act as additional offsets relative to the gravity anchor.The overlay can be restricted to a time window with from_seconds and to_seconds. Outside the window the base video is shown unmodified.
- Parameters:
asset (Asset) – Base video asset
overlay_asset (Asset) – Image or video asset to composite on top
x (int) – Horizontal pixel offset from the left edge (or gravity anchor)
y (int) – Vertical pixel offset from the top edge (or gravity anchor)
gravity (str) – One of
north_west,north,north_east,west,center,east,south_west,south,south_eastfrom_seconds (float or None) – Start time of the overlay window in seconds;
Nonemeans the overlay is visible from the beginningto_seconds (float or None) – End time of the overlay window in seconds;
Nonemeans the overlay is visible until the end
- Returns:
New video asset with overlay composited
- Return type:
- Raises:
UnsupportedFormatError – If the base asset type is not supported
- read(file: IO) Asset[source]
Returns an
Assetobject 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:
- Raises:
UnsupportedFormatError – if the specified data format is not supported
- resize(asset: Asset, width: int, height: int) 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.
- rotate(asset: Asset, angle: float, expand: bool = False) Asset[source]
Creates an asset whose essence is rotated by the specified angle in degrees.
- Parameters:
- Returns:
New asset with rotated essence
- Return type:
- set_speed(asset: Asset, factor: float) Asset[source]
Creates a new audio or video asset whose playback speed is scaled by factor relative to the source.
A factor greater than
1.0speeds up playback (timelapse); a factor less than1.0slows it down (slow motion). The output duration equalssource_duration / factor.For video streams the
setptsfilter is used. For audio streams theatempofilter is used; becauseatempoaccepts values only in[0.5, 2.0], the filter is chained automatically for extreme factors.- Parameters:
- Returns:
New asset with adjusted playback speed
- Return type:
- Raises:
ValueError – If factor is not positive
UnsupportedFormatError – If the asset type is not supported
- property supported_mime_types: frozenset
MIME types this processor can handle (used to build the Madam index).
- thumbnail_sprite(asset: Asset, columns: int, rows: int, thumb_width: int, thumb_height: int, mime_type: MimeType | str = 'image/jpeg') Asset[source]
Extracts
columns × rowsevenly-spaced frames from asset and stitches them into a single sprite-sheet image.The returned image asset has dimensions
(columns × thumb_width) × (rows × thumb_height). Its metadata includes a'sprite'dict with the grid parameters, which can be used by the application layer to generate a WebVTT thumbnail track.- Parameters:
asset (Asset) – Source video asset
columns (int) – Number of thumbnail columns in the sprite sheet
rows (int) – Number of thumbnail rows in the sprite sheet
thumb_width (int) – Width of each thumbnail in pixels
thumb_height (int) – Height of each thumbnail in pixels
mime_type (MimeType or str) – MIME type of the output image (default
image/jpeg)
- Returns:
Image asset containing the sprite sheet
- Return type:
- Raises:
UnsupportedFormatError – If the source asset is not a video
- to_dash(asset: Asset, output: MultiFileOutput, segment_duration: float = 6, video: Mapping[str, Any] | None = None, audio: Mapping[str, Any] | None = None) None[source]
Transcodes asset to MPEG-DASH format and writes all output files to output.
The output consists of an MPD manifest and one or more MP4 segment files. Stream options can be provided via video and audio; by default the video is encoded as H.264 and audio as AAC.
- Parameters:
asset (Asset) – Source video asset
output (MultiFileOutput) – Destination for the manifest and segment files
segment_duration (float) – Target segment duration in seconds
video (dict or None) – Optional video stream options (
codec,bitrate)audio (dict or None) – Optional audio stream options (
codec,bitrate)
- Raises:
UnsupportedFormatError – If the source asset is not a video
- to_hls(asset: Asset, output: MultiFileOutput, segment_duration: float = 6, video: Mapping[str, Any] | None = None, audio: Mapping[str, Any] | None = None) None[source]
Transcodes asset to HLS (HTTP Live Streaming) format and writes all output files to output.
The output consists of an M3U8 playlist and one or more MPEG-TS segment files. Stream options can be provided via video and audio; by default the video is encoded as H.264 and audio as AAC, which are the most widely supported codecs for HLS.
- Parameters:
asset (Asset) – Source video asset
output (MultiFileOutput) – Destination for the playlist and segment files
segment_duration (float) – Target segment duration in seconds
video (dict or None) – Optional video stream options (
codec,bitrate)audio (dict or None) – Optional audio stream options (
codec,bitrate)
- Raises:
UnsupportedFormatError – If the source asset is not a video
- class madam.ffmpeg.VideoCodec[source]
Bases:
objectNamed constants for video codec strings accepted by
FFmpegProcessor.convert().Use these instead of raw FFmpeg codec names to avoid depending on FFmpeg internals:
processor.convert(mime_type='video/mp4', video={'codec': VideoCodec.H264})
- madam.ffmpeg.concatenate(assets: Iterable[Asset], mime_type: MimeType | str, video: Mapping[str, Any] | None = None, audio: Mapping[str, Any] | None = None) Asset[source]
Joins a sequence of audio or video assets end-to-end into a single asset.
Assets are concatenated in the order they appear in assets. By default the streams are copied without re-encoding (
-c copy). Provide video and/or audio stream options to force re-encoding, which is required when the source clips use different codecs.Uses the FFmpeg
concatdemuxer, which supports any format that can be read from files.- Parameters:
assets (Iterable[Asset]) – Iterable of assets to concatenate; must be non-empty
mime_type (MimeType or str) – MIME type of the output container
video (dict or None) – Optional video stream options (same keys as
FFmpegProcessor.convert())audio (dict or None) – Optional audio stream options (same keys as
FFmpegProcessor.convert())
- Returns:
New asset with concatenated essence
- Return type:
- Raises:
ValueError – If assets is empty
UnsupportedFormatError – If mime_type is not supported