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 APV (Advanced Professional Video)

-c:v libopenapv

mkv AV1 libaom‑av1 (reference encoder, highest quality, slower)

-c:v libaom-av1

mkv AV1 (Rust‑based encoder, good quality, fast)

--c:v librav1e

mkv AV1 SVT‑AV1 (Intel‑backed, very fast at high quality)

--c:v libsvtav1

mkv FFV1 (lossless)

-c:v ffv1

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 +quality 50 (0-100 higher value is better, default is 75)

ffmpeg -i input.gif -q.:v 50 output.webp

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

Video .avi to .webm lossless (VP9 video, no audio)

ffmpeg -i input.avi -c:v libvpx-vp9 -lossless 1 -an output.webm

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

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

Good WebM Video + Audio .mkv (SVTAV1 + Opus)

ffmpeg -i input -c:v libsvtav1 -c:a Libopus output.webm

True lossless Video + Audio .mkv (FFV1 + FLAC)

ffmpeg -i input -c:v ffv1 -c:a flac output.mkv

Convert a PNG image to a loss‑less AVIF file

ffmpeg -i input.png -c:v libaom-av1 -lossless 1 output.avif

  • -i input.png : Specifies the source PNG file.
  • -c:v libaom-av1 : Selects the AV1 encoder (libaom‑av1) that ffmpeg uses for AVIF output.
  • -lossless 1 : Turns on loss‑less mode for the encoder (equivalent to -crf 0).
  • output.avif : The resulting loss‑less AVIF file.

🛈 Actually only libaom-av1 encoder support lossless mode.

Convert a PNG image to a loss‑less WebP file

ffmpeg -i input.png -c:v libwebp -lossless 1 -preset picture -compression_level 6 output.webp

  • -i input.png : Specifies the source PNG file.
  • -c:v libwebp :Use the WebP video codec (the same encoder ffmpeg ships for still images).
  • -lossless 1 : Forces true loss‑less encoding (equivalent to -qscale 100).
  • -preset picture : Optimizes for still‑image quality (other presets are photo, drawing, icon, text).
  • -compression_level 6 : Controls how hard the encoder works (0‑6). Higher values give smaller files but take longer; 6 is the maximum for loss‑less.
  • output.webp : Resulting loss‑less WebP file.

🛈 If you want the absolute smallest loss‑less file, you can bump -compression_level to 6 (the max). For most everyday use, the default 6 already gives a good size‑vs‑speed trade‑off.


Test & Comparison

The original source file lossless.mkv, is a 1920x1080, 60 sec, 60 fps, 2.4 GB lossless .mkv video, encoded with FFV1 for video stream and FLAC audio stream.

All options and encoding parameters are default, as ffmpeg manage them.

CONTAINER VIDEO AUDIO OUTPUT SIZE TIME ELAPSED COMMAND .mkv ffv1 (lossless) flac (lossless) 48000Hz 2.4 GB 0:00:18.62 ffmpeg -i lossless.mkv -c:v ffv1 -c:a flac lossless2.mkv .mkv h264 (high) (libx264) vorbis (libvorbis) 48000Hz 27.6 MB 0:00:23.85 ffmpeg -i lossless.mkv video.mkv .mp4 h264 (high) (libx264) aac (LC) (mp4a) 48000Hz 27.8 MB 0:00:22.08 ffmpeg -i lossless.mkv video.mp4 .webm vp9 (libvpx-vp9) opus (libopus) 48000Hz 34.6 MB 0:05:41.99 ffmpeg -i lossless.mkv video.webm .webm av1 (libsvtav1) Opus (libopus) 48000Hz 30.0 MB 0:00:23.12 ffmpeg -i lossless.mkv -c:v libsvtav1 -c:a libopus video.webm

We like and support WebM. By defaut, FFmpeg encodes WebM video with VP9 (libvpx-vp9) and audio with OPUS (libopus), which is probably the best lossy audio codec actually (2025), but in our test, VP9 is slow compared to AV1 (libsvtav1), so to have a good compromise between compression and time, we strongly suggest to use AV1 (libsvtav1) instead of VP9 (libvpx-vp9) to make WebM.

Why WebM is good

