From 6256a037d33db3e9363e108bd32a33ac71f0fd1b Mon Sep 17 00:00:00 2001 From: tiyn Date: Sat, 18 Mar 2023 03:53:57 +0100 Subject: [PATCH] ffmpeg: target size encoding added --- wiki/linux/ffmpeg.md | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/wiki/linux/ffmpeg.md b/wiki/linux/ffmpeg.md index bf6d436..52b656b 100644 --- a/wiki/linux/ffmpeg.md +++ b/wiki/linux/ffmpeg.md @@ -1,6 +1,6 @@ -# ffmpeg +# FFmpeg -[ffmpeg](https://www.ffmpeg.org) is a free and open-source suite consisting of +[FFmpeg](https://www.ffmpeg.org) is a free and open-source suite consisting of many audio and video tools and libraries. ## Usage @@ -32,3 +32,34 @@ ffmpeg -f concat -safe 0 -i files.txt -map 0 -c copy output.mp4 If the video files you want to concatenate are not mp4 files change the above command accordingly. + +### Two-Pass Encoding + +Two pass encoding - as described in the +[official FFmpeg documentation](https://trac.ffmpeg.org/wiki/Encode/H.264#twopass) +- uses two passes. +The first pass analyzes the input data and outputs a descriptor file. +The second pass actually encodes the data. +The following is an example where the file `input` is encoded with `libx264` to +`mp4` video with `libfdk_aac` audio. +The video bitrate is `555k` and the audio bitrate is `128k`. + +```sh +ffmpeg -y -i input -c:v libx264 -preset medium -b:v 555k -pass 1 -c:a libfdk_aac -b:a 128k -f mp4 /dev/null && \ +ffmpeg -i input -c:v libx264 -preset medium -b:v 555k -pass 2 -c:a libfdk_aac -b:a 128k output.mp4 +``` + +### Encode Audio/Video to Target Size + +For the encoding of a file to a target size the target bitrate of the output +video is needed. +An explanation of this was given by +[aergistal on Stack Overflow](https://stackoverflow.com/questions/29082422/ffmpeg-video-compression-specific-file-size). +This can easily be done with the calculation `bitrate = target size / duration` +in Bits/Second. +Afterwards the encoding can be done by using Two-Pass Encoding as explained in +[a previous section](#two-pass-encoding). +Note that the bitrate for videos is split amongst a bitrate for video and a +bitrate for audio. +The target bitrate has to be equal to or greater than the sum of both video +bitrate and audio bitrate.