FFMPEG

This page shows some examples on how to use FFmpeg command line tool which is a complete, cross-platform solution to record, convert, capture, decode, encode, modify, combine, stream audio and video.


CONTENT


CODEC / LIBRARIES

The following formats use these codecs/libraries.

AUDIO

flac

-c:a flac

mp3

-c:a libmp3lame

mp3

-c:a aac

ogg

-c:a libvorbis

opus

-c:a libopus

IMAGE

avif

-c:v libaom-av1

jxl

-c:v libjxl

png

-c:v libpng

webp

-c:v libwebp

VIDEO

mkv AV1 (Slower)

-c:v libaom-av1

mkv AV1 (Faster)

--c:v librav1e

mp4 H264

-c:v libx264

webm VP9

-c:a libvpx-vp9


CONVERT AUDIO, VIDEO, IMAGE

Simple file convertion with the command lines below. The rule is the same with audio, video and images. Specifiy the codecs are optional for a simple format convertion unless you need more control on the output.

Syntax: ffmpeg -i input (c:v codec-video) (c:a codec-audio) output

Convert an audio file

ffmpeg -i input.wav output.opus

Convert an image file

ffmpeg -i input.bmp output.webp

Convert a video file

ffmpeg -i input.avi output.webm

EXAMPLES

Audio .wav to .flac

ffmpeg -i input.wav output.flac

Audio .flac to .mp3

ffmpeg -i input.flac output.mp3

Audio .mp3 to .opus

ffmpeg -i input.mp3 output.opus

Image .bmp to .webp

ffmpeg -i input.bmp output.webp

Image .png to .jxl

ffmpeg -i input.png output.jxl

Animation .gif to .avif

ffmpeg -i input.gif output.avif

Animation .gif to .apng +loop

ffmpeg -i input.gif -plays 0 output.apng

Animation .gif to .webp +loop +lossless

ffmpeg -i input.gif -lossless 1 -loop 0 output.webp

Video .avi to .mkv (AV1 video + Opus audio)

ffmpeg -i input.avi -c:v libaom-av1 -c:a libopus output.mkv

Video .avi to .mp4 (H264 video + AAC audio)

ffmpeg -i input.avi -c:v libx264 -c:a aac output.mp4

Video .avi to .webm (VP9 video + OPUS audio)

ffmpeg -i input.avi -c:v libvpx-vp9 -c:a libopus output.webm


CROPDETECT AND CROP FILTERS (Remove black borders)

Simple file convertion with the command lines below. The rule is the same with audio, video and images. Specifiy the codecs are optional for a simple format convertion unless you need more control on the output.

1. Get crop parameters

cropdetect can be used to provide the parameters for the crop filter.

In this example the first 90 seconds is skipped and 10 frames are processed:

ffmpeg -ss 90 -i input.mp4 -vframes 10 -vf cropdetect -f null -

...
[Parsed_cropdetect_0 @ 0x220cdc0] x1:0 x2:1279 y1:0 y2:719 w:1280 h:720 x:0 y:0 pts:215 t:0.215000 crop=1280:720:0:0
[Parsed_cropdetect_0 @ 0x220cdc0] x1:0 x2:1279 y1:0 y2:719 w:1280 h:720 x:0 y:0 pts:257 t:0.257000 crop=1280:720:0:0
[Parsed_cropdetect_0 @ 0x220cdc0] x1:0 x2:1279 y1:0 y2:719 w:1280 h:720 x:0 y:0 pts:299 t:0.299000 crop=1280:720:0:0

At the end of each line, you can see it says crop=1280:720:0:0. So according to cropdetect we can use crop=1280:720:0:0.

2. Preview with ffplay

ffplay -vf crop=1280:720:0:0 input.mp4

3. Re-encode using the crop filter

ffmpeg -i input.mp4 -vf crop=1280:720:0:0 -c:a copy output.mp4


source: llogan @ superuser.com


EXTRACT OR TRIM A VIDEO PART