WebM is a modern, open-source video format designed for efficient web streaming and playback, offering several key advantages. It is royalty-free, meaning it can be used by anyone without licensing fees, which makes it a cost-effective choice for developers and content creators. WebM is particularly effective for online video delivery because it provides high video quality while achieving smaller file sizes compared to formats like MP4, often reducing file size by 20-30% without sacrificing visual quality. This efficient compression is due to its use of advanced codecs like VP9 and AV1 for video, and Opus for audio, which deliver excellent quality at lower bitrates.

The format is well-suited for websites and online platforms, where faster load times and optimized user experience are crucial. Its compatibility with modern web browsers, including recent versions of Safari, Chrome, Firefox, and Opera, ensures seamless playback for users with up-to-date devices. WebM is also ideal for live streaming and video conferencing due to its low latency, especially when using the Opus audio codec. Furthermore, WebM is widely supported by major video platforms like YouTube and Vimeo, and it integrates well with HTML5, making it a preferred choice for web-based video content.

REMINDER:
A simple command to make a good webm video:
ffmpeg -i input.video.mkv -c:v libsvtav1 -c:a libopus output.video.webm


Insert subtitles SRT file in a video

To embed an SRT file into a video using FFmpeg, you have several options depending on whether you want to include subtitles as a separate, selectable track (soft subtitles) or burn them directly into the video (hard subtitles).

Method 1: Embed subtitles as a separate track (Recommended for MP4)

To add the SRT file as a soft subtitle track (so viewers can enable/disable them), use this command for MP4 output:

ffmpeg -i input.mp4 -f srt -i subtitles.srt -map 0:0 -map 0:1 -map 1:0 -c:v copy -c:a copy -c:s mov_text output.mp4

NOTE: MP4 only supports mov_text subtitles. If your SRT contains special formatting, ensure compatibility.

Method 2: Embed subtitles in MKV (Supports native SRT)

For MKV files, which support SRT subtitles natively, use:

ffmpeg -i video.mkv -f srt -i subtitles.srt -map 0:0 -map 0:1 -map 1:0 -c:v copy -c:a copy -c:s srt output.mkv

This creates a new MKV file with the subtitles as a separate selectable track, preserving the original video and audio quality.

Method 3: Burn subtitles into the video (Hardcoded subtitles)

To permanently burn subtitles into the video frames (visible to all viewers):

ffmpeg -i input.mp4 -vf "subtitles=subtitles.srt" output.mp4

Customize subtitle appearance:

You can style the burned-in subtitles:

ffmpeg -i input.mp4 -vf "subtitles=subtitles.srt:force_style='FontName=Arial, FontSize=24, PrimaryColour=&H00FFFF&, Alignment=2'" output.mp4

This sets:

Summary

Method Command Use Case Soft subtitles (MP4)-c:s mov_text Soft subtitles (MKV)-c:s srt Hardcoded subtitles-vf "subtitles=..."

Choose the method based on your needs: flexibility (soft subtitles) or guaranteed visibility (hardcoded).


ADD A COVER IMAGE TO AN AUDIO FILE

The following command will add an album art cover.jpg to an mp3 file input.mp3 without re-encoding by copying the streams.

ffmpeg -i input.mp3 -i cover.jpg -c copy -map 0:a -map 1:0 output.mp3


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 AUDIO FROM VIDEO

To keep only the audio stream from a video file using FFmpeg, you can use the -map option to select the audio stream and discard all others. The following command will extract the audio from the input file and save it as a new audio file:

ffmpeg -i input.mp4 -map 0:a -c:a copy output.aac

This command maps only the audio stream (0:a) from the first input file (0) and copies the audio codec without re-encoding (-c:a copy). The output file will contain only the audio track.

If the input file contains multiple audio tracks and you want to keep only a specific one, such as the first audio track, you can specify it by index:

ffmpeg -i input.mkv -map 0:a:0 -c:a copy output.aac

This selects the first audio stream (0:a:0).


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

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


Scripts

Below are some useful scripts for you to use, for mass file manipulation with FFmepg, for Linux and Windoes. Modifiy them to your needs!

Linux Bash Scripts

