Difference between revisions of "FFmpeg"

From ArchWiki
Jump to: navigation, search
m (Two-pass MPEG-4 (very high-quality): fixed a link)
(Screen cast: link to virtual device page instead of ALSA; use official terminology "devices" instead of "formats")
(48 intermediate revisions by 13 users not shown)
Line 1: Line 1:
[[Category:Audio/Video (English)]]
+
[[Category:Audio/Video]]
{{i18n|FFmpeg}}
+
From the project [http://www.ffmpeg.org/ home page]:
 +
:''FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video. It includes libavcodec - the leading audio/video codec library.''
  
{{Article summary start}}
+
== Package installation ==
{{Article summary text|This article attempts to walk users through the installation, usage and configuration of FFmpeg.}}
+
{{Article summary end}}
+
  
[http://www.ffmpeg.org/ FFmpeg] is command-line driven collection of tools which enables the decoding, encoding, conversion and playback of [http://ffmpeg.org/general.html#Supported-File-Formats-and-Codecs most audio and video streams].
+
Various flavors and related projects can be [[pacman|installed]] from the [[official repositories]] and the [[AUR]]:
  
== Package installation ==
+
* {{Pkg|ffmpeg}} official package
{{Pkg|FFmpeg}} is part of the [[Official_repositories#.5Bextra.5D|official Arch Linux [extra] repository]].
+
  
{{bc|# pacman -S ffmpeg}}
+
Notable variants:
 +
 
 +
* {{AUR|ffmpeg-git}} – development version
 +
* {{AUR|ffmpeg-full}} – built with as much optional features enabled as possible
 +
 
 +
Forks:
 +
 
 +
* {{AUR|ffmbc}} – targeted for broadcasting usage
 +
<!-- Why is highlighting the differance in binary name useful? Needs to be replaced with a meaningful description of libav. -->
 +
* {{AUR|libav-git}} – the binary it provides is called {{ic|avconv}} instead of {{ic|ffmpeg}}
  
 
== Encoding examples ==
 
== Encoding examples ==
 +
 +
=== Screen cast  ===
 +
 +
FFmpeg includes the [http://www.ffmpeg.org/ffmpeg-devices.html#x11grab x11grab] and [http://www.ffmpeg.org/ffmpeg-devices.html#alsa-1 ALSA] virtual devices that enable capturing the entire user display and audio output.
 +
 +
First create {{ic|test.mkv}} with lossless encoding:
 +
 +
$ ffmpeg -f x11grab -i $DISPLAY -f alsa -i default -c:v ffvhuff -c:a flac test.mkv
 +
 +
then we process it into a smaller [[Wikipedia:WebM]] file:
 +
 +
$ ffmpeg -i test.mkv  -c:v libvpx -c:a libvorbis  -b:v 2000k -q:a 3 test.webm
 +
 +
=== Recording webcam ===
 +
 +
FFmpeg supports grabbing input from Video4Linux2 devices.  The following command will record a video from the webcam, assuming that the webcam is correctly recognized under {{ic|/dev/video0}}:
 +
 +
$ ffmpeg -f v4l2 -s 640x480 -i /dev/video0 ''output''.mpg
 +
 +
The above produces a silent video. It is also possible to include audio sources from a microphone.  The following command will include a stream from the default [[Advanced Linux Sound Architecture|ALSA]] recording device into the video:
 +
 +
$ ffmpeg -f alsa -i default -f v4l2 -s 640x480 -i /dev/video0 ''output''.mpg
 +
 +
To use [[PulseAudio]] with an ALSA backend:
 +
 +
$ ffmpeg -f alsa -i pulse -f v4l2 -s 640x480 -i /dev/video0 ''output''.mpg
 +
 +
For a higher quality capture, try encoding the output using higher quality codecs:
 +
 +
$ ffmpeg -f alsa -i default -f v4l2 -s 640x480 -i /dev/video0 -acodec flac \
 +
-vcodec libx264 ''output''.mkv
  
 
=== VOB to any container ===
 
=== VOB to any container ===
Concatenate the desired [[Wikipedia:VOB|VOB]] files into a single VOB file:
 
$ cat VTS_01_1.VOB VTS_01_2.VOB VTS_01_3.VOB VTS_01_4.VOB > Transformers.3.Dark.of.the.Moon.VOB
 
Or concatenate and then pipe the output VOB to FFmpeg:
 
$ cat VTS_01_1.VOB VTS_01_2.VOB VTS_01_3.VOB VTS_01_4.VOB > Transformers.3.Dark.of.the.Moon.VOB | ffmpeg -i ...
 
  
=== x264 Lossless ===
+
Concatenate the desired [[Wikipedia:VOB]] files into a single stream and mux them to MPEG-2:
 +
$ cat f0.VOB f1.VOB f2.VOB | ffmpeg -i - out.mp2
 +
 
 +
=== x264 lossless ===
 +
 
 
The ''ultrafast'' preset will provide the fastest encoding and is useful for quick capturing (such as screencasting):
 
The ''ultrafast'' preset will provide the fastest encoding and is useful for quick capturing (such as screencasting):
{{bc|ffmpeg -i input -vcodec libx264 -preset ultrafast -qp 0 -acodec copy output.mkv}}
+
$ ffmpeg -i input -vcodec libx264 -preset ultrafast -qp 0 -acodec copy ''output.mkv''
 
On the opposite end of the preset spectrum is ''veryslow'' and will encode slower than ''ultrafast'' but provide a smaller output file size:
 
On the opposite end of the preset spectrum is ''veryslow'' and will encode slower than ''ultrafast'' but provide a smaller output file size:
{{bc|ffmpeg -i input -vcodec libx264 -preset veryslow -qp 0 -acodec copy output.mkv}}
+
$ ffmpeg -i input -vcodec libx264 -preset veryslow -qp 0 -acodec copy ''output.mkv''
 
Both examples will provide the same quality output.
 
Both examples will provide the same quality output.
  
 
=== Single-pass MPEG-2 (near lossless) ===
 
=== Single-pass MPEG-2 (near lossless) ===
Allow FFmpeg to automatically set DVD standardized parameters. Encode to DVD [[Wikipedia:MPEG-2|MPEG-2]] at a frame rate of 30 frames/second:
 
  
ffmpeg -i 13.Assassins.VOB -target ntsc-dvd -sameq 13.Assassins.mpg
+
Allow FFmpeg to automatically set DVD standardized parameters. Encode to DVD [[Wikipedia:MPEG-2|MPEG-2]] at ~30 FPS:
  
Encode to DVD MPEG-2 at a frame rate of 24 frames/second:
+
$ ffmpeg -i ''video''.VOB -target ntsc-dvd -q:a 0 -q:v 0 ''output''.mpg
  
ffmpeg -i 13.Assassins.VOB -target film-dvd -sameq 13.Assassins.mpg
+
Encode to DVD MPEG-2 at ~24 FPS:
  
=== Single-pass x264 (very high-quality) ===
+
$ ffmpeg -i ''video''.VOB -target film-dvd -q:a 0 -q:v 0 ''output''.mpg
{{ic|threads}}={{ic|0}} '''=''' automatically detect and select the appropriate number of threads:
+
{{bc|1=ffmpeg -i Transformers.3.Dark.of.the.Moon.VOB -acodec libmp3lame -ab 256k -vcodec libx264 -preset veryslow -crf 15 -threads 0 -x264opts frameref=15:fast_pskip=0 Transformers.3.Dark.of.the.Moon.mkv}}
+
{{ic|tune}} option should be set to [http://forum.doom9.org/showthread.php?t=149394 match the type and content of the of media being encoded]:
+
{{bc|1=ffmpeg -i 13.Assassins.VOB -acodec libmp3lame -ab 256k -vcodec libx264 -preset veryslow -tune film -crf 15 -threads 0 -x264opts frameref=15:fast_pskip=0 13.Assassins.mkv}}
+
  
* [http://lame.sourceforge.net/ libmp3lame] is generally recommended over [http://www.audiocoding.com/faac.html libfaac] encoding at all bitrates.
+
=== x264: constant rate factor ===
 +
 
 +
Used when you want a specific quality output. General usage is to use the highest {{ic|-crf}} value that still provides an acceptable quality. Lower values are higher quality; 0 is lossless, 18 is visually lossless, and 23 is the default value. A sane range is between 18 and 28. Use the slowest {{ic|-preset}} you have patience for. See the [https://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide x264 Encoding Guide] for more information.
 +
$ ffmpeg -i ''video'' -vcodec libx264 -preset slow -crf 22 -acodec libmp3lame -aq 4 ''output''.mkv
 +
{{ic|-tune}} option can be used to [http://forum.doom9.org/showthread.php?t=149394 match the type and content of the of media being encoded].
 +
 
 +
=== YouTube ===
 +
 
 +
FFmpeg is very useful to encode videos and strip their size before you upload them on YouTube. The following single line of code takes an input file and outputs a mkv container.
 +
 
 +
$ ffmpeg -i ''video'' -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a copy ''output''.mkv
 +
 
 +
For more information see the [https://bbs.archlinux.org/viewtopic.php?pid=1200667#p1200667 forums]. You can also create a custom alias {{ic|ytconvert}} which takes the name of the input file as first argument and the name of the .mkv container as second argument. To do so add the following to your {{ic|~/.bashrc}}:
 +
 
 +
{{bc|<nowiki>
 +
youtubeConvert(){
 +
        ffmpeg -i $1 -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a copy $2.mkv
 +
}
 +
alias ytconvert=youtubeConvert
 +
</nowiki>}}
 +
See also [https://bbs.archlinux.org/viewtopic.php?pid=1200542#p1200542 Arch Linux forum thread].
  
 
=== Two-pass x264 (very high-quality) ===
 
=== Two-pass x264 (very high-quality) ===
 +
 
Audio deactivated as only video statistics are recorded during the first of multiple pass runs:  
 
Audio deactivated as only video statistics are recorded during the first of multiple pass runs:  
{{bc|1=ffmpeg -i Transformers.3.Dark.of.the.Moon.VOB -an -vcodec libx264 -pass 1 -preset veryslow -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 -f rawvideo -y /dev/null}}
+
$ ffmpeg -i ''video''.VOB -an -vcodec libx264 -pass 1 -preset veryslow \
 +
-threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 -f rawvideo -y /dev/null
 
Container format is automatically detected and muxed into from the output file extenstion ({{ic|.mkv}}):
 
Container format is automatically detected and muxed into from the output file extenstion ({{ic|.mkv}}):
{{bc|1=ffmpeg -i Transformers.3.Dark.of.the.Moon.VOB -acodec libvo-aacenc -ab 256k -ar 96000 -vcodec libx264 -pass 2 -preset veryslow -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 Transformers.3.Dark.of.the.Moon.mkv}}
+
$ ffmpeg -i ''video''.VOB -acodec libvo-aacenc -ab 256k -ar 96000 -vcodec libx264 \
 +
-pass 2 -preset veryslow -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 ''video''.mkv
 +
 
 +
{{Tip|If you receive {{ic|Unknown encoder 'libvo-aacenc'}} error (given the fact that your ffmpeg is compiled with libvo-aacenc enabled), you may want to try {{ic|-acodec libvo_aacenc}}, an underscore instead of hyphen.}}
  
 
=== Two-pass MPEG-4 (very high-quality) ===
 
=== Two-pass MPEG-4 (very high-quality) ===
 +
 
Audio deactivated as only video statistics are logged during the first of multiple pass runs:  
 
Audio deactivated as only video statistics are logged during the first of multiple pass runs:  
{{bc|ffmpeg -i Transformers.3.Dark.of.the.Moon.VOB -an -vcodec mpeg4 -pass 1 -mbd 2 -trellis 2 -flags +cbp+mv0 -pre_dia_size 4 -dia_size 4 -precmp 4 -cmp 4 -subcmp 4 -preme 2 -qns 2 -b 3000k -f rawvideo -y /dev/null}}
+
$ ffmpeg -i ''video''.VOB -an -vcodec mpeg4 -pass 1 -mbd 2 -trellis 2 -flags +cbp+mv0 \
 +
-pre_dia_size 4 -dia_size 4 -precmp 4 -cmp 4 -subcmp 4 -preme 2 -qns 2 -b 3000k \
 +
-f rawvideo -y /dev/null
  
 
Container format is automatically detected and muxed into from the output file extenstion ({{ic|.avi}}):
 
Container format is automatically detected and muxed into from the output file extenstion ({{ic|.avi}}):
{{bc|ffmpeg -i Transformers.3.Dark.of.the.Moon.VOB -acodec copy -vcodec mpeg4 -vtag DX50 -pass 2 -mbd 2 -trellis 2 -flags +cbp+mv0 -pre_dia_size 4 -dia_size 4 -precmp 4 -cmp 4 -subcmp 4 -preme 2 -qns 2 -b 3000k Transformers.3.Dark.of.the.Moon.avi}}
+
$ ffmpeg -i ''video''.VOB -acodec copy -vcodec mpeg4 -vtag DX50 -pass 2 -mbd 2 -trellis 2 \
 +
-flags +cbp+mv0 -pre_dia_size 4 -dia_size 4 -precmp 4 -cmp 4 -subcmp 4 -preme 2 -qns 2 \
 +
-b 3000k ''video''.avi
 
* Introducing {{ic|threads}}={{ic|n}}>{{ic|1}} for {{ic|-vcodec mpeg4}} may skew the effects of [[Wikipedia:Motion_estimation|motion estimation]] and lead to [http://ffmpeg.org/faq.html#Why-do-I-see-a-slight-quality-degradation-with-multithreaded-MPEG_002a-encoding_003f reduced video quality] and compression efficiency.
 
* Introducing {{ic|threads}}={{ic|n}}>{{ic|1}} for {{ic|-vcodec mpeg4}} may skew the effects of [[Wikipedia:Motion_estimation|motion estimation]] and lead to [http://ffmpeg.org/faq.html#Why-do-I-see-a-slight-quality-degradation-with-multithreaded-MPEG_002a-encoding_003f reduced video quality] and compression efficiency.
 
* The two-pass MPEG-4 example above also supports output to the [[Wikipedia:MPEG-4_Part_14|MP4]] container (replace {{ic|.avi}} with {{ic|.mp4}}).
 
* The two-pass MPEG-4 example above also supports output to the [[Wikipedia:MPEG-4_Part_14|MP4]] container (replace {{ic|.avi}} with {{ic|.mp4}}).
  
 
==== Determining bitrates with fixed output file sizes ====
 
==== Determining bitrates with fixed output file sizes ====
 +
 
* (Desired File Size in MB - Audio File Size in MB) '''x''' 8192 kb/MB '''/''' Length of Media in Seconds (s) '''=''' [[Wikipedia:Bitrate|Bitrate]] in kb/s
 
* (Desired File Size in MB - Audio File Size in MB) '''x''' 8192 kb/MB '''/''' Length of Media in Seconds (s) '''=''' [[Wikipedia:Bitrate|Bitrate]] in kb/s
 
:* (3900 MB - 275 MB) = 3625 MB '''x''' 8192 kb/MB '''/''' 8830 s = 3363 kb/s required to achieve an approximate total output file size of 3900 MB
 
:* (3900 MB - 275 MB) = 3625 MB '''x''' 8192 kb/MB '''/''' 8830 s = 3363 kb/s required to achieve an approximate total output file size of 3900 MB
  
== Preset files ==
+
=== Subtitles ===
=== Creating presets ===
+
  
Populate {{ic|~/.ffmpeg}} with the default [http://ffmpeg.org/ffmpeg-doc.html#SEC13 preset files]:
+
==== Extracting ====
  
$ cp -iR /usr/share/ffmpeg ~/.ffmpeg
+
Subtitles embedded in container files, such as MPEG-2 and Matroska, can be extracted and converted into SRT, SSA, among other subtitle formats.
  
Create new and/or modify the default preset files:
+
* Inspect a file to determine if it contains a subtitle stream:
  
{{hc|~/.ffmpeg/libavcodec-vhq.ffpreset|<nowiki>
+
{{hc|$ ffprobe foo.mkv|
vtag=DX50
+
...
mbd=2
+
Stream #0:0(und): Video: h264 (High), yuv420p, 1920x800 [SAR 1:1 DAR 12:5], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
trellis=2
+
  Metadata:
flags=+cbp+mv0
+
  CREATION_TIME  : 2012-06-05 05:04:15
pre_dia_size=4
+
  LANGUAGE        : und
dia_size=4
+
Stream #0:1(und): Audio: aac, 44100 Hz, stereo, fltp (default)
  precmp=4
+
  Metadata:
  cmp=4
+
  CREATION_TIME  : 2012-06-05 05:10:34
  subcmp=4
+
  LANGUAGE        : und
  preme=2
+
  HANDLER_NAME    : GPAC ISO Audio Handler
qns=2</nowiki>}}
+
'''Stream #0:2: Subtitle: ssa (default)}}
  
=== Using preset files ===
+
* {{ic|foo.mkv}} has an embedded SSA subtitle which can be extracted into an independent file:
  
Enable the {{ic|-vpre}} option after declaring the desired {{ic|-vcodec}}
+
  $ ffmpeg -i foo.mkv foo.ssa
  
==== {{ic|libavcodec-vhq.ffpreset}} ====
+
==== Hardsubbing ====
  
* {{ic|libavcodec}} '''=''' Name of the vcodec/acodec
+
(instructions based on an [http://trac.ffmpeg.org/wiki/How%20to%20burn%20subtitles%20into%20the%20video FFmpeg wiki article])
* {{ic|vhq}} '''=''' Name of specific preset to be called out
+
* {{ic|ffpreset}} '''=''' FFmpeg preset filetype suffix
+
  
===== Two-pass MPEG-4 (very high quality) =====
+
[[Wikipedia:Hardsub|Hardsubbing]] entails merging subtitles with the video. Hardsubs can't be disabled, nor language switched.
  
First pass of a multipass (bitrate) ratecontrol transcode:
+
* Overlay {{ic|foo.mpg}} with the subtitles in {{ic|foo.ssa}}:
{{bc|ffmpeg -i 13.Assassins.2010.mpg -an -vcodec mpeg4 -pass 1 -vpre vhq -f rawvideo -y /dev/null}}
+
Ratecontrol based on the video statistics logged from the first pass:
+
{{bc|ffmpeg -i 13.Assassins.2010.mpg -acodec libvorbis -aq 8 -ar 48000 -vcodec mpeg4 -pass 2 -vpre vhq -b 3000k 13.Assassins.2010.mp4}}
+
 
+
* '''libvorbis quality settings (VBR)'''
+
:* -aq 4 '''=''' 128 kb/s
+
:* -aq 5 '''=''' 160 kb/s
+
:* -aq 6 '''=''' 192 kb/s
+
:* -aq 7 '''=''' 224 kb/s
+
:* -aq 8 '''=''' 256 kb/s
+
  
*[http://www.geocities.jp/aoyoume/aotuv/ aoTuV] is generally preferred over [http://vorbis.com/ libvorbis] provided by [http://www.xiph.org/ Xiph.Org] and is provided by [http://aur.archlinux.org/packages.php?ID=6155 libvorbis-aotuv] in the [[AUR]].
+
$ ffmpeg -i foo.mpg -c copy -vf subtitles=foo.ssa out.mpg
  
== Volume gain ==
+
=== Volume gain ===
  
 
Change the audio volume in multiples of 256 where '''256 = 100%''' (normal) volume. Additional values such as 400 are also valid options.  
 
Change the audio volume in multiples of 256 where '''256 = 100%''' (normal) volume. Additional values such as 400 are also valid options.  
Line 122: Line 173:
  
 
To double the volume '''(512 = 200%)''' of an [[Wikipedia:Mp3|MP3]] file:
 
To double the volume '''(512 = 200%)''' of an [[Wikipedia:Mp3|MP3]] file:
  ffmpeg -i example.mp3 -vol 512 loud-example.mp3
+
  $ ffmpeg -i ''file''.mp3 -vol 512 ''louder file''.mp3
  
 
To quadruple the volume '''(1024 = 400%)''' of an [[Wikipedia:Ogg|Ogg]] file:
 
To quadruple the volume '''(1024 = 400%)''' of an [[Wikipedia:Ogg|Ogg]] file:
  ffmpeg -i example.ogg -vol 1024 loud-example.ogg
+
  $ ffmpeg -i ''file''.ogg -vol 1024 ''louder file''.ogg
  
 
Note that gain metadata is only written to the output file. Unlike mp3gain or ogggain, the source sound file is untouched.
 
Note that gain metadata is only written to the output file. Unlike mp3gain or ogggain, the source sound file is untouched.
  
== Extracting audio ==
+
=== Extracting audio ===
  
{{bc|$ ffmpeg -i The.Kings.Speech.mpg<br>
+
{{hc|$ ffmpeg -i ''video''.mpg|
Input #0, avi, from 'The.Kings.Speech.2010.mpg':<br>  Duration: 01:58:28.96, start: 0.000000, bitrate: 3000 kb/s<br>    Stream #0.0: Video: mpeg4, yuv420p, 720x480 [PAR 1:1 DAR 16:9], 29.97 tbr, 29.97 tbn, 29.97 tbc<br>    Stream #0.1: Audio: ac3, 48000 Hz, stereo, s16, 384 kb/s<br>    Stream #0.2: Audio: ac3, 48000 Hz, 5.1, s16, 448 kb/s<br>    Stream #0.3: Audio: dts, 48000 Hz, 5.1 768 kb/s}}
+
...
 +
Input #0, avi, from '''video''.mpg':
 +
  Duration: 01:58:28.96, start: 0.000000, bitrate: 3000 kb/s
 +
    Stream #0.0: Video: mpeg4, yuv420p, 720x480 [PAR 1:1 DAR 16:9], 29.97 tbr, 29.97 tbn, 29.97 tbc
 +
    Stream #0.1: Audio: ac3, 48000 Hz, stereo, s16, 384 kb/s
 +
    Stream #0.2: Audio: ac3, 48000 Hz, 5.1, s16, 448 kb/s
 +
    Stream #0.3: Audio: dts, 48000 Hz, 5.1 768 kb/s
 +
...
 +
}}
  
 
Extract the first ({{ic|-map 0:1}}) [[Wikipedia:Dolby_Digital|AC-3]] encoded audio stream exactly as it was multiplexed into the file:  
 
Extract the first ({{ic|-map 0:1}}) [[Wikipedia:Dolby_Digital|AC-3]] encoded audio stream exactly as it was multiplexed into the file:  
{{bc|ffmpeg -i The.Kings.Speech.mpg -map 0:1 -acodec copy -vn The.Kings.Speech.ac3}}
+
$ ffmpeg -i ''video''.mpg -map 0:1 -acodec copy -vn ''video''.ac3
 
Convert the third ({{ic|-map 0:3}}) [[Wikipedia:DTS_(sound_system)|DTS]] audio stream to an [[Wikipedia:Advanced_Audio_Coding|AAC]] file with a bitrate of 192 kb/s and a [[Wikipedia:Sampling_rate|sampling rate]] of 96000 Hz:
 
Convert the third ({{ic|-map 0:3}}) [[Wikipedia:DTS_(sound_system)|DTS]] audio stream to an [[Wikipedia:Advanced_Audio_Coding|AAC]] file with a bitrate of 192 kb/s and a [[Wikipedia:Sampling_rate|sampling rate]] of 96000 Hz:
{{bc|ffmpeg -i The.Kings.Speech.mpg -map 0:3 -acodec libvo-aacenc -ab 192k -ar 96000 -vn The.Kings.Speech.aac}}
+
$ ffmpeg -i ''video''.mpg -map 0:3 -acodec libvo-aacenc -ab 192k -ar 96000 -vn ''output''.aac
 
{{ic|-vn}} disables the processing of the video stream.
 
{{ic|-vn}} disables the processing of the video stream.
 +
 +
Extract audio stream with certain time interval:
 +
$ ffmpeg -ss 00:01:25 -t 00:00:05 -i ''video''.mpg -map 0:1 -acodec copy -vn ''output''.ac3
 +
{{ic|-ss}} specifies the start point, and {{ic|-t}} specifies the duration.
  
 
=== Stripping audio ===
 
=== Stripping audio ===
Line 144: Line 207:
 
# Copy the first video stream ({{ic|-map 0:0}}) along with the second AC-3 audio stream ({{ic|-map 0:2}}).
 
# Copy the first video stream ({{ic|-map 0:0}}) along with the second AC-3 audio stream ({{ic|-map 0:2}}).
 
# Convert the AC-3 audio stream to two-channel MP3 with a bitrate of 128 kb/s and a sampling rate of 48000 Hz.
 
# Convert the AC-3 audio stream to two-channel MP3 with a bitrate of 128 kb/s and a sampling rate of 48000 Hz.
{{bc|ffmpeg -i The.Kings.Speech.mpg -map 0:0 -map 0:2 -vcodec copy -acodec libmp3lame -ab 128k -ar 48000 -ac 2 The.Kings.Speech.mkv}}
+
$ ffmpeg -i ''video''.mpg -map 0:0 -map 0:2 -vcodec copy -acodec libmp3lame \
{{bc|$ ffmpeg -i The.Kings.Speech.mkv<br>
+
-ab 128k -ar 48000 -ac 2 ''video''.mkv
Input #0, avi, from 'The.Kings.Speech.2010.mpg':<br>  Duration: 01:58:28.96, start: 0.000000, bitrate: 3000 kb/s<br>    Stream #0.0: Video: mpeg4, yuv420p, 720x480 [PAR 1:1 DAR 16:9], 29.97 tbr, 29.97 tbn, 29.97 tbc<br>    Stream #0.1: Audio: mp3, 48000 Hz, stereo, s16, 128 kb/s}}
+
 
 +
{{hc|$ ffmpeg -i ''video''.mkv|
 +
...
 +
Input #0, avi, from '''video''.mpg':
 +
  Duration: 01:58:28.96, start: 0.000000, bitrate: 3000 kb/s
 +
    Stream #0.0: Video: mpeg4, yuv420p, 720x480 [PAR 1:1 DAR 16:9], 29.97 tbr, 29.97 tbn, 29.97 tbc
 +
    Stream #0.1: Audio: mp3, 48000 Hz, stereo, s16, 128 kb/s
 +
}}
 +
 
 
{{Note|Removing undesired audio streams allows for additional bits to be allocated towards improving video quality.}}
 
{{Note|Removing undesired audio streams allows for additional bits to be allocated towards improving video quality.}}
  
== Adding subtitles ==
+
== Preset files ==
  
FFmpeg does not currently support muxing [[Wikipedia:Category:Subtitle_file_formats|subtitle files]] into existing streams. See [[Mencoder#Adding_SubRip_subtitles_to_a_file|MEncoder]] for subtitle muxing support.
+
Populate {{ic|~/.ffmpeg}} with the default [http://ffmpeg.org/ffmpeg-doc.html#SEC13 preset files]:
  
== Recording webcam ==
+
$ cp -iR /usr/share/ffmpeg ~/.ffmpeg
  
FFmpeg supports grabbing input from Video4Linux2 devices.  The following command will record a video from the webcam, assuming that the webcam is correctly recognized under {{Ic|/dev/video0}}:
+
Create new and/or modify the default preset files:
  
$ ffmpeg -f v4l2 -s 640x480 -i /dev/video0 out.mpg
+
{{hc|~/.ffmpeg/libavcodec-vhq.ffpreset|2=
 +
vtag=DX50
 +
mbd=2
 +
trellis=2
 +
flags=+cbp+mv0
 +
pre_dia_size=4
 +
dia_size=4
 +
precmp=4
 +
cmp=4
 +
subcmp=4
 +
preme=2
 +
qns=2
 +
}}
  
The above produces a silent video.  It is also possible to include audio sources from a microphone.  The following command will include a stream from the default [[Advanced Linux Sound Architecture|ALSA]] recording device into the video:
+
=== Using preset files ===
  
  $ ffmpeg -f alsa -i default -f v4l2 -s 640x480 -i /dev/video0 out.mpg
+
Enable the {{ic|-vpre}} option after declaring the desired {{ic|-vcodec}}
  
To use [[PulseAudio]] with an ALSA backend:
+
==== libavcodec-vhq.ffpreset ====
  
$ ffmpeg -f alsa -i pulse -f v4l2 -s 640x480 -i /dev/video0 out.mpg
+
* {{ic|libavcodec}} '''=''' Name of the vcodec/acodec
 +
* {{ic|vhq}} '''=''' Name of specific preset to be called out
 +
* {{ic|ffpreset}} '''=''' FFmpeg preset filetype suffix
  
For a higher quality capture, try encoding the output using higher quality codecs:
+
===== Two-pass MPEG-4 (very high quality) =====
  
  $ ffmpeg -f alsa -i default -f v4l2 -s 640x480 -i /dev/video0 -acodec flac -vcodec libx264 out.mkv
+
First pass of a multipass (bitrate) ratecontrol transcode:
 +
  $ ffmpeg -i ''video''.mpg -an -vcodec mpeg4 -pass 1 -vpre vhq -f rawvideo -y /dev/null
 +
Ratecontrol based on the video statistics logged from the first pass:
 +
$ ffmpeg -i ''video''.mpg -acodec libvorbis -aq 8 -ar 48000 -vcodec mpeg4 \
 +
-pass 2 -vpre vhq -b 3000k ''output''.mp4
 +
 
 +
* '''libvorbis quality settings (VBR)'''
 +
:* {{ic|-aq 4}} = 128 kb/s
 +
:* {{ic|-aq 5}} = 160 kb/s
 +
:* {{ic|-aq 6}} = 192 kb/s
 +
:* {{ic|-aq 7}} = 224 kb/s
 +
:* {{ic|-aq 8}} = 256 kb/s
 +
 
 +
* [http://www.geocities.jp/aoyoume/aotuv/ aoTuV] is generally preferred over [http://vorbis.com/ libvorbis] provided by [http://www.xiph.org/ Xiph.Org] and is provided by [https://aur.archlinux.org/packages.php?ID=6155 libvorbis-aotuv] in the [[AUR]].
  
 
== Package removal ==
 
== Package removal ==
Line 175: Line 273:
 
[[pacman]] will not [[Pacman#Removing_packages|remove]] configuration files outside of the defaults that were created during package installation. This includes user-created preset files.
 
[[pacman]] will not [[Pacman#Removing_packages|remove]] configuration files outside of the defaults that were created during package installation. This includes user-created preset files.
  
== Additional resources ==
+
== See also ==
* [http://mewiki.project357.com/wiki/X264_Settings x264 Settings] - MeWiki Documentation
+
 
* [http://ffmpeg.org/ffmpeg-doc.html FFmpeg Documentation] - Official Documentation
+
* [http://mewiki.project357.com/wiki/X264_Settings x264 Settings] - MeWiki documentation
* [http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-x264.html Encoding with the x264 Codec] - MEncoder Documentation
+
* [http://ffmpeg.org/ffmpeg-doc.html FFmpeg documentation] - official documentation
* [http://avidemux.org/admWiki/doku.php?id=tutorial:h.264 H.264 eEcoding Guide] - Avidemux Wiki
+
* [http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-x264.html Encoding with the x264 codec] - MEncoder documentation
* [http://howto-pages.org/ffmpeg/ Using FFmpeg] - Linux How To Pages
+
* [http://avidemux.org/admWiki/doku.php?id=tutorial:h.264 H.264 encoding guide] - Avidemux wiki
 +
* [http://howto-pages.org/ffmpeg/ Using FFmpeg] - Linux how to pages
 +
* [http://ffmpeg.org/general.html#Supported-File-Formats-and-Codecs List] of supported audio and video streams

Revision as of 20:23, 27 November 2013

From the project home page:

FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video. It includes libavcodec - the leading audio/video codec library.

Package installation

Various flavors and related projects can be installed from the official repositories and the AUR:

Notable variants:

  • ffmpeg-gitAUR – development version
  • ffmpeg-fullAUR – built with as much optional features enabled as possible

Forks:

  • ffmbcAUR – targeted for broadcasting usage
  • libav-gitAUR – the binary it provides is called avconv instead of ffmpeg

Encoding examples

Screen cast

FFmpeg includes the x11grab and ALSA virtual devices that enable capturing the entire user display and audio output.

First create test.mkv with lossless encoding:

$ ffmpeg -f x11grab -i $DISPLAY -f alsa -i default -c:v ffvhuff -c:a flac test.mkv

then we process it into a smaller Wikipedia:WebM file:

$ ffmpeg -i test.mkv  -c:v libvpx -c:a libvorbis  -b:v 2000k -q:a 3 test.webm

Recording webcam

FFmpeg supports grabbing input from Video4Linux2 devices. The following command will record a video from the webcam, assuming that the webcam is correctly recognized under /dev/video0:

$ ffmpeg -f v4l2 -s 640x480 -i /dev/video0 output.mpg

The above produces a silent video. It is also possible to include audio sources from a microphone. The following command will include a stream from the default ALSA recording device into the video:

$ ffmpeg -f alsa -i default -f v4l2 -s 640x480 -i /dev/video0 output.mpg

To use PulseAudio with an ALSA backend:

$ ffmpeg -f alsa -i pulse -f v4l2 -s 640x480 -i /dev/video0 output.mpg

For a higher quality capture, try encoding the output using higher quality codecs:

$ ffmpeg -f alsa -i default -f v4l2 -s 640x480 -i /dev/video0 -acodec flac \
-vcodec libx264 output.mkv

VOB to any container

Concatenate the desired Wikipedia:VOB files into a single stream and mux them to MPEG-2:

$ cat f0.VOB f1.VOB f2.VOB | ffmpeg -i - out.mp2

x264 lossless

The ultrafast preset will provide the fastest encoding and is useful for quick capturing (such as screencasting):

$ ffmpeg -i input -vcodec libx264 -preset ultrafast -qp 0 -acodec copy output.mkv

On the opposite end of the preset spectrum is veryslow and will encode slower than ultrafast but provide a smaller output file size:

$ ffmpeg -i input -vcodec libx264 -preset veryslow -qp 0 -acodec copy output.mkv

Both examples will provide the same quality output.

Single-pass MPEG-2 (near lossless)

Allow FFmpeg to automatically set DVD standardized parameters. Encode to DVD MPEG-2 at ~30 FPS:

$ ffmpeg -i video.VOB -target ntsc-dvd -q:a 0 -q:v 0 output.mpg

Encode to DVD MPEG-2 at ~24 FPS:

$ ffmpeg -i video.VOB -target film-dvd -q:a 0 -q:v 0 output.mpg

x264: constant rate factor

Used when you want a specific quality output. General usage is to use the highest -crf value that still provides an acceptable quality. Lower values are higher quality; 0 is lossless, 18 is visually lossless, and 23 is the default value. A sane range is between 18 and 28. Use the slowest -preset you have patience for. See the x264 Encoding Guide for more information.

$ ffmpeg -i video -vcodec libx264 -preset slow -crf 22 -acodec libmp3lame -aq 4 output.mkv

-tune option can be used to match the type and content of the of media being encoded.

YouTube

FFmpeg is very useful to encode videos and strip their size before you upload them on YouTube. The following single line of code takes an input file and outputs a mkv container.

$ ffmpeg -i video -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a copy output.mkv

For more information see the forums. You can also create a custom alias ytconvert which takes the name of the input file as first argument and the name of the .mkv container as second argument. To do so add the following to your ~/.bashrc:

youtubeConvert(){
        ffmpeg -i $1 -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a copy $2.mkv
}
alias ytconvert=youtubeConvert

See also Arch Linux forum thread.

Two-pass x264 (very high-quality)

Audio deactivated as only video statistics are recorded during the first of multiple pass runs:

$ ffmpeg -i video.VOB -an -vcodec libx264 -pass 1  -preset veryslow \
-threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 -f rawvideo -y /dev/null

Container format is automatically detected and muxed into from the output file extenstion (.mkv):

$ ffmpeg -i video.VOB -acodec libvo-aacenc -ab 256k -ar 96000 -vcodec libx264 \
-pass 2 -preset veryslow -threads 0 -b 3000k -x264opts frameref=15:fast_pskip=0 video.mkv
Tip: If you receive Unknown encoder 'libvo-aacenc' error (given the fact that your ffmpeg is compiled with libvo-aacenc enabled), you may want to try -acodec libvo_aacenc, an underscore instead of hyphen.

Two-pass MPEG-4 (very high-quality)

Audio deactivated as only video statistics are logged during the first of multiple pass runs:

$ ffmpeg -i video.VOB -an -vcodec mpeg4 -pass 1 -mbd 2 -trellis 2 -flags +cbp+mv0 \
-pre_dia_size 4 -dia_size 4 -precmp 4 -cmp 4 -subcmp 4 -preme 2 -qns 2 -b 3000k \
-f rawvideo -y /dev/null

Container format is automatically detected and muxed into from the output file extenstion (.avi):

$ ffmpeg -i video.VOB -acodec copy -vcodec mpeg4 -vtag DX50 -pass 2 -mbd 2 -trellis 2 \
-flags +cbp+mv0 -pre_dia_size 4 -dia_size 4 -precmp 4 -cmp 4 -subcmp 4 -preme 2 -qns 2 \
-b 3000k video.avi
  • Introducing threads=n>1 for -vcodec mpeg4 may skew the effects of motion estimation and lead to reduced video quality and compression efficiency.
  • The two-pass MPEG-4 example above also supports output to the MP4 container (replace .avi with .mp4).

Determining bitrates with fixed output file sizes

  • (Desired File Size in MB - Audio File Size in MB) x 8192 kb/MB / Length of Media in Seconds (s) = Bitrate in kb/s
  • (3900 MB - 275 MB) = 3625 MB x 8192 kb/MB / 8830 s = 3363 kb/s required to achieve an approximate total output file size of 3900 MB

Subtitles

Extracting

Subtitles embedded in container files, such as MPEG-2 and Matroska, can be extracted and converted into SRT, SSA, among other subtitle formats.

  • Inspect a file to determine if it contains a subtitle stream:
$ ffprobe foo.mkv
...
Stream #0:0(und): Video: h264 (High), yuv420p, 1920x800 [SAR 1:1 DAR 12:5], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
  Metadata:
  CREATION_TIME   : 2012-06-05 05:04:15
  LANGUAGE        : und
Stream #0:1(und): Audio: aac, 44100 Hz, stereo, fltp (default)
 Metadata:
 CREATION_TIME   : 2012-06-05 05:10:34
 LANGUAGE        : und
 HANDLER_NAME    : GPAC ISO Audio Handler
Stream #0:2: Subtitle: ssa (default)
  • foo.mkv has an embedded SSA subtitle which can be extracted into an independent file:
$ ffmpeg -i foo.mkv foo.ssa

Hardsubbing

(instructions based on an FFmpeg wiki article)

Hardsubbing entails merging subtitles with the video. Hardsubs can't be disabled, nor language switched.

  • Overlay foo.mpg with the subtitles in foo.ssa:
$ ffmpeg -i foo.mpg -c copy -vf subtitles=foo.ssa out.mpg

Volume gain

Change the audio volume in multiples of 256 where 256 = 100% (normal) volume. Additional values such as 400 are also valid options.

-vol 256  = 100%
-vol 512  = 200%
-vol 768  = 300%
-vol 1024 = 400%
-vol 2048 = 800%

To double the volume (512 = 200%) of an MP3 file:

$ ffmpeg -i file.mp3 -vol 512 louder file.mp3

To quadruple the volume (1024 = 400%) of an Ogg file:

$ ffmpeg -i file.ogg -vol 1024 louder file.ogg

Note that gain metadata is only written to the output file. Unlike mp3gain or ogggain, the source sound file is untouched.

Extracting audio

$ ffmpeg -i video.mpg
...
Input #0, avi, from 'video.mpg':
  Duration: 01:58:28.96, start: 0.000000, bitrate: 3000 kb/s
    Stream #0.0: Video: mpeg4, yuv420p, 720x480 [PAR 1:1 DAR 16:9], 29.97 tbr, 29.97 tbn, 29.97 tbc
    Stream #0.1: Audio: ac3, 48000 Hz, stereo, s16, 384 kb/s
    Stream #0.2: Audio: ac3, 48000 Hz, 5.1, s16, 448 kb/s
    Stream #0.3: Audio: dts, 48000 Hz, 5.1 768 kb/s
...

Extract the first (-map 0:1) AC-3 encoded audio stream exactly as it was multiplexed into the file:

$ ffmpeg -i video.mpg -map 0:1 -acodec copy -vn video.ac3

Convert the third (-map 0:3) DTS audio stream to an AAC file with a bitrate of 192 kb/s and a sampling rate of 96000 Hz:

$ ffmpeg -i video.mpg -map 0:3 -acodec libvo-aacenc -ab 192k -ar 96000 -vn output.aac

-vn disables the processing of the video stream.

Extract audio stream with certain time interval:

$ ffmpeg -ss 00:01:25 -t 00:00:05 -i video.mpg -map 0:1 -acodec copy -vn output.ac3

-ss specifies the start point, and -t specifies the duration.

Stripping audio

  1. Copy the first video stream (-map 0:0) along with the second AC-3 audio stream (-map 0:2).
  2. Convert the AC-3 audio stream to two-channel MP3 with a bitrate of 128 kb/s and a sampling rate of 48000 Hz.
$ ffmpeg -i video.mpg -map 0:0 -map 0:2 -vcodec copy -acodec libmp3lame \
-ab 128k -ar 48000 -ac 2 video.mkv
$ ffmpeg -i video.mkv
...
Input #0, avi, from 'video.mpg':
  Duration: 01:58:28.96, start: 0.000000, bitrate: 3000 kb/s
    Stream #0.0: Video: mpeg4, yuv420p, 720x480 [PAR 1:1 DAR 16:9], 29.97 tbr, 29.97 tbn, 29.97 tbc
    Stream #0.1: Audio: mp3, 48000 Hz, stereo, s16, 128 kb/s
Note: Removing undesired audio streams allows for additional bits to be allocated towards improving video quality.

Preset files

Populate ~/.ffmpeg with the default preset files:

$ cp -iR /usr/share/ffmpeg ~/.ffmpeg

Create new and/or modify the default preset files:

~/.ffmpeg/libavcodec-vhq.ffpreset
vtag=DX50
mbd=2
trellis=2
flags=+cbp+mv0
pre_dia_size=4
dia_size=4
precmp=4
cmp=4
subcmp=4
preme=2
qns=2

Using preset files

Enable the -vpre option after declaring the desired -vcodec

libavcodec-vhq.ffpreset

  • libavcodec = Name of the vcodec/acodec
  • vhq = Name of specific preset to be called out
  • ffpreset = FFmpeg preset filetype suffix
Two-pass MPEG-4 (very high quality)

First pass of a multipass (bitrate) ratecontrol transcode:

$ ffmpeg -i video.mpg -an -vcodec mpeg4 -pass 1 -vpre vhq -f rawvideo -y /dev/null

Ratecontrol based on the video statistics logged from the first pass:

$ ffmpeg -i video.mpg -acodec libvorbis -aq 8 -ar 48000 -vcodec mpeg4 \
-pass 2 -vpre vhq -b 3000k output.mp4
  • libvorbis quality settings (VBR)
  • -aq 4 = 128 kb/s
  • -aq 5 = 160 kb/s
  • -aq 6 = 192 kb/s
  • -aq 7 = 224 kb/s
  • -aq 8 = 256 kb/s

Package removal

pacman will not remove configuration files outside of the defaults that were created during package installation. This includes user-created preset files.

See also