Difference between revisions of "FFmpeg"

From ArchWiki
Jump to: navigation, search
(Package installation: Mention AUR version with ALL codecs)
(Fix link)
(5 intermediate revisions by 2 users not shown)
Line 4: Line 4:
 
{{Article summary end}}
 
{{Article summary end}}
  
From the [http://www.ffmpeg.org/ FFmpeg homepage]:
+
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.''
 
:''FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video. It includes libavcodec - the leading audio/video codec library.''
  
Line 11: Line 11:
 
[[pacman|Install]] {{Pkg|ffmpeg}} from the [[official repositories]].
 
[[pacman|Install]] {{Pkg|ffmpeg}} from the [[official repositories]].
  
A drop-in replacement fork called {{AUR|libav}} is available in [[AUR]]. The binary it provides is called '''avconv''' instead of '''ffmpeg'''.
+
Notable variants are:
 
+
* {{AUR|ffmpeg-full}} - This version includes all codecs that due to license constraints are not in the official repositoies version, notably, the Fraunhofer AAC codec and the AAC+ codec.
Note that the version in the official repositories does not include all codecs due to license constraints. Notably, the Fraunhofer AAC codec and the AAC+ codec are not included. {{AUR|ffmpeg-full}} from [[AUR]] provides a drop-in replacement with all codecs enabled.
+
* {{AUR|libav}} - Replacement fork. The binary it provides is called ''avconv'' instead of ''ffmpeg''.
  
 
== Encoding examples ==
 
== Encoding examples ==
Line 19: Line 19:
 
=== Screen cast to .webm ===
 
=== Screen cast to .webm ===
  
Using '''x11grab''' to video grab your display and using '''ALSA''' for sound. First we create lossless raw file {{ic|test.mkv}}.
+
Using ''x11grab'' to video grab your display and using ''ALSA'' for sound. First we create lossless raw file {{ic|test.mkv}}.
  
 
  $ ffmpeg -f x11grab -r 30 -i :0.0 -f alsa -i hw:0,0 -acodec flac -vcodec ffvhuff test.mkv
 
  $ ffmpeg -f x11grab -r 30 -i :0.0 -f alsa -i hw:0,0 -acodec flac -vcodec ffvhuff test.mkv
Line 27: Line 27:
 
  $ ffmpeg -y -i test.mkv -c:a libvorbis -q:a 3 -c:v libvpx -b:v 2000k test.webm
 
  $ ffmpeg -y -i test.mkv -c:a libvorbis -q:a 3 -c:v libvpx -b:v 2000k test.webm
  
See https://github.com/kaihendry/recordmydesktop2.0/blob/master/r2d2.sh for a more fleshed out example.
+
See https://github.com/kaihendry/recordmydesktop2.0/ for a more fleshed out example.
 +
 
 +
=== 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 ===
Line 39: Line 58:
  
 
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):
  $ 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:
  $ 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.
  
Line 68: Line 87:
 
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}}:
 
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}}:
  
youtubeConvert(){
+
{{bc|<nowiki>
ffmpeg -i $1 -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a copy $2.mkv
+
youtubeConvert(){
}
+
        ffmpeg -i $1 -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a copy $2.mkv
alias ytconvert=youtubeConvert
+
}
 +
alias ytconvert=youtubeConvert
 +
</nowiki>}}
 