Convert multi audio files to FLAC
        
            #!/bin/bash

            # Run me in the directory where the files to convert are located.

            # Directory containing the media files
            directory="" # update your directory
            output_format="flac"

            # Change to the specified directory
            cd "$directory"

            # Loop through all files in the directory
            for file in *; do
                # Skip directories
                if [ -d "$file" ]; then
                    continue
                fi

                # Extract the base name and extension
                base_name=$(basename "$file")
                extension="${file##*.}"

                # Construct the output file name
                output_file="${base_name%.*}.${output_format}"

                # Use ffmpeg to convert the file
                echo "Converting file: $file"
                ffmpeg -loglevel quiet -i "$file" "$output_file"

                echo "Converted $file to $output_file"
            done
        
    
Convert multi video files to webm SVTAV1 + Opus and remove meta data
      
          #!/bin/bash

          # Run me in the directory where the files to convert are located.

          # Directory containing the media files
          directory="" # update your directory
          output_format="webm"

          # Change to the specified directory
          cd "$directory"

          # Options for encoding
          options="-map_metadata -1 -c:v libsvtav1 -c:a libopus"


          # Loop through all files in the directory
          for file in *; do
              # Skip directories
              if [ -d "$file" ]; then
                  continue
              fi

              # Extract the base name and extension
              base_name=$(basename "$file")
              extension="${file##*.}"

              # Construct the output file name
              output_file="${base_name%.*}.${output_format}"

              # Use ffmpeg to convert the file
              # -loglevel quiet
              echo "Converting file: $file"
              ffmpeg -i "$file" $options "$output_file"

              echo "Converted $file to $output_file"
          done
      
  
Converting Multi Files Based on Extension

Script to convert all files of a specific extension in a directory to a different format.

This script loops through all .mp4 files in the current directory, converting them to .mkv format. The basename command extracts the filename without its extension, allowing the script to create a new filename with the desired output format. Adjust the input_format and output_format variables as needed for different file types.


#!/bin/bash

# Specify the input and output formats
input_format="mp4"
output_format="mkv"

# Loop through all files with the input format
for file in *.$input_format; do
    # Extract the base name without the extension
    base_name=$(basename "$file" .$input_format)

    # Construct the output file name
    output_file="${base_name}.${output_format}"

    # Use ffmpeg to convert the file
    echo "Converting file: $file"
    ffmpeg -loglevel quiet -i "$file" "$output_file"

    echo "Converted $file to $output_file"
done

Converting All Files in a Specific Directory

Script to convert all files with various formats within a specific directory.

This script changes to the specified directory and converts each file within it to the .avi format. It checks each item to ensure it is a file and not a directory before proceeding with the conversion. The script can be adjusted to exclude certain file types or to apply different conversion settings based on the file type.


#!/bin/bash

# Directory containing the media files
directory="/path/to/your/media/files" # update your directory
output_format="avi"

# Change to the specified directory
cd "$directory"

# Loop through all files in the directory
for file in *; do
    # Skip directories
    if [ -d "$file" ]; then
        continue
    fi

    # Extract the base name and extension
    base_name=$(basename "$file")
    extension="${file##*.}"

    # Construct the output file name
    output_file="${base_name%.*}.${output_format}"

    # Use ffmpeg to convert the file
    echo "Converting file: $file"
    ffmpeg -loglevel quiet -i "$file" "$output_file"

    echo "Converted $file to $output_file"
done

Remove meta data of multi files
        
            #!/usr/bin/env bash
            # Supprimer les métadonnées de tous les fichiers du dossier avec ffmpeg
            # Usage : ./ffmpeg-meta-remover.sh [chemin_du_dossier]

            DIR="${1:-.}"                     # dossier à traiter (défaut = .)

            for f in "$DIR"/*; do
                # ne garder que les fichiers réguliers
                [ -f "$f" ] || continue

                # nom de sortie : même nom + "_clean" avant l'extension
                ext="${f##*.}"
                base="${f%.*}"
                out="${base}_clean.${ext}"

                echo "Traitement : $f → $out"
                ffmpeg -hide_banner -loglevel error -i "$f" -map_metadata -1 -c copy "$out"
            done

            echo "Terminé."

        
    

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