Skip to content

Instantly share code, notes, and snippets.

@pjobson
Last active April 24, 2024 05:00
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pjobson/312c490bd55a6c84ae48510d61bf8072 to your computer and use it in GitHub Desktop.
Save pjobson/312c490bd55a6c84ae48510d61bf8072 to your computer and use it in GitHub Desktop.
FFMPEG Notes

Some Recipies for ffmpeg Usage

I screw around with ffmpeg a lot, here are some recipies which I frequently use.

Cropping

You need 4 variables:

  • W - Width of Output Video

  • H - Height of Output Video

  • X - Offset From Left

  • Y - Offset From Top

    ffmpeg -i in.mp4 -vf "crop=W:H:X:Y" out.mp4
    
    +-----------------------+---------------------------+
    |                       |                           |
    |                      (Y)                          |
    |                       |                           |
    |                       V                           |
    +----------(X)--------->+-------(W)-------+         |
    |                       |                 |         |
    |                      (H)                |         |
    |                       |                 |         |
    |                       +-----------------+         |
    |                                                   |
    |                                                   |
    |                                                   |
    +---------------------------------------------------+
    

SUBTITLES

Convert

ffmpeg -i blah.vtt blah.srt
for i in *.vtt ; do ffmpeg -i "$i" "$i.srt" ; done

AUDIO

Static & No Audio WAV

Create 1 hour of static noise wav file.

ffmpeg -ar 48000 -t 3600 -f s16le -acodec pcm_s16le -ac 2  -i /dev/random -t 3600 -f wav -bitexact -acodec pcm_s16le -ar 22050 -ac 2 static.wav

Create 1 hour of silent wav file.

ffmpeg -ar 48000 -t 3600 -f s16le -acodec pcm_s16le -ac 2  -i /dev/zero -t 3600 -f wav -bitexact -acodec pcm_s16le -ar 22050 -ac 2 silent.wav

Change Volume

Halve Volume (50% decrease)

ffmpeg -i input.wav -filter:a "volume=0.5" output.wav

Double Volume (50% increase)

ffmpeg -i input.wav -filter:a "volume=1.5" output.wav

Loudness Normalization

ffmpeg -i input.wav -filter:a loudnorm output.wav

... to MP3

ffmpeg -i <input>.wav -codec:a libmp3lame -b:a 320k output.mp3
ffmpeg -i <input>.wav -codec:a libmp3lame -q:a 0 output.mp3
Average kbit/s Bitrate range kbit/s ffmpeg option
320 320 CBR -b:a 320k
245 220-260 -q:a 0
225 190-250 -q:a 1
190 170-210 -q:a 2
175 150-195 -q:a 3
165 140-185 -q:a 4
130 120-150 -q:a 5
115 100-130 -q:a 6
100 80-120 -q:a 7
85 70-105 -q:a 8
65 45-85 -q:a 9

... to FLAC

ffmpeg -i <input>.<ext> -c:a flac <output>.flac

... to WAV

Raw Audio Types

ffmpeg -i <input>.<ext> -acodec pcm_s32le output.wav

There are various encoders for the WAV format, you can list them by passing the -formats switch to ffmpeg. More on this below.

ffmpeg -formats | grep PCM

Create Forced Stereo Track

ffmpeg -i mono.ac3 -ac 2 stereo.ac3

To 6-Channel

This is good for converting 7.1(+) channel audio to 5.1 channel audio.

https://www.reddit.com/r/PleX/comments/9htmso/anyone_know_a_way_to_convert_mkvs_with_71_audio/e6gjkf8/

ffmpeg -i 7.1.channel.ac3 -ac 6 5.1.channel.ac3

Change Speed

Decress speed by half.

ffmpeg -i input.wav -filter:a "atempo=0.5" -vn output.wav

Increase speed by half.

ffmpeg -i input.wav -filter:a "atempo=1.5" -vn output.wav

Double speed.

ffmpeg -i input.wav -filter:a "atempo=2.0" -vn output.wav

VIDEO

... to MP4

Use this for AVI files which are old and pretty outdated, if you run Plex they'll tend to use up a lot of CPU time to stream.

ffmpeg -i input.avi -c:v libx264 -crf 19 -preset slow -c:a aac -b:a 192k -ac 2 -strict experimental out.mp4