See also [https://bbs.archlinux.org/viewtopic.php?pid=1200542#p1200542 Arch Linux forum thread].
 
See also [https://bbs.archlinux.org/viewtopic.php?pid=1200542#p1200542 Arch Linux forum thread].
  
Line 104: Line 125:
 
:* (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
  
=== Softsubs to hardsubs ===
+
=== Adding subtitles ===
 +
 
 +
See http://trac.ffmpeg.org/wiki/How%20to%20burn%20subtitles%20into%20the%20video
 +
 
 +
==== Softsubs to hardsubs ====
  
 
If have a softsubbed video (eg. ASS/SSA subs in a mkv container like most anime) you can 'burn' these subs into a new file to be played on a device which does not support subs or is to weak to display complex subs.
 
If have a softsubbed video (eg. ASS/SSA subs in a mkv container like most anime) you can 'burn' these subs into a new file to be played on a device which does not support subs or is to weak to display complex subs.
  
* Install {{Pkg|mkvtoolnix-cli}} to pull out *.ass files from *.mkv files.
+
* Install {{Pkg|mkvtoolnix-cli}} to pull out {{ic|.ass}} files from {{ic|.mkv}} files.
  
* Recompile '''ffmpeg''' with {{ic|--enable-libass}} if it is not already enabled in your ffmpeg build. See [[ABS]] for easy recompiling.
+
* Recompile FFmpeg with {{ic|--enable-libass}} if it is not already enabled in your FFmpeg build. See [[ABS]] for easy recompiling.
  
 
* Pull out subs from your file. This command assumes that track #2 is the ASS/SSA track. Use {{ic|mkvinfo}} if it is not.
 
* Pull out subs from your file. This command assumes that track #2 is the ASS/SSA track. Use {{ic|mkvinfo}} if it is not.
Line 121: Line 146:
 
  $ ffmpeg -i ''video''.mkv -sn -vcodec libx264 -crf 18 -preset slow -vf ass=''subtitles''.ass -acodec copy ''output''.mp4
 
  $ ffmpeg -i ''video''.mkv -sn -vcodec libx264 -crf 18 -preset slow -vf ass=''subtitles''.ass -acodec copy ''output''.mp4
  
== Preset files ==
+
=== Volume gain ===
 
+
=== Creating presets ===
+
 
+
 
+
Populate {{ic|~/.ffmpeg}} with the default [http://ffmpeg.org/ffmpeg-doc.html#SEC13 preset files]:
+
 
+
$ cp -iR /usr/share/ffmpeg ~/.ffmpeg
+
 
+
Create new and/or modify the default preset files:
+
 
+
{{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
+
}}
+
 
+
=== Using preset files ===
+
 
+
Enable the {{ic|-vpre}} option after declaring the desired  {{ic|-vcodec}}
+
 
+
==== libavcodec-vhq.ffpreset ====
+
 
+
* {{ic|libavcodec}} '''=''' Name of the vcodec/acodec
+
* {{ic|vhq}} '''=''' Name of specific preset to be called out
+
* {{ic|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
+
 
+
* [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]].
+
 
+
== 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 190: Line 163:
 
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 ===
  
 
{{hc|$ ffmpeg -i ''video''.mpg|
 
{{hc|$ ffmpeg -i ''video''.mpg|
Line 230: Line 203:
 
{{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 ''output''.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 ''output''.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 ''output''.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 \
+
First pass of a multipass (bitrate) ratecontrol transcode:
  -vcodec libx264 ''output''.mkv
+
  $ 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 262: Line 261:
 
* [http://ffmpeg.org/ffmpeg-doc.html FFmpeg documentation] - official documentation
 
* [http://ffmpeg.org/ffmpeg-doc.html FFmpeg documentation] - official documentation
 
* [http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-x264.html Encoding with the x264 codec] - MEncoder documentation
 
* [http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-x264.html Encoding with the x264 codec] - MEncoder documentation
* [http://avidemux.org/admWiki/doku.php?id=tutorial:h.264 H.264 eEcoding guide] - Avidemux wiki
+
* [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://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
 
* [http://ffmpeg.org/general.html#Supported-File-Formats-and-Codecs List] of supported audio and video streams

Revision as of 08:42, 30 July 2013

Summary help replacing me
This article attempts to walk users through the installation, usage and configuration of FFmpeg.

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

Install ffmpeg from the official repositories.

Notable variants are:

  • ffmpeg-fullAUR - This version includes all codecs that due to license constraints are not in the official repositoies version, notably, the Fraunhofer AAC codec and the AAC+ codec.
  • libavAUR - Replacement fork. The binary it provides is called avconv instead of ffmpeg.

Encoding examples

Screen cast to .webm

Using x11grab to video grab your display and using ALSA for sound. First we create lossless raw file test.mkv.

$ ffmpeg -f x11grab -r 30 -i :0.0 -f alsa -i hw:0,0 -acodec flac -vcodec ffvhuff test.mkv

Then we process this test.mkv file into a smaller test.webm end product. Complex switches like c:a and c:v convert the stream into what's needed for WebM.

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

See https://github.com/kaihendry/recordmydesktop2.0/ for a more fleshed out example.

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 VOB files into a single VOB file:

$ cat video-1.VOB video-2.VOB video-3.VOB > output.VOB

Concatenate and then pipe the output VOB to FFmpeg to use a different format:

$ cat video-1.VOB video-2.VOB video-3.VOB > output.VOB | ffmpeg -i ...

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 a frame rate of 30 frames/second:

$ ffmpeg -i video.VOB -target ntsc-dvd -sameq output.mpg

Encode to DVD MPEG-2 at a frame rate of 24 frames/second:

$ ffmpeg -i video.VOB -target film-dvd -sameq 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. A sane range is 18-28 and 23 is default. 18 is considered to be visually lossless. 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

Adding subtitles

See http://trac.ffmpeg.org/wiki/How%20to%20burn%20subtitles%20into%20the%20video

Softsubs to hardsubs

If have a softsubbed video (eg. ASS/SSA subs in a mkv container like most anime) you can 'burn' these subs into a new file to be played on a device which does not support subs or is to weak to display complex subs.

  • Recompile FFmpeg with --enable-libass if it is not already enabled in your FFmpeg build. See ABS for easy recompiling.
  • Pull out subs from your file. This command assumes that track #2 is the ASS/SSA track. Use mkvinfo if it is not.
$ mkvextract tracks your file.mkv 2:your file.ass
  • Recode file with ffmpeg. See sections above for suitable options. It is very important to disable sub-recording and enable sub-rendering:
$ ffmpeg ... -sn -vf ass=subtitles.ass

Output is set as *.mp4 since the default Android 4.2 player dislikes *.mkv. (But VLC on Android works with mkv). Example:

$ ffmpeg -i video.mkv -sn -vcodec libx264 -crf 18 -preset slow -vf ass=subtitles.ass -acodec copy output.mp4

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