knowledge/technology/applications/media/ffmpeg.md
2024-06-10 20:12:02 +02:00

30 KiB
Raw Blame History

website repo obj rev
https://ffmpeg.org/ https://github.com/FFmpeg/FFmpeg application 2024-06-10

ffmpeg

FFmpeg is a free and open-source software project consisting of a suite of libraries and programs for handling video, audio, and other multimedia files and streams. At its core is the command-line ffmpeg tool itself, designed for processing of video and audio files. It is widely used for format transcoding, basic editing (trimming and concatenation), video scaling, video post-production effects and standards compliance (SMPTE, ITU).

FFmpeg also includes other tools: ffplay, a simple media player and ffprobe, a command-line tool to display media information. Among included libraries are libavcodec, an audio/video codec library used by many commercial and free software products, libavformat (Lavf), an audio/video container mux and demux library, and libavfilter, a library for enhancing and editing filters through a Gstreamer-like filtergraph.

FFmpeg is part of the workflow of many other software projects, and its libraries are a core part of software media players such as VLC, and has been included in core processing for YouTube and Bilibili. Encoders and decoders for many audio and video file formats are included, making it highly useful for the transcoding of common and uncommon media files.

Example:

ffmpeg -i INPUT.mp4 OUTPUT.mkv

Usage

Usage: ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...

Options

Option Description
-i [input] Input a file
-map [input]:[stream] Map input streams
-c:[stream]:[input], -codec:[stream]:[input] [codec] Specify codec. If codec is copy stream will not be reencoded.
-t duration (input/output) When used as an input option (before -i), limit the duration of data read from the input file.
-to position (input/output) Stop writing the output or reading the input at position.
-ss position (input/output) When used as an input option (before -i), seeks in this input file to position. Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position.
-sseof position (input) Like the -ss option but relative to the "end of file". That is negative values are earlier in the file, 0 is at EOF.
-itsoffset offset (input) Set the input time offset.
offset must be a time duration specification.
The offset is added to the timestamps of the input files. Specifying a positive offset means that the corresponding streams are delayed by the time duration specified in offset.
-itsscale scale (input,per-stream) Rescale input timestamps. scale should be a floating point number.
-timestamp date (output) Set the recording timestamp in the container.
date must be a date specification.
-metadata[:metadata_specifier] key=value (output,per-metadata) Set a metadata key/value pair.
An optional metadata_specifier may be given to set metadata on streams, chapters or programs.
This option overrides metadata set with -map_metadata. It is also possible to delete metadata by using an empty value.
For example, for setting the title in the output file: ffmpeg -i in.avi -metadata title="my title" out.flv
To set the language of the first audio stream: ffmpeg -i INPUT -metadata:s:a:0 language=eng OUTPUT
-filter[:stream_specifier] filtergraph (output,per-stream) Create the filtergraph specified by filtergraph and use it to filter the stream.
-frames[:stream_specifier] framecount (output,per-stream) Stop writing to the stream after framecount frames.
-q[:stream_specifier] q (output,per-stream), -qscale[:stream_specifier] q (output,per-stream) Use fixed quality scale (VBR). The meaning of q/qscale is codec-dependent. If qscale is used without a stream_specifier then it applies only to the video stream, this is to maintain compatibility with previous behavior and as specifying the same codec specific value to 2 different codecs that is audio and video generally is not what is intended when no stream_specifier is used.
-attach filename (output) Add an attachment to the output file. This is supported by a few formats like Matroska for e.g. fonts used in rendering subtitles. Attachments are implemented as a specific type of stream, so this option will add a new stream to the file. It is then possible to use per-stream options on this stream in the usual way. Attachment streams created with this option will be created after all the other streams (i.e. those created with -mapor automatic mappings).
Note that for Matroska you also have to set the mimetype metadata tag: ffmpeg -i INPUT -attach DejaVuSans.ttf -metadata:s:2 mimetype=application/x-truetype-font out.mkv
-dump_attachment[:stream_specifier] filename (input,per-stream) Extract the matching attachment stream into a file named filename. If filename is empty, then the value of the filename metadata tag will be used.
E.g. to extract the first attachment to a file named out.ttf: ffmpeg -dump_attachment:t:0 out.ttf -i INPUT
To extract all attachments to files determined by the filename tag: ffmpeg -dump_attachment:t "" -i INPUT

