Roland's homepage

My random knot in the Web

Video manipulation with ffmpeg

Serveral useful recipes for manipulation video with ffmpeg.

Note that a name within angle brancket (like <this>) represents a generic input or output file.

Extracting raw audio

The following recipe extracts the audio from a video file and converts it to WAV format.

ffmpeg -y -hide_banner -i <input> -vn -c:a pcm_s16le -ac 2 <output.wav>

You can then use e.g. flac or lame to convert this to a more compact format.

Shortening a video

Films that you want to transcode often contain material at the beginning or end that is not very interesting. This snippet shows how to remove that using the seek options of ffmpeg.

ffmpeg -hide_banner -i <input> -ss <HH:MM:SS> -sseof <HH:MM:SS> \
-c:a copy -c:v copy -y <output>

With -sseof you will generally want to use negative values, since it acts from the end of the video. You can use -to instead of -sseof, but then you have to measure from the beginning of the file.

Using the seek options after the input file means that the input is decoded but discarded.

Using the seek options before the input file means that ffmpeg will try to seek in the input file. This might lead to a couple of seconds of video without audio.

Finding and changing the sound volume on a video

Finding the volume.

ffmpeg -y -hide_banner -i <input> -vn -nostats -af "volumedetect" -f null /dev/null

This will result in output that looks like this.

[Parsed_volumedetect_0 @ 0x80b106000] n_samples: 102158336
[Parsed_volumedetect_0 @ 0x80b106000] mean_volume: -29.9 dB
[Parsed_volumedetect_0 @ 0x80b106000] max_volume: -11.0 dB
[Parsed_volumedetect_0 @ 0x80b106000] histogram_10db: 1
[Parsed_volumedetect_0 @ 0x80b106000] histogram_11db: 1245
[Parsed_volumedetect_0 @ 0x80b106000] histogram_12db: 10665
[Parsed_volumedetect_0 @ 0x80b106000] histogram_13db: 31675
[Parsed_volumedetect_0 @ 0x80b106000] histogram_14db: 65625

Changing the volume is done with a filter. Halving the volume is the same as reducing it by 6.0206 dB.

ffmpeg -y -hide_banner -i <input> -af "volume=-6.0206dB" -c:v copy <output>

Note that this also re-encodes the audio stream.

←  Adding an overlay to each page of a pdf file