The following command lines are used to trim video in FFmpeg, the stream copy (-c copy) enables to trim video without re-encoding and keeps original quality for the output video.

Method 1: Using start time -ss and end time -to

ffmpeg -i input.mp4 -ss 00:00:00 -to 00:10:00 -c copy output.mp4

Method 2 : Using start time -ss and duration time -t

ffmpeg -i input.mp4 -ss 00 -t 10 -c copy output.mp4

INFO

-i input.mp4 = the path of input file.
-ss 00:00:00 = the start of the section that you want to trim.
-to 00:10:00 = the end of the section that you want to trim.
-c copy = this option means trimming video via stream copy, which is fast and will not re-encode video.
output.mp4 = the path for the output file.
-t 10 = the duration of the section to trim, rather than the end time.

NOTE: You can use -t durationTime instead of -to endTime.


JOIN MULTIPLE AUDIO OR VIDEO FILES TOGETHER

1. Create the list file

Create a list.txt file in the same directory as the media files you want to join, list.txt contains the list of media input files you will concatenate, in this format:

list.txt contains the lines below:
file 'media1.mp4'
file 'media2.mp4'
file 'media3.mp4'

2. Run the command

ffmpeg -f concat -safe 0 -i list.txt output.mp4

INFO

-f concat = this forces ffmpeg to use concat mode for the output file.
-safe 0 = this is needed if absolute paths are provided as the input.
-i list.txt = the path to the list of input files, if your current working directory is where this file is you can supply the file name only.
output.mp4 = the path for the output file.


WINDOWS .BAT SCRIPTS
SCRIPT: FFMPEG CONVERTER .BAT

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

@echo off
echo Type the file extension format (mp3, opus, mp4, avi, webm, etc.) you want to convert your files, then press ENTER.
set /p extension=
echo.
echo Converting your files to %extension%...
echo.
if not exist ".\CONVERTED\" mkdir ".\CONVERTED"
for %%a in ("*.*") do ffmpeg.exe -i "%%a" ".\CONVERTED\%%~na.%extension%"
pause

SCRIPT: CONVERT MULTI FILES TO FLAC .BAT

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

for %%a in ("*.*") do ffmpeg.exe -i "%%a" "%%~na.flac"

SCRIPT: CONVERT MULTI FILES TO OPUS .BAT

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

for %%a in ("*.*") do ffmpeg.exe -i "%%a" "%%~na.opus"

SCRIPT: CONVERT MULTI FILES TO WAV .BAT

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

for %%a in ("*.*") do ffmpeg.exe -i "%%a" "%%~na.wav"

SCRIPT: CONVERT MULTI FILES TO WEBM .BAT

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

for %%a in ("*.*") do ffmpeg.exe -i "%%a" "%%~na.webm"

SCRIPT: CONVERT MULTI FILES TO WEBP LOSSLESS LOOP .BAT

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

for %%a in ("*.*") do ffmpeg.exe -i "%%a" -lossless 1 -loop 0 "%%~na.webp"

SCRIPT: CONVERT MULTI FILES TO AV1 OPUS .BAT (.mkv or .webm)

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

@echo off
set extension=mkv
echo.
echo Converting your files to %extension%...
echo.
if not exist ".\CONVERTED\" mkdir ".\CONVERTED"
for %%a in ("*.*") do ffmpeg.exe -i "%%a" -c:v libaom-av1 -c:a libopus ".\CONVERTED\%%~na.%extension%"
pause

SCRIPT: CONVERT MULTI FILES TO AV1 (rav1e) .BAT

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

@echo off
set extension=mkv
echo.
echo Converting your files to %extension%...
echo.
if not exist ".\CONVERTED\" mkdir ".\CONVERTED"
for %%a in ("*.*") do ffmpeg.exe -i "%%a" -c:v librav1e -an ".\CONVERTED\%%~na.%extension%"
pause

SCRIPT: CONVERT MULTI FILES TO AVIF .BAT

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

