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 5bca3374c6c7a1fd 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 4864fe8ad7559a26 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.

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 5bca3374c6c7a1fd input.mp4 459941b4ffc7f89e output.mp4 5bca3374c6c7a1fd->459941b4ffc7f89e * => 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 5bca3374c6c7a1fd input.mp4 5e452279bdb10573 output.mp4 5bca3374c6c7a1fd->5e452279bdb10573 * => 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 764af99a182fe7fb input1.mp4 6721ced775024801 output.mp4 764af99a182fe7fb->6721ced775024801 * => 0 ea4a39865c2d06f input2.mp3 ea4a39865c2d06f->6721ced775024801 * => 1

Alternatively:

import ffmpeg

# Chain input and output operations
(
    ffmpeg.input('input1.mp4')
    .output(ffmpeg.input("input2.mp3"), filename="output.mp4")
)
%3 764af99a182fe7fb input1.mp4 6721ced775024801 output.mp4 764af99a182fe7fb->6721ced775024801 * => 0 ea4a39865c2d06f input2.mp3 ea4a39865c2d06f->6721ced775024801 * => 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)
%3 764af99a182fe7fb input1.mp4 6304e905f0734936 output1.mp4 764af99a182fe7fb->6304e905f0734936 * => 0 3c6bf10053cc6e90 input2.mp4 6142c059d7553450 output2.mp4 3c6bf10053cc6e90->6142c059d7553450 * => 0 86635a5abc4c2b9 6304e905f0734936->86635a5abc4c2b9 * => 0 6142c059d7553450->86635a5abc4c2b9 * => 1