Skip to content

Basic API Usage for FFmpeg

Input

Creating a New Input Stream

To create a new input stream, use the ffmpeg.input function. This function is straightforward and initiates the stream from a specified file.

import ffmpeg

# Create a new input stream
ffmpeg.input("input.mp4")
%3 2a583e37e126135d -i input.mp4

Adding Input Options

To specify additional options for the input stream, such as start time and duration, you can include them directly in the ffmpeg.input function.

import ffmpeg

# Create a new input stream with specific options
ffmpeg.input("input.mp4", ss=10, t=20)  # Start at 10 seconds and last for 20 seconds
%3 1a6404e32d576a2a -t 20 -ss 10 -i input.mp4

Note

The ss option specifies the start time (in seconds), and the t option specifies the duration (in seconds) of the input stream. For more details, refer to the FFmpeg documentation.

Stream Selection

To specify a particular Audio or Video Stream, use the stream selection feature

import ffmpeg

ffmpeg.input("input.mp4").video_stream(0).output(filename="output.mp4")
%3 2a583e37e126135d -i input.mp4 3d4d52739e798844 -map 0:v:0 output.mp4 2a583e37e126135d->3d4d52739e798844 0:v:0 -> 0
import ffmpeg

ffmpeg.input("input.mp4").video.output(
    filename="output.mp4"
).compile_line()  # select video stream
'ffmpeg -i input.mp4 -map 0:v output.mp4'
ffmpeg.input("input.mp4").audio.output(
    filename="output.mp3"
).compile_line()  # select audio stream
'ffmpeg -i input.mp4 -map 0:a output.mp3'
ffmpeg.input("input.mp4").subtitle.output(
    filename="output.mp3"
).compile_line()  # select subtitle stream
'ffmpeg -i input.mp4 -map 0:s output.mp3'
ffmpeg.input("input.mp4").video_stream(0).output(
    filename="output.mp4"
).compile_line()  # select video stream with index 0
'ffmpeg -i input.mp4 -map 0:v:0 output.mp4'
ffmpeg.input("input.mp4").audio_stream(0).output(
    filename="output.mp3"
).compile_line()  # select audio stream with index 0
'ffmpeg -i input.mp4 -map 0:a:0 output.mp3'
ffmpeg.input("input.mp4").subtitle_stream(0).output(
    filename="output.mp3"
).compile_line()  # select subtitle stream with index 0
'ffmpeg -i input.mp4 -map 0:s:0 output.mp3'

Output

Creating a New Output Stream

To create a new output stream, use the ffmpeg.output function. This function is used to configure the output settings of the stream.

Note

The ss option specifies the start time (in seconds), and the t option specifies the duration (in seconds) of the input stream. For more details, refer to the FFmpeg documentation.

Output

Creating a New Output Stream

To create a new output stream, use the ffmpeg.output function. This function is used to configure the output settings of the stream.

import ffmpeg

# Create a new output stream
ffmpeg.input("input.mp4").output(filename="output.mp4")
%3 2a583e37e126135d -i input.mp4 3f7ff3f6d6e9a664 output.mp4 2a583e37e126135d->3f7ff3f6d6e9a664 0 -> 0

Specifying Output Options

You can specify various output options directly in the ffmpeg.output function, such as the start time and duration for the output file.

import ffmpeg

# Create and configure a new output stream
ffmpeg.input("input.mp4").output(
    filename="output.mp4", ss=10, t=20
)  # Output starting at 10 seconds with a duration of 20 seconds
%3 2a583e37e126135d -i input.mp4 357fb5a485f20632 -t 20 -ss 10 output.mp4 2a583e37e126135d->357fb5a485f20632 0 -> 0

You can also map multiple streams to a single output file.

import ffmpeg

# Define input streams
input1 = ffmpeg.input("input1.mp4")
input2 = ffmpeg.input("input2.mp3")

# Map multiple inputs to a single output
ffmpeg.output(input1, input2, filename="output.mp4")
%3 2e39e2d79e9f33a4 -i input1.mp4 7af599b721210e78 -map 0 -map 1 output.mp4 2e39e2d79e9f33a4->7af599b721210e78 0 -> 0 54675f3088c8e58c -i input2.mp3 54675f3088c8e58c->7af599b721210e78 1 -> 1

Alternatively:

import ffmpeg

# Chain input and output operations
(ffmpeg.input("input1.mp4").output(ffmpeg.input("input2.mp3"), filename="output.mp4"))
%3 2e39e2d79e9f33a4 -i input1.mp4 7af599b721210e78 -map 0 -map 1 output.mp4 2e39e2d79e9f33a4->7af599b721210e78 0 -> 0 54675f3088c8e58c -i input2.mp3 54675f3088c8e58c->7af599b721210e78 1 -> 1

Merging Outputs

FFmpeg allows processing multiple output files simultaneously. The Python FFmpeg wrapper supports this feature through the merge_outputs method.

import ffmpeg

# Define input streams
input1 = ffmpeg.input("input1.mp4")
input2 = ffmpeg.input("input2.mp4")

# Define output streams
output1 = input1.output(filename="output1.mp4")
output2 = input2.output(filename="output2.mp4")

# Merge the outputs into a single operation
ffmpeg.merge_outputs(output1, output2).global_args(y=True)
%3 2e39e2d79e9f33a4 -i input1.mp4 3a2f691057e7fcfc -map 0 output1.mp4 2e39e2d79e9f33a4->3a2f691057e7fcfc 0 -> 0 5ccc047fe4a85547 -i input2.mp4 4ca705b1d754ed4b -map 1 output2.mp4 5ccc047fe4a85547->4ca705b1d754ed4b 1 -> 0 37af1bb0b0c8ae01 -y 3a2f691057e7fcfc->37af1bb0b0c8ae01 out -> 0 4ca705b1d754ed4b->37af1bb0b0c8ae01 out -> 1

Codecs Support

typed-ffmpeg supports codec options for input and output nodes. It provides typehint, docstrings and autocomplete for ffmpeg codecs

import ffmpeg

input1 = ffmpeg.input("input1")
input1.output(
    filename="out.mp4",
    encoder_options=ffmpeg.codecs.encoders.libx264(
        crf=18, preset="slow", tune="flim", aq_strength=1.8, mbtree=0
    ),
    pix_fmt="yuv420p",
)
%3 2b909728b8e78ecc -i input1 40c9ca558343f4bc -pix_fmt yuv420p -preset slow -tune flim -crf 18 -aq_strength 1.8 -mbtree 0 out.mp4 2b909728b8e78ecc->40c9ca558343f4bc 0 -> 0