... to MKV

It is a bit easier to do stuff with MKV files using MKVToolNix – Matroska tools for Linux/Unix and Windows, but you can accomplish many things with ffmpeg.

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

Remove Audio from an MKV

ffmpeg -i <input>.mkv -c copy -an <output>-nosound.mkv

Convert to Grayscale

ffmpeg -i <input>.mkv -vf hue=s=0 <output>-grayscale.mkv

Loop an Image into a Video

Convert an image into a 720p video which is 40m 1.04 seconds.

ffmpeg -loop 1 -i image.png -c:v libx264 -t 00:40:01.04 -pix_fmt yuv420p -vf scale=1280x720 out.mp4

Convert a Vertical Movie to Hoizontal with Black Bars

This is good for videos which were shot vertically on a phone 1080w x 1920h.

This is a two-step proces which I use, it could probably be made into one step.

ffmpeg -i vertical.1080x1920.mkv -vf scale=-1:1080 vertical.608x1080.mkv
ffmpeg -i vertical.608x1080.mkv -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2" horizontal.1920x1080.mkv

Crop a Video

https://video.stackexchange.com/questions/4563/how-can-i-crop-a-video-with-ffmpeg?newreg=00479d410f7f478b9a50cdca73820942#answer-4571

Extract JPGs from a Video

Extract 20 Frames per Second

ffmpeg -i example.mkv -r 20 img.%10d.jpg

Extract 1 Frame per Second

ffmpeg -i example.mkv -r 1 img.%8d.jpg

Convert a Series of Images to a Video

ffmpeg -framerate 24 -i img%03d.png output.mp4

Rotate Video and Convert to MKV

ffmpeg -i <input>.<ext> -c:v libx264 -preset ultrafast -crf 0 -vf "transpose=1" <output>.mkv

Rotations:

Num Rotation
0 90 Counter Clockwise and Vertical Flip (default)
1 90 Clockwise
2 90 Counter Clockwise
3 90 Clockwise and Vertical Flip

Add Subtitles to MKV

ffmpeg -i <input>.mkv -i <subtitle>.srt -map 0 -map 1 -c copy <output>.mkv

Many subtitles for movies may be downloaded from SubScene.

Concatenate Several Videos to One

Easy with cat

cat file01.mp4 file02.mp4 | ffmpeg -i - out.mp4

Files of the Same Type

For files of like type, useful if you have files like: file001.mp4, file002.mp4, file003.mp4, etc.

ffmpeg -f concat                                                        \
       -safe 0                                                          \
       -i <(find . -name 'file*.mp4' -printf "file '$PWD/%f'\n" | sort) \
       -c:v libx264                                                     \
       -preset ultrafast                                                \
       -crf 22                                                          \
       -c:a copy                                                        \
       -ac 2 output.mp4

Files of the Various File Types

If you use youtube-dl to batch download a bunch of files on youtube, you can wind up with webm, mp4, mkv, and other formats.

For files of unlike type, useful if you have files like: file001.mp4, file002.mkv, file003.avi, etc.

Convert files to mp2 format first which will get you file001.mts, file002.mts, file003.mts, etc.

for f in ./file*.mp4; do ffmpeg  -n  -i "${f}" -q 0 "${f%.*}.mts"; done

Then concatenate them:

ffmpeg -f concat                                                        \
       -safe 0                                                          \
       -i <(find . -name 'file*.mts' -printf "file '$PWD/%p'\n" | sort) \
       -c:v libx264                                                     \
       -preset ultrafast                                                \
       -crf 22                                                          \
       -c:a copy                                                        \
       -ac 2 output.mp4

INFORMATIONAL

Suppress Headers and Info

The banner which ffmpeg uses is a bit annoying to see every time it is run and not very useful.

This for example is what is displayed with my custom build, none of which is useful at all for basic usage.

ffmpeg version 3.4.4-0ubuntu0.18.04.1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
  configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Don't display the header information.

ffmpeg -hide_banner

Don't display the encoding/decoding messages.

ffmpeg -nostats -loglevel 0

Media Info (Probe)

ffprobe gathers information from multimedia streams and prints it in human- and machine-readable fashion.

ffprobe example.mkv

Installed Capabilities

Formats

FFmpeg Formats Documentation

