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.

Installation

Note: You may encounter FFmpeg forks like libav and avconv, see The FFmpeg/Libav situation for a blog article about the differences between the project and the current status of FFmpeg.

Install the ffmpeg package.

For the development version, install the ffmpeg-gitAUR package. There is also ffmpeg-fullAUR, which is built with as many optional features enabled as possible.

Encoding examples

Note:
  • It is important that parameters are specified in the correct order (e.g. input, video, filters, audio, output). Failing to do so may cause parameters being skipped or will prevent FFmpeg from executing.
  • FFmpeg should automatically choose the number of CPU threads available. However you may want to force the number of threads available by the parameter -threads number.

See FFmpeg encoding wiki and ffmpeg(1) §EXAMPLES.

Screen capture

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

To take a screenshot screen.png:

$ ffmpeg -f x11grab -video_size 1920x1080 -i $DISPLAY -vframes 1 screen.png

where -video_size specifies the size of the area to capture.

To take a screencast with lossless encoding and without audio:

$ ffmpeg -f x11grab -video_size 1920x1080 -framerate 25 -i $DISPLAY -c:v ffvhuff screen.mkv

Here, the Huffyuv codec is used, which is fast, but produces huge file sizes.

To take a screencast with lossy encoding and with audio:

$ ffmpeg -f x11grab -video_size 1920x1080 -framerate 25 -i $DISPLAY -f alsa -i default -c:v libx264 -preset ultrafast -c:a aac screen.mp4

Here, the x264 codec with the fastest possible encoding speed is used. Other codecs can be used; if writing each frame is too slow (either due to inadequate disk performance or slow encoding), then frames will be dropped and video output will be choppy.

See also the official documentation.

Recording webcam

FFmpeg includes the video4linux2 and ALSA input devices that enable capturing webcam and audio input.

The following command will record a video from the webcam without audio, assuming that the webcam is correctly recognized under :

$ ffmpeg -f v4l2 -video_size 640x480 -i /dev/video0 -c:v libx264 -preset ultrafast webcam.mp4

where -video_size specifies the largest allowed image size from the webcam.

The above produces a silent video. To record a video from the webcam with audio:

$ ffmpeg -f v4l2 -video_size 640x480 -i /dev/video0 -f alsa -i default -c:v libx264 -preset ultrafast -c:a aac webcam.mp4

Here, the x264 codec with the fastest possible encoding speed is used. Other codecs can be used; if writing each frame is too slow (either due to inadequate disk performance or slow encoding), then frames will be dropped and video output will be choppy.

See also the official documentation.

VOB to any container

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

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

Lossless

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

$ ffmpeg -i input -c:v libx264 -preset ultrafast -qp 0 -c:a copy output

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 -c:v libx264 -preset veryslow -qp 0 -c:a copy output

Both examples will provide the same quality output.

Tip: If your computer is able to handle -preset superfast in realtime, you should use that instead of -preset ultrafast. Ultrafast is far less efficient compression than superfast.

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 you have patience for. See the x264 Encoding Guide for more information.

$ ffmpeg -i video -c:v libx264 -tune film -preset slow -crf 22 -x264opts fast_pskip=0 -c:a libmp3lame -aq 4 output.mkv
option can be used to match the type and content of the of media being encoded.

