I use ffmpeg in a VM instance running Ubuntu to encode some videos I downloaded from various streaming sites to HEVC. Currently I use a bash script to convert all these files in a directory. Overall Bitrate of these videos varies between 300 kb/s to 1500 kb/s. In my test using the same CRF for all these videos produced either bigger output file in the case of high bitrate videos or low quality video in the case of low bitrate. Currently I manually check the bitrate and move similar bitrate files to a directory and change CRF accordingly each and everytime. This is the command I use to retrieve the bitrate:
find . -name "*.mp4" -print0 | xargs -0 -i{} sh -c " echo -n '{} ' && ffmpeg -i '{}' 2>&1 | sed -n -e 's/^.*bitrate: //p' "
This is the bash script.
#!/bin/bash -e
for i in ~/ffmpeg/*.mp4;
do
ffmpeg -i "$i" -c:v libx265 -crf 26 -c:a libopus -b:a 48k -vbr on -compression_level 10 -frame_duration 60 -application audio "${i%.*}.mkv"
mv "${i%.*}.mkv" ~/ffmpeg/hevc
rm -f -- "$i"
done
How can I change the CRF dynamically based on a range of bitrates in this bash script? Like, if overall Bit Rate above 950 = crf 26, if Overall Bit Rate between 750 to 949 = crf 24, if Overall Bit Rate between 500 to 749 = crf 22, if overall Bit Rate below 499 = crf 18.