Professional Documents
Culture Documents
1 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
ffmpeg recipes
by Jeremy Osborne, 2014.02.16 21:46:06
The following are ffmpeg commands that I found useful for video
editing (mainly for recorded screencasts and Youtube videos). I've
given a rough description of each command, followed by my
explanation of the commandline options, followed by the command
itself. I assume you will modify the commands to suit your needs.
All commands based on version Mar 22 2013 08:56:38 of ffmpeg,
which is a newer version compared to a lot of the help material I found
for ffmpeg on the web. Most of these commands are tested on
Windows versions of ffmpeg, despite that I often use Unix multi-line
escapes to break up long commands.
8/29/2014 8:08 PM
ffmpeg recipes
2 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
#
-i 2.mp4 -i 3.mp4 -i 4.mp4 -i 5.mp4 -i 6.mp4
#
The input videos. I have 5 videos in
this command.
#
Input order is important when ffmpeg
needs to reference
#
the video by index.
#
2.mp4 will be referenced by index 0
because it is first.
#
5.mp4, being the fourth video, is
referenced by index 3.
#
ffmpeg uses 0-based indexing (as most
nerd things do for
#
historical reasoning).
#
-filter_complex "[0:0] [0:1] [1:0] [1:1]
[2:0] [2:1] [3:0] [3:1] [4:0] [4:1]
concat=n=5:v=1:a=1 [v] [a]"
#
Perform a complex filtering of input to
output.
#
Since we know 2.mp4 maps to index of
zero, I could describe the
#
above as:
#
#
[0:0] [0:1]
#
Takes the video stream of 2.mp4,
denoted as [0:0], and the audio
#
stream of 2.mp4, denoted as [0:1],
and includes them in the concat
#
process. These values were true for
my videos, although it is
#
possible you will be concatenating
different streams than me.
#
#
And with that mapping, we are stating
"Take the audio and
#
video streams of all of my input videos
and..."
8/29/2014 8:08 PM
ffmpeg recipes
3 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
8/29/2014 8:08 PM
ffmpeg recipes
4 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
#
-i capture.avi
#
The input video.
#
-filter:v crop=1280:960:0:32
#
Apply a filter to the video streams.
#
Take a 1280 pixel wide by 960 pixel tall
#
video window from the input, and offset
this
#
1280x960 rectangle 0 pixels from the
left side
#
(aka. the cropping begins from the
original left side)
#
and 32 pixels down from the top.
#
-c:v libx264
#
Use the libx264 video codec (h.264
encoder).
#
-preset slow
#
From the ffmpeg x264 encoding wiki:
#
A preset is a collection of options that
will provide
#
a certain encoding speed to compression
ratio.
#
-crf 18
#
From the ffmpeg x264 encoding wiki:
#
crf stands for constant rate factor. The
range of the
#
quantizer scale is 0-51; where 0 is
lossless, 23 is
#
default, and 51 is worst possible.
#
Consider 18 to be visually lossless.
#
-b:a 128k
#
Set audio bitrate to 128k.
#
-c:a libmp3lame
#
Use the lame library for MP3 encoding.
#
-threads 0
#
Let ffmpeg decide how many threads to use
#
for encoding (if it even chooses to use
8/29/2014 8:08 PM
ffmpeg recipes
5 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
8/29/2014 8:08 PM
ffmpeg recipes
6 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
#
-i capture.avi
#
input file
#
-filter:v crop=1280:960:0:32
#
Take the video stream and crop it within
a
#
box of dimensions 1280x960, and position
#
the box 0 pixels from the left and 32
pixels
#
from the top.
#
-sws_flags lanczos+full_chroma_inp
#
using lanczos with full chroma input flag
#
-s 2560x1920
#
Size to scale to
#
(which is double the cropped, scaled
#
video size of 1280x960).
#
-c:v libx264 -preset slow -crf 18
#
Use the x264 encoder and some settings
#
that I can't quite explain.
#
-b:a 128k
#
Set audio bitrate to 128k.
#
-c:a libmp3lame
#
Use the lame library for MP3 encoding.
#
-threads 0
#
ffmpeg decide how many threads to use
#
for encoding (if more than one).
#
-pix_fmt yuv420p
#
Pixel formatting for the video.
#
upload.mkv
#
The output file.
ffmpeg -i capture.avi -filter:v
crop=1280:960:0:32 -sws_flags
lanczos+full_chroma_inp -s 2560x1920 -c:v libx264
-preset slow -crf 18 -b:a 128k -c:a libmp3lame
-threads 0 -pix_fmt yuv420p upload.mkv
8/29/2014 8:08 PM
ffmpeg recipes
7 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
ffmpeg recipes
8 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
# Windows Powershell
# For every MP4 file in the directory
# convert to h264
# copy the audio
# color scheme in yuv420p
# and output file with the same basename
# except switch container (and file extension) to
Matroska.
for %F IN (*.mp4) DO (ffmpeg -i %F -c:v libx264
-preset slow -crf 23 -c:a copy -threads 0
-pix_fmt yuv420p %~nF.mkv)
8/29/2014 8:08 PM
ffmpeg recipes
9 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
-i capture_video.avi
The first input file.
-i remixed_audio.mp3
The second input file.
-map 0
Map all of the streams from the first
input into the output video, in order.
-map 1
Map all of the streams from the second
input into the output video, in order.
-c copy
Do not reencode any streams, use the same
encoding.
output.mkv
Output into the Matroska container format
(detected by the file extension).
8/29/2014 8:08 PM
ffmpeg recipes
10 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
#
-i capture.avi
#
The input video.
#
-sws_flags lanczos+full_chroma_inp
#
Use the lanczos filter with full chroma
input
#
during the resize process.
#
-s 2560x1840
#
Width x Height in pixels to scale
#
the output video to.
#
-c:v libx264
#
Use the libx264 video codec (h.264
encoder).
#
-preset slow
#
From the ffmpeg x264 encoding wiki:
#
A preset is a collection of options that
will provide
#
a certain encoding speed to compression
ratio.
#
-crf 18
#
From the ffmpeg x264 encoding wiki:
#
crf stands for constant rate factor. The
range of the
#
quantizer scale is 0-51; where 0 is
lossless, 23 is
#
default, and 51 is worst possible.
#
Consider 18 to be visually lossless.
#
-b:a 128k
#
Set audio bitrate to 128k.
#
-c:a libmp3lame
#
Use the lame library for MP3 encoding.
#
-threads 0
#
Let ffmpeg decide how many threads to use
#
for encoding (if it even chooses to use
more than one).
#
-pix_fmt yuv420p
#
Pixel format / color scheme.
8/29/2014 8:08 PM
ffmpeg recipes
11 of 11
http://www.jeremyosborne.com/pages/ffmpeg_recipes.html
References
The following links were the ones that helped me the most with
figuring out how to use ffmpeg.
http://blog.superuser.com/2012/02/24/ffmpeg-the-ultimatevideo-and-audio-manipulation-tool/
http://ingomar.wesp.name/2011/04/dosbox-gameplay-videocapture.html
http://ffmpeg.org/trac/ffmpeg/wiki/EncodeforYouTube
http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide
8/29/2014 8:08 PM