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> -to <HH:MM:SS> \
-c:a copy -c:v copy -y <output>
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.
Using the copy
option for the audio and video streams prevents ffmpeg
from re-encoding the whole thing.
This makes the operation very fast.
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.
For comments, please send me an e-mail.
Related articles
- vid2mkv
- vid2mp4
- Making ringtones with free software tools
- Comparing stock ffmpeg with optimized ffmpeg
- Using Matroška containers for videos