Video Options

Option Description
-r[:stream_specifier] fps (input/output,per-stream) Set frame rate (Hz value, fraction or abbreviation).
-aspect[:stream_specifier] aspect (output,per-stream) Set the video display aspect ratio specified by aspect.
aspect can be a floating point number string, or a string of the form num:den, where num and den are the numerator and denominator of the aspect ratio. For example "4:3", "16:9", "1.3333", and "1.7777" are valid argument values.
If used together with -vcodec copy, it will affect the aspect ratio stored at container level, but not the aspect ratio stored in encoded frames, if it exists.
-pix_fmt[:stream_specifier] format (input/output,per-stream) Set pixel format. Use -pix_fmts to show all the supported pixel formats. If the selected pixel format can not be selected, ffmpeg will print a warning and select the best pixel format supported by the encoder. If pix_fmt is prefixed by a +, ffmpeg will exit with an error if the requested pixel format can not be selected, and automatic conversions inside filtergraphs are disabled. If pix_fmt is a single +, ffmpeg selects the same pixel format as the input (or graph output) and automatic conversions are disabled.

Audio Options

Option Description
-ar[:stream_specifier] freq (input/output,per-stream) Set the audio sampling frequency. For output streams it is set by default to the frequency of the corresponding input stream. For input streams this option only makes sense for audio grabbing devices and raw demuxers and is mapped to the corresponding demuxer options.
-ac[:stream_specifier] channels (input/output,per-stream) Set the number of audio channels. For output streams it is set by default to the number of input audio channels. For input streams this option only makes sense for audio grabbing devices and raw demuxers and is mapped to the corresponding demuxer options.

Advanced Options

Option Description
-ignore_unknown Ignore input streams with unknown type instead of failing if copying such streams is attempted.
-copy_unknown Allow input streams with unknown type to be copied instead of failing if copying such streams is attempted.
-map_chapters input_file_index (output) Copy chapters from input file with index input_file_index to the next output file. If no chapter mapping is specified, then chapters are copied from the first input file with at least one chapter. Use a negative file index to disable any chapter copying.
-b:a <bitrate> Audio Bitrate

Filters

List of all filters

Video Filters

Scale

Syntax: -vf scale=w:h
Example: Resize video to 720x480 pixels

ffmpeg -i input.mp4 -vf scale=720:480 output_scaled.mp4
Crop

Syntax: -vf crop=w:h:x:y
Example: Crop video to 640x480 pixels starting from the top-left corner

ffmpeg -i input.mp4 -vf crop=640:480:0:0 output_cropped.mp4
Rotate

Syntax: -vf rotate=angle
Example: Rotate video by 90 degrees clockwise

ffmpeg -i input.mp4 -vf rotate=90 output_rotated.mp4
Overlay

Syntax: -vf "movie=overlay.mp4 [ov]; [in][ov] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]"
Example: Overlay one video on top of another

ffmpeg -i main_video.mp4 -i overlay.mp4 -filter_complex "overlay=10:10" output_overlay.mp4
Drawtext

Syntax: -vf "drawtext=text='Your Text':x=10:y=10:fontsize=20:fontcolor=white"
Example: Add text to the video

ffmpeg -i input.mp4 -vf "drawtext=text='Hello, World!':x=10:y=10:fontsize=20:fontcolor=white" output_texted.mp4
Chroma Keying (Green Screen Removal)

Syntax: -vf "colorkey=0x00ff00:0.3:0.2"
Example: Remove green background using chroma keying

ffmpeg -i input.mp4 -vf "colorkey=0x00ff00:0.3:0.2" output_no_green_screen.mp4
Reverse Video

Syntax: -vf reverse
Example: Reverse the frames of a video

ffmpeg -i input.mp4 -vf reverse output_reversed.mp4
Blur

Syntax: -vf "boxblur=5:1"
Example: Apply a box blur to the video

ffmpeg -i input.mp4 -vf "boxblur=5:1" output_blurred.mp4

Audio Filters

Volume Adjustment:

Syntax: -af volume=vol
Example: Increase audio volume by a factor of 2

ffmpeg -i input.mp3 -af volume=2 output_louder.mp3
Equalizer