@echo off
set extension=avif
echo.
echo Converting your files to %extension%...
echo.
if not exist ".\CONVERTED\" mkdir ".\CONVERTED"
for %%a in ("*.*") do ffmpeg.exe -i "%%a" -an ".\CONVERTED\%%~na.%extension%"
pause

SCRIPT: CONVERT MULTI FILES TO WEBM VP9 .BAT

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

@echo off
set extension=webm
echo.
echo Converting your files to %extension%...
echo.
if not exist ".\CONVERTED\" mkdir ".\CONVERTED" for %%a in ("*.*") do ffmpeg.exe -i "%%a" -c:v libvpx-vp9 -an ".\CONVERTED\%%~na.%extension%"
pause

SCRIPT: CONVERT MULTI FILES TO WEBM VP9 OPUS .BAT (.webm)

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

@echo off
set extension=webm
echo.
echo Converting your files to %extension%...
echo.
if not exist ".\CONVERTED\" mkdir ".\CONVERTED" for %%a in ("*.*") do ffmpeg.exe -i "%%a" -c:v libvpx-vp9 -c:a libopus ".\CONVERTED\%%~na.%extension%"
pause

SCRIPT: CONVERT MULTI FILES TO Y4M .BAT

This script will convert all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

@echo off
set extension=y4m
echo.
echo Converting your files to %extension%...
echo.
if not exist ".\CONVERTED\" mkdir ".\CONVERTED" for %%a in ("*.*") do ffmpeg.exe -i "%%a" -an ".\CONVERTED\%%~na.%extension%"
pause

SCRIPT: FFMPEG COPYRIGHTER .BAT

This script will add copyright metadata in all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

@echo off
echo Type the file extension format (mp3, mp4, avi, webm, etc.) of the files you want to tag from, then press ENTER.
set /p extension=
echo Type copyright text.
set /p copyright=
if not exist ".\COPYRIGHTED\" mkdir ".\COPYRIGHTED"
for %%a in ("*.%extension%") do ffmpeg.exe -i "%%a" -metadata copyright="%copyright%" -c copy ".\COPYRIGHTED\%%~na.%extension%"
pause

SCRIPT: FFMPEG TAGGER .BAT

This script will add tags metadata in all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

@echo off
echo Type the file extension format (mp3, mp4, avi, webm, etc.) of the files you want to tag from, then press ENTER.
set /p extension=
echo.
echo Now type Artist tag.
set /p artist=
echo.
echo Now type Album tag.
set /p album=
echo.
echo Now type Year tag.
set /p year=
echo.
echo Now type Title tag. ( For multi files let it blank )
set /p title=
echo.
echo Now type Track number tag. ( For multi files let it blank )
set /p track=
echo.
echo Now type Genre tag. ( For multi files let it blank )
set /p genre=
echo.
echo Now type Copyright tag.
set /p copyright=
echo.
echo Now type Comment tag.
set /p comment=
echo.
if not exist ".\TAGGED\" mkdir ".\TAGGED"
for %%a in ("*.%extension%") do ffmpeg.exe -i "%%a" -metadata artist="%artist%" -metadata album="%album%" -metadata year="%year%" -metadata title="%title%" -metadata track="%track%" -metadata genre="%genre%" -metadata copyright="%copyright%" -metadata comment="%comment%" -c copy ".\TAGGED\%%~na.%extension%"
pause

SCRIPT: FFMPEG TAG REMOVER .BAT

This script will remove the tags and chapters in all media files of the folder where this script is located.

Copy the code below in a .bat file, then run this script in the same folder of your media files, ffmpeg.exe must be there too!

@echo off
echo Type the file extension format (mp3, mp4, avi, webm, etc.) of the files you want to remove tags from, then press ENTER.
set /p extension=
if not exist ".\TAGREMOVED\" mkdir ".\TAGREMOVED"
for %%a in ("*.%extension%") do ffmpeg.exe -i "%%a" -map_metadata -1 -map_chapters -1 -c copy ".\TAGREMOVED\%%~na.%extension%"
pause