Two-pass (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:v 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 ():

$ ffmpeg -i video.VOB -acodec aac -b:a 256k -ar 96000 -vcodec libx264 \
-pass 2 -preset veryslow -threads 0 -b:v 3000k -x264opts frameref=15:fast_pskip=0 video.mkv

Video stabilization

Video stablization using the vid.stab plugin entails two passes.

First pass

The first pass records stabilization parameters to a file and/or a test video for visual analysis.

  • Records stabilization parameters to a file only
  • Records stabilization parameters to a file and create test video "output-stab" for visual analysis
Second pass

The second pass parses the stabilization parameters generated from the first pass and applies them to produce "output-stab_final". You will want to apply any additional filters at this point so as to avoid subsequent transcoding to preserve as much video quality as possible. The following example performs the following in addition to video stabilization:

  • is recommended by the author of vid.stab. Here we are simply using the defaults of 5:5:1.0:5:5:1.0
  • fade in from black starting from the beginning of the file for four seconds
  • fade out to black starting from sixty seconds into the video for four seconds
  • -c:a pcm_s16le XAVC-S codec records in pcm_s16be which is losslessly transcoded to pcm_s16le
$  ffmpeg -i input -vf vidstabtransform=smoothing=30:interpol=bicubic:input=transforms.trf,unsharp,fade=t=in:st=0:d=4,fade=t=out:st=60:d=4 -c:v libx264 -tune film -preset veryslow -crf 8 -x264opts fast_pskip=0 -c:a pcm_s16le output-stab_final

x265

Example command showing the defaults when libx265 is invoked without any parameters (Constant Rate Factor encoding):

ffmpeg -i input -c:v libx265 -crf 28 -preset medium -c:a libvorbis output.mp4

See FFmpeg H.265/HEVC Video Encoding Guide for more information.

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 output.mpg

Encode to DVD MPEG-2 at ~24 FPS:

$ ffmpeg -i video.VOB -target film-dvd output.mpg

Extracting

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

  • Inspect a file to determine if it contains a subtitle stream:
$ ffprobe -hide_banner 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)'''
  • has an embedded SSA subtitle which can be extracted into an independent file:
$ ffmpeg -i foo.mkv foo.ssa

Add to save subtitles in desirable format, e.g. SubRip:

$ ffmpeg -i foo.mkv -c:s srt foo.srt

When dealing with multiple subtitles, you may need to specify the stream that needs to be extracted using the parameter:

$ ffmpeg -i foo.mkv -map 0:2 foo.ssa

Hardsubbing

(instructions based on HowToBurnSubtitlesIntoVideo at the FFmpeg wiki)

Hardsubbing entails merging subtitles with the video. Hardsubs cannot be disabled, nor language switched.

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

Volume gain

Volume gain can be modified through ffmpeg's filter function. First select the audio stream by using or , then select the volume filter followed by the number that you want to change the stream by. For example:

$ ffmpeg -i input.flac -af volume=1.5 ouput.flac

Here provides a 150% volume gain, instead of 1.5 use for example 0.5 to half the volume. The volume filter can also take a decibel measure, use volume=3dB to increase the volume by 3dB or to decrease it by 3dB.

Tip: To know the current average and peak volume of a file, one can use the volumedetect filter: ffmpeg -i input.flac -af volumedetect -f null -. Then, the difference between the target and the current level can be provided to the volume filter to achieve the desired level.

Volume normalization

A given average and peak volume can also be achieved through normalization using the loudnorm filter. To normalize the perceived loudness of a file using fmpeg's default values for target average, peak and range loudness (respectively -24 LUFS, -2 dBTP and 7 LU), use:

$ ffmpeg -i input.flac -af loudnorm output.flac

To obtain a different loudness profile, use the , and parameters of the filter to indicate respectively the integrated, true peak and loudness range. For example for a higher perceived loudness than the default, use:

$ ffmpeg -i input.flac -af loudnorm=i=-16:tp=-1.5:lra=11:print_format=summary output.flac

In this example, is also added to display the input and output loudness values of the audio file.

Extracting audio

$ ffmpeg -i ''video''.mpg output.ext
...
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 () 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 () 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 aac -b:a 192k -ar 96000 -vn output.aac
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
specifies the start point, and  specifies the duration.

Stripping audio

  1. Copy the first video stream () 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 \
-b:a 128k -ar 48000 -ac 2 video.mkv

Splitting files

You can use the copy codec to perform operations on a file without changing the encoding. For example, this allows you to easily split any kind of media file into two:

$ ffmpeg -i file.ext -t 00:05:30 -c copy part1.ext -ss 00:05:30 -c copy part2.ext

Hardware video acceleration

Encoding/decoding performance may be improved by using hardware acceleration API's, however only a specific kind of codec(s) are allowed and/or may not always produce the same result when using software encoding.

VA-API

VA-API can be used for encoding and decoding on Intel CPUs (requires or ) and on certain AMD GPUs when using the open-source AMDGPU driver (requires ). See the FFmpeg documentation or Libav documentation for information about available parameters and supported platforms.

An example of encoding using the supported H.264 codec:

$ ffmpeg -threads 1 -i file.ext -vaapi_device /dev/dri/renderD128 -vcodec h264_vaapi -vf format='nv12|vaapi,hwupload' output.mp4

For a quick reference, a constant quality encoding can be achieved with:

$ ffmpeg -vaapi_device /dev/dri/renderD128 -i input.mp4 -vf 'format=nv12,hwupload' -c:v hevc_vaapi -f mp4 -rc_mode 1 -qp 25 output.mp4

If using hevc_vaapi, tune -qp between 25 (visually identical) and more (28 starts to have very small visual loss). If using h264_vaapi, tune between 18 (visually identical) and more (20 starts to have very small visual loss). Also, hevc_vaapi seems to encode 50% faster than h264_vaapi.

NVIDIA NVENC/NVDEC

NVENC and NVDEC can be used for encoding/decoding when using the proprietary NVIDIA driver with the package installed. Minimum supported GPUs are from 600 series, see Hardware video acceleration#NVIDIA for details.

This old gist provides some techniques. NVENC is somewhat similar to CUDA, thus it works even from terminal session. Depending on hardware NVENC is several times faster than Intel's VA-API encoders.

To print available options execute ( may also be available):

$ ffmpeg -help encoder=h264_nvenc

Example usage:

$ ffmpeg -i source.ext -c:v h264_nvenc -rc constqp -qp 28 output.mkv

Intel QuickSync (QSV)

Intel® Quick Sync Video uses media processing capabilities of an Intel GPU to decode and encode fast, enabling the processor to complete other tasks and improving system responsiveness.

This requires a libmfx runtime implementation to be installed. libmfx is a dispatcher library that loads an implementation at runtime based on the underlying hardware platform. When running under Broadwell to Rocket Lake GPUs, it will load as the runtime implementation. When running under Alder Lake and newer GPUs, libmfx will load . The runtime implementation cannot be changed or chosen on systems with a single Intel GPU, and the corresponding implementation should be installed following the hardware where it will run.

Failure to install said runtime will result in errors like the following:

[AVHWDeviceContext @ 0x558283838c80] Error initializing an MFX session: -3.
Device creation failed: -1313558101.

The usage of QuickSync is describe in the FFmpeg Wiki. It is recommended to use VA-API with either the iHD or i965 driver instead of using libmfx directly, see the FFmpeg Wiki section Hybrid transcode for encoding examples and Hardware video acceleration#Configuring VA-API for driver instructions.

AMD AMF

AMD added support for H264 only video encoding on Linux through AMD Video Coding Engine (GPU encoding) with the AMDGPU PRO proprietary packages, and ffmpeg added support for AMF video encoding, so in order to encode using the h264_amf video encoder, amf-amdgpu-proAUR is required. You may need to link to the ICD file provided by the AMDGPU PRO packages as a variable or ffmpeg could use the open AMDGPU's ICD file and not be able to use this video encoder. An example of a command for encoding could be as follows:

$ VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/amd_pro_icd64.json ffmpeg -hwaccel auto -vaapi_device /dev/dri/renderD128 -i input.mkv -c:v h264_amf -rc 1 -b:v 8M h264_amf_8M.mp4

For a quick reference, a constant quality encoding can be achieved with:

$ VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/amd_pro_icd64.json ffmpeg -hwaccel auto -vaapi_device /dev/dri/renderD128 -i input.mp4 -c:v h264_amf -f mp4 -rc 0 -qp_b 22 -qp_i 22 -qp_p 22 -quality 2 output.mp4

Tune the three -qp_(b|i|p) together being 18 visually identical and 22 starting to have very small visual loss.

Animated GIF

Whilst animated GIFs are generally a poor choice of video format due to their poor image quality, relatively large file size and lack of audio support, they are still in frequent use on the web. The following command can be used to turn a video into an animated GIF:

$ ffmpeg -i input.mp4 -vf "fps=10,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop -1 output.gif

See http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html for more information on using the palette filters to generate high quality GIFs.

Preset files

Populate with the default preset files:

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

Create new and/or modify the default preset files:

Using preset files

Enable the -vpre option after declaring the desired

libavcodec-vhq.ffpreset

  • = Name of the vcodec/acodec
  • = Name of specific preset to be called out
  • = FFmpeg preset filetype suffix

Tips and tricks

Reduce verbosity

Use a combination of the following options to reduce verbosity to the desired level:

  • : prevents ffmpeg from outputting its copyright notice, build options and library versions
  • -loglevel: modulates verbosity (fine-tuning options are available), e.g.
  • -nostats: disables printing of encoding progress/statistics

Output the duration of a video

$ ffprobe -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 file.ext

Output stream information as JSON

$ ffprobe -v quiet -print_format json -show_format -show_streams file.ext

Create a screen of the video every X frames

$ ffmpeg -i file.ext -an -s 319x180 -vf fps=1/100 -qscale:v 75 %03d.jpg
gollark: ÆÆÆÆAa
gollark: I mean, the C11 ones seem like an inelegant version of the second one.
gollark: Roughly, the ability to have types/containers containing any type, and to write code parametrized by types.
gollark: no.
gollark: Anyway, I checked and it seems like C11 generics are NOT what I meant by "generics".

See also

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.