Syntax: -af "equalizer=f=1000:width_type=h:width=200:g=-10"
Example: Apply an equalizer effect to boost frequencies around 1000 Hz

ffmpeg -i input.mp3 -af "equalizer=f=1000:width_type=h:width=200:g=10" output_equalized.mp3
Aecho (Audio Echo)

Syntax: -af "aecho=0.8:0.9:1000:0.3"
Example: Add an echo effect to the audio

ffmpeg -i input.mp3 -af "aecho=0.8:0.9:1000:0.3" output_echo.mp3
Compand (Audio Compression/Expansion)

Syntax: -af "compand=attacks=0.3:decays=0.8:points=-90/-900|-70/-70|-30/-9|0/-3"
Example: Compress the audio dynamic range

ffmpeg -i input.mp3 -af "compand=attacks=0.3:decays=0.8:points=-90/-900|-70/-70|-30/-9|0/-3" output_companded.mp3
High-Pass Filter

Syntax: -af "highpass=f=100"
Example: Apply a high-pass filter to the audio to allow frequencies above 100 Hz

ffmpeg -i input.mp3 -af "highpass=f=100" output_highpass.mp3
Low-Pass Filter

Syntax: -af "lowpass=f=1000"
Example: Apply a low-pass filter to the audio to allow frequencies below 1000 Hz

ffmpeg -i input.mp3 -af "lowpass=f=1000" output_lowpass.mp3
Resample (Change Audio Sample Rate)

Syntax: -af "aresample=44100"
Example: Change the audio sample rate to 44100 Hz

ffmpeg -i input.mp3 -af "aresample=44100" output_resampled.mp3
Tremolo (Audio Modulation)

Syntax: -af "tremolo=f=5"
Example: Apply a tremolo effect to modulate the audio with a frequency of 5 Hz

ffmpeg -i input.mp3 -af "tremolo=f=5" output_tremolo.mp3
Echor

Syntax: -af "aecho=in_gain=0.8:out_gain=0.8:delays=500|1000|1500"
Example: Apply a multi-tap echo effect to the audio

ffmpeg -i input.mp3 -af "aecho=in_gain=0.8:out_gain=0.8:delays=500|1000|1500" output_echo.mp3
Speed/Pitch Change

Syntax: -af "atempo=1.5"
Example: Increase the speed and pitch of the audio by 1.5 times

ffmpeg -i input.mp3 -af "atempo=1.5" output_fast.mp3
Reverb

Syntax: -af "aecho=0.8:0.9:1000:0.3"
Example: Add a reverb effect to the audio

ffmpeg -i input.mp3 -af "aecho=0.8:0.9:1000:0.3" output_reverbed.mp3
Bass Boost

Syntax: -af "equalizer=f=80:width_type=h:width=200:g=10"
Example: Boost the bass frequencies in the audio

ffmpeg -i input.mp3 -af "equalizer=f=80:width_type=h:width=200:g=10" output_bass_boosted.mp3
Audio Noise Reduction

Syntax: -af "anlmdn=n=20"
Example: Apply audio noise reduction

ffmpeg -i input.mp3 -af "anlmdn=n=20" output_noise_reduced.mp3

ffprobe

Usage: ffprobe [options] [input]

Options

Option Description
-hide_banner Suppress printing banner.
-output_format, -of, -print_format writer_name[=writer_options] Set the output printing format.
writer_name specifies the name of the writer, and writer_options specifies the options to be passed to the writer.
For example for printing the output in JSON format, specify:
-output_format json
-select_streams stream_specifier Select only the streams specified by stream_specifier. This option affects only the options related to streams (e.g. show_streams, show_packets, etc.).
For example to show only audio streams, you can use the command:
ffprobe -show_streams -select_streams a INPUT
To show only video packets belonging to the video stream with index 1:
ffprobe -show_packets -select_streams v:1 INPUT
-show_error Show information about the error found when trying to probe the input.
The error information is printed within a section with name "ERROR".
-show_format Show information about the container format of the input multimedia stream.
All the container format information is printed within a section with name "FORMAT".
-show_streams Show information about each media stream contained in the input multimedia stream.
Each media stream information is printed within a dedicated section with name "STREAM".
-show_chapters Show information about chapters stored in the format.
Each chapter is printed within a dedicated section with name "CHAPTER".