I screw around with ffmpeg a lot, here are some recipies which I frequently use.
- Converting Subtitles
- AUDIO
- VIDEO
- ... to MP4
- ... to MKV
- Remove Audio from an MKV
- Convert to Grayscale
- Loop an Image into a Video
- Convert a Vertical Movie to Hoizontal with Black Bars
- Cropping
- Extract JPGs from a Video
- Convert a Series of Images to a Video
- Rotate Video and Convert to MKV
- Add Subtitles to MKV
- Concatenate/Join Several Videos to One
- Make A Sample
- Stacking & Grids
- INFORMATIONAL
ffmpeg -i blah.vtt blah.srt
for i in *.vtt ; do ffmpeg -i "$i" "$i.srt" ; done
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
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
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 |
ffmpeg -i <input>.<ext> -c:a flac <output>.flac
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
ffmpeg -i mono.ac3 -ac 2 stereo.ac3
This is good for converting 7.1(+) channel audio to 5.1 channel audio.
ffmpeg -i 7.1.channel.ac3 -ac 6 5.1.channel.ac3
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
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
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
ffmpeg -i <input>.mkv -c copy -an <output>-nosound.mkv
ffmpeg -i <input>.mkv -vf hue=s=0 <output>-grayscale.mkv
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
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
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) | | | | | | | +-----------------+ | | | | | | | +---------------------------------------------------+
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
ffmpeg -framerate 24 -i img%03d.png output.mp4
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 |
ffmpeg -i <input>.mkv -i <subtitle>.srt -map 0 -map 1 -c copy <output>.mkv
Many subtitles for movies may be downloaded from SubScene.
cat file01.mp4 file02.mp4 | ffmpeg -i - out.mp4
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
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
This creates a 30-second sample of input.mkv
starting at 00:00:00 ending
at 00:00:30.
ffmpeg -ss 00:00:00 -i input.mkv \
-t 00:00:30 \
-map 0:v? -map 0:a? \
-map 0:s? -map 0:d? \
-c copy output.mkv
You can stack images or videos can be done with -filter_complex
which I think is a light scripting language for ffmpeg.
Stack two videos, like:
┌─────────────────┬─────────────────┐
│ │ │
│ 0:v │ 1:v │
│ │ │
└─────────────────┴─────────────────┘
Where 0:v
and 1:v
are references to the inputs.
ffmpeg -i video0.mp4 -i video1.mp4 \
-filter_complex "[0:v][1:v]hstack[video]" \
-map "[video]" output.mp4
Grid four videos, like:
════════════════════════════════════════════════╗
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ║
┌─────────────────┬─────────────────┐ ┃ ║
│ │ │ ┃ ║
│ 0:v │ 1:v │ ┣━┫ top ║
│ │ │ ┃ ║
├─────────────────┼─────────────────┤ ┃ ╠═╣ video
│ │ │ ┃ ║
│ 2:v │ 3:v │ ┣━┫ btm ║
│ │ │ ┃ ║
└─────────────────┴─────────────────┘ ┃ ║
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ║
════════════════════════════════════════════════╝
ffmpeg -i video0.mp4 -i video1.mp4 -i video2.mp4 -i video3.mp4 \
-filter_complex "[0:v][1:v]hstack[top];[2:v][3:v]hstack[btm];[top][btm]vstack[video]" \
-map "[video]" output.mp4
After the -map
switch you can transcode to whatever you want with something like -c:v libx264
if you want.
Explanation:
# Reference [0:v] [1:v] [2:v] [3:v]
-i video0.mp4 -i video1.mp4 -i video2.mp4 -i video3.mp4
# Using [0:v] and [1:v], horizontal stack them storing in [top] variable
[0:v][1:v]hstack[top];
# Using [2:v] and [3:v], horizontal stack them storing in [btm] variable
[2:v][3:v]hstack[btm];
#Using the [top] and [btm, vertial stack them and store as [video] variable
[top][btm]vstack[video]
# Select the output stream, as the [video] variable.
-map "[video]"
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
ffprobe
gathers information from multimedia streams and prints it in human- and machine-readable fashion.
ffprobe example.mkv
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
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
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...
how-to convert mp3, m4a or aac file to wav A-Law, 8000Hz, 64kbps, mono?