Skip to content

Instantly share code, notes, and snippets.

@ljsabc
Last active July 4, 2021 08:48
Show Gist options
  • Save ljsabc/56ea05735136246e46bc32c4b2a977f7 to your computer and use it in GitHub Desktop.
Save ljsabc/56ea05735136246e46bc32c4b2a977f7 to your computer and use it in GitHub Desktop.
Standalone bash script for batch iPhone video conversion
#!/bin/bash
# Q: upper bitrate limit: 1/Q
Q=2
for file in `find ./input/ -iname "*.mov"`;
do
echo $file
bitrate=`./ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of csv=p=0 $file`
bitrate=`awk -v var="$bitrate" -v Q="$Q" 'BEGIN {OFMT="%d";print var/Q}'`
echo $bitrate
# ffmpeg for crf
./ffmpeg -y -i $file -vcodec libx265 -pix_fmt yuv420p10le -crf 21 -x265-params "asm=avx512" -preset slow -tag:v hvc1 -profile:v main10 -acodec copy output2.mov
tgt_bitrate=`./ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of csv=p=0 output2.mov`
if [ $tgt_bitrate -lt $bitrate ]
then
mv ./output2.mov ./output/`basename $file`
else
# The crf file is larger than the expectation.
# we need a better rate control. Invoke the 2pass.
./ffmpeg -y -i $file -vcodec libx265 -pix_fmt yuv420p10le -b:v $bitrate -x265-params "pass=1:asm=avx512" -preset slow -tag:v hvc1 -profile:v main10 -acodec copy -f mov tmp
./ffmpeg -y -i $file -vcodec libx265 -pix_fmt yuv420p10le -b:v $bitrate -x265-params "pass=2:asm=avx512" -preset slow -tag:v hvc1 -profile:v main10 -acodec copy output.mov
mv ./output.mov ./output/`basename $file`
fi
./exiftool -tagsFromFile $file -ExtractEmbedded -all:all -overwrite_original ./output/`basename $file`
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment