FFMPEG

A cross-platform solution to record, convert and stream audio and video. Download from ffmpeg.org or BtbN (Windows Builds).

Syntax
      ffmpeg [global_options] {[infile_options] -i infile}... {[outfile_options] outfile}...

Installation:

Download ffmpeg and copy it to /usr/local/bin/
On Windows just place it somewhere on the PATH.

On Unix/macOS apply permissions:
sudo chmod a+rx /usr/local/bin/ffmpeg

On macOS, right-click and Open to allow it to be run.

Global options:
   -y                Overwrite output files
   -n                Never overwrite output files
   -stats            Print progress report during encoding
   -nostdin          Ignore data from stdin / pipeline.
   -formats          Show available formats
   -codecs           Show available codecs

   -crf              High-quality encoding (Constant Rate Factor) scale is 0–51
                     The lower crf, the higher the quality.
                     Lossless = 0, default = 23, visually lossless = 18. 

   -Preset           Values for encoding speed to compression ratio. Slower = better compression.
                     {ultrafast | superfast | veryfast | faster | fast |medium | slow | slower | veryslow }
infile_options:
   -c codec          Codec name
   -codec codec      Codec name
   -c:a codec        Audio codec
   -c:v codec        Video codec

Video options:
   -vframes number  Set the number of video frames to output
   -r rate          Set frame rate (Hz value, fraction or abbreviation)
   -s size          Set frame size (WxH or abbreviation)
   -aspect aspect   Set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
   -vn              Disable video
   -c:v codec       Force video codec ('copy' to copy stream)
   -timecode hh:mm:ss[:;.]ff  set initial TimeCode value.
   -b:v             Video bitrate

Audio options:
   -an              Disable audio
   -c:a codec       Force audio codec ('copy' to copy stream)
   -b:a bitrate     Audio bitrate
   -vol volume      Change audio volume (256=normal)
   -af filter_graph  Set audio filters

Most media files consist of a container file with one or more streams of content. For example an .mp3 audio file may contain channels for the left and right channels or a .mpg video file may contain a video stream plus left and right channels, but it might also include 5+1 audio channels plus a channel for subtitles.

Do not mix input and output files – first specify all input files, then all output files. Also do not mix options which belong to different files.

All options apply ONLY to the next input or output file and are reset between files.

This page is not a complete document of all the options and switches, ffmpeg is updated several times a year, so check the official website for the latest full documentation.

Most of the time ffmpeg will automatically select the correct settings from the requested file extension without any complex configuration.

You can specify the codecs you want to use, ffmpeg -codecs will print all the codecs that your version of FFmpeg can handle, typically including at least the following:

==Video==
binkvideo        Bink video
h264             H.264 / AVC / MPEG-4 AVC/ MPEG-4 part 10 (decoders: h264 h264_vda ) (encoders: libx264 libx264rgb )
mpeg4            MPEG-4 part 2 (encoders: mpeg4 libxvid )
vp9              Google VP9
webp             WebP
wmv3             Windows Media Video 9
rawvideo         raw video

==Audio==
aac              AAC (Advanced Audio Coding) (encoders: aac libvo_aacenc )
alac             ALAC (Apple Lossless Audio Codec)
flac             FLAC (Free Lossless Audio Codec)
mp3              MP3 (MPEG audio layer 3)
vorbis           Vorbis (decoders: vorbis, libvorbis ) (encoders: vorbis, libvorbis )
wmalossless      Windows Media Audio Lossless
wmapro           Windows Media Audio 9 Professional

==Images==
bmp              BMP (Windows bitmap)
dvvideo          DV (Digital Video)
gif              GIF
png              PNG image
vc1image         Windows Media Video 9 Image v2
wmv3image        Windows Media Video 9 Image

==Text==
bintext          Binary text
dvb_subtitle     DVB subtitles
dvb_teletext     DVB teletext
dvd_subtitle     DVD subtitles

The codec to be used can be specified with the -c codec option

To specify the audio codec -codec:a codec
or abbreviate to -acodec codec or just -c:a codec

To specify the video codec -codec:v codec
or abbreviate to -vcodec codec or just -c:v codec

To copy a stream without re-encoding it, just specify the codec as 'copy'

If 'copy' is applied to both the audio and video (-c:av) then ffmpeg will convert from one container format to another without any re-encoding: ffmpeg -i input.webm -c:av copy output.mkv

The name ffmpeg is short for 'Fast Forward MPEG' files although the utility now does far more than that.

Examples

Convert file formats:

ffmpeg -i input.mp4 output.avi

ffmpeg -i input.mp3 output.ogg

Recursively convert all .FLAC files in the current directory and subdirectories to 320k .MP3:

find . -iname '*.flac' -exec bash -c 'D=$(dirname "{}"); B=$(basename "{}"); mkdir "$D/mp3/"; ffmpeg -i "{}" -ab 320k -map_metadata 0 -id3v2_version 3 -acodec libmp3lame "$D/mp3/${B%.*}.mp3"' \;

Concatenate audio files together

ffmpeg -i "concat:file1.mp3|file2.mp3" -acodec copy output.mp3

Or using a text file listing:

$ cat files.txt
file 'file1.mp3'
file 'file2.mp3'
file 'file3.mp3'

ffmpeg -f concat -safe 0 -i files.txt -c copy output.mp3

Change Codec

Remux from MKV (a container format) into MP4 (another container format), lossless copy without re-encoding.
':av' is the default so this could also be specified with just -c copy:

ffmpeg -i input.mkv -c:av copy output.mp4

Similarly remux from .webm (a container format) to .mkv (another container format):

ffmpeg -i input.webm -c:av copy output.mkv

Specify the codecs for both video (-c:v) and audio (-c:a)

ffmpeg -i input.mp4 -c:v vp9 -c:a mp3 output.mkv

Encoding Quality

Encode the audio as mp3 and copy the video stream unchanged:

ffmpeg -i input.mp4 -c:v copy -c:a mp3 output.mkv

High-quality encoding using -crf (Constant Rate Factor) and -preset:

ffmpeg -i input.mp4 -preset slower -crf 18 output.mp4

Convert a .webm file to .mp3 specifying a high bitrate (-b) audio for best quality sound.

ffmpeg -i ~/input.webm -b:a 192k ~/output.mp3

Convert a .webm file to .mp4 specifying a high bitrate (-b) audio for best quality sound.

ffmpeg -i ~/input.webm -c:v libx264 -c:a aac -strict experimental -b:a 192k output.mp4

Deinterlace

interlaced video is generated for TV/broadcasting with even lines are captured for one half frame followed by the odd lines in the next half frame. This can give a jagged appearance on a computer monitor.

Deinterlace a file using "yet another deinterlacing filter".

ffmpeg -i input.mp4 -vf yadif output.mp4

Extracting partial content

To Trim a file, add the options -ss [StartTime] -t [duration] or -to [EndTime]

Times and duration can be given as hh:mm:ss.msec or a simple number of seconds.
Trimming can be done with or without -c copy, if you leave out -c copy, ffmpeg will re-encode the output file.

Copy only the first 60 seconds of a video:

ffmpeg -i input.mp4 -t 60 -c copy output.mp4

Copy 2.5 minutes from the middle of a video without re-encoding it:

ffmpeg -ss "00:00:59.59" -i "input.mp4" -c copy -map 0 -to "00:03:30.00" "output.mp4"

Mux video and audio

To copy the video from invid0.mp4 and the audio from inaudio1.mp4 and mux together matching the duration of the -shortest input stream:

ffmpeg -i invid0.mp4 -i inaudio1.mp4 -c copy -map 0:0 -map 1:1 -shortest output.mp4

To copy the video from invideo0.mp4 and the audio from inaudio1.mp3 and mux together with the settings:
video from the start, keeping the first 40 seconds,
audio starting from 15.5 seconds and cutting after a duration of 30 seconds, so the last 10 seconds of the output will be silent:

ffmpeg -ss 00:00:00 -t 40 -i "invid0.mp4" -ss 0:00:15.5 -t 30 -i "inaudio1.mp3" -map 0:v:0 -map 1:a:0 -y out.mp4

Mux video and a static image

To multiplex together the image pic.jpg and the audio inaudio.mp3 into a single output.mp4 file:

ffmpeg -loop 1 -i pic.jpg -i inaudio.mp3 -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest output.mp4

Audio/video sync

Delay video by 0.2 seconds:

ffmpeg -i inaudio1.mp3 -itsoffset 0.2 -i invid0.mp4 -map 1:v -map 0:a -vcodec copy -acodec copy out.mp4

Delay audio by 0.2 seconds:

ffmpeg -i invid0.mp4 -itsoffset 0.2 -i inaudio1.mp3 -map 0:v -map 1:a -vcodec copy -acodec copy out.mp4

Rotate a video

Transpose parameter values:
0 = 90CounterClockwise + Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise + Vertical Flip

Rotate 90 clockwise:

ffmpeg -i input.mp4 -vf "transpose=1" output.mp4

Rotate 180 degrees clockwise:

ffmpeg -i input.mp4 -vf "transpose=2,transpose=2" output.mp4

Change Aspect ratio

Changing the resolution will always requires re-encoding.

Change the Sample Aspect Ratio to be half that of the input file, this will create a significantly smaller (but lower quality) file:

ffmpeg -i "input.mp4" -vf "scale=trunc(iw/4)*2:trunc(ih/4)*2" ~/output.mp4
In this example, the size is divided by twice the value required and then multiplied by two to ensure the pixel size is a multiple of two, which is required for some codecs.

Change the Sample Aspect Ratio (SAR) of a video from 720p to 540 (4:3):

ffmpeg -i "input.mp4" -vf scale=720:540 -c:v [Video_Codec] "output.mp4"

Change the Display Aspect Ratio (DAR) of a video from 720p to 540 (4:3) [x] this sets a metadata flag at the container level:

ffmpeg -i "input.mp4" -aspect 720:540 -c copy "output.mp4"

Batch conversions

Batch convert a folder of .mp4 files to .mp3 (Windows CMD, see stackoverflow for bash and PowerShell versions.)

for %i in (*.mp4) do ffmpeg -i "%i" "%~ni.mp3"

Merge together 2 or more files of the same type/dimensions/codec, create a file with the filenames (one file per line) prefixing each line with 'file' to indicate that they are file inputs:

input.txt
file 'ex1.aiff'
file 'ex2.aiff'
Then run: ffmpeg -f concat -i input.txt -c copy output.aiff

Alternatively, on one line:
ffmpeg -i "concat:vid1.mp4|vid2.mp4" -c copy output.mp4

Batch convert a set of .jpg images into a video with a frame rate of 1 per 2 seconds:

ffmpeg -framerate 1/2 -pattern_type glob -i '*.jpg' output.mp4

Television and the media are everywhere and they are taking over so powerfully. They don't shut up for a second. So you are unable to think. It is very difficult to think independently when you are surrounded by all that noise. What I most aspire to is to be alone. Not lonely, but alone. To stop all this noise. That is what I do when I go to Umbria. There is no television there, no telephone” ~ Terry Gilliam

Related

ffmprovisr - helpful information about how to perform a wide variety of tasks using FFmpeg.
ffmpeg - Full documentation.
youtube-dl - Download Video.
A simple PowerShell GUI front end for ffmpeg (compression only).


 
Copyright © 1999-2024 SS64.com
Some rights reserved