This shows what formats the installed ffmpeg supports for Muxing and Demuxing.

ffmpeg -formats

Output Example (Support, File Extension, File Description)

File formats:
 D. = Demuxing supported
 .E = Muxing supported
 --
 D  3dostr             3DO STR
  E 3g2                3GP2 (3GPP2 file format)
  E 3gp                3GP (3GPP file format)
 D  4xm                4X Technologies
  E a64                a64 - video for Commodore 64
 D  aa                 Audible AA format files
 D  aac                raw ADTS AAC (Advanced Audio Coding)
 DE ac3                raw AC-3
 D  acm                Interplay ACM
 D  act                ACT Voice file format
 D  adf                Artworx Data Format
 D  adp                ADP
 D  ads                Sony PS2 ADS
  E adts               ADTS AAC (Advanced Audio Coding)
 DE adx                CRI ADX
 D  aea                MD STUDIO audio
...etc...
 DE wav                 WAV / WAVE (Waveform Audio)
 D  wc3movie            Wing Commander III movie
  E webm                WebM
  E webm_chunk          WebM Chunk Muxer
 DE webm_dash_manifest  WebM DASH Manifest
  E webp                WebP
 D  webp_pipe           piped webp sequence
 DE webvtt              WebVTT subtitle
 D  wsaud               Westwood Studios audio
 D  wsd                 Wideband Single-bit Data (WSD)
 D  wsvqa               Westwood Studios VQA
 DE wtv                 Windows Television (WTV)
 DE wv                  raw WavPack
 D  wve                 Psion 3 audio
 D  xa                  Maxis XA
 D  xbin                eXtended BINary text (XBIN)
 D  xmv                 Microsoft XMV
 D  xvag                Sony PS3 XVAG
 D  xwma                Microsoft xWMA
 D  yop                 Psygnosis YOP
 DE yuv4mpegpipe        YUV4MPEG pipe
 DE adx                 CRI ADX
 D  aea                 MD STUDIO audio

Devices

FFmpeg Devices Documentation

This shows the available devices the installed ffmpeg supports for Muxing and Demuxing.

ffmpeg -devices

Output example for OS X MacBrew installation.

Devices:
 D. = Demuxing supported
 .E = Muxing supported
 --
 D  avfoundation    AVFoundation input device
  E caca            caca (color ASCII art) output device
 D  lavfi           Libavfilter virtual input device
 D  qtkit           QTKit input device
  E sdl             SDL output device

Output example for Linux Mint installation.

Devices:
 D. = Demuxing supported
 .E = Muxing supported
 --
 DE alsa            ALSA audio output
 D  dv1394          DV1394 A/V grab
 DE fbdev           Linux framebuffer
 D  lavfi           Libavfilter virtual input device
 DE oss             OSS (Open Sound System) playback
  E v4l2            Video4Linux2 output device
 D  video4linux2,v4l2 Video4Linux2 device grab
 D  x11grab         X11 screen capture, using XCB

Codecs

FFmpeg Codecs Documentation

This shows what codecs are installed.

ffmpeg -codecs

Output Example

Codecs:
 D..... = Decoding supported
 .E.... = Encoding supported
 ..V... = Video codec
 ..A... = Audio codec
 ..S... = Subtitle codec
 ...I.. = Intra frame-only codec
 ....L. = Lossy compression
 .....S = Lossless compression
 -------
 D.VI.S 012v                 Uncompressed 4:2:2 10-bit
 D.V.L. 4xm                  4X Movie
 D.VI.S 8bps                 QuickTime 8BPS video
 .EVIL. a64_multi            Multicolor charset for Commodore 64 (encoders: a64multi )
 .EVIL. a64_multi5           Multicolor charset for Commodore 64, extended with 5th color (colram) (encoders: a64multi5 )
 D.V..S aasc                 Autodesk RLE
 D.VIL. aic                  Apple Intermediate Codec
 DEVI.S alias_pix            Alias/Wavefront PIX image
 DEVIL. amv                  AMV Video
 ...etc...

More

https://github.com/chgeuer/mybookmarks/blob/master/ffmpeg%20recipes.md

@kiquenet
Copy link

how-to convert mp3, m4a or aac file to wav A-Law, 8000Hz, 64kbps, mono?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment