Last active
February 25, 2023 20:42
-
-
Save ClashLuke/0288e3a135995a02148b6778538fe156 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
probe () { | |
ffprobe -v error -show_entries $1 -of default=noprint_wrappers=1:nokey=1 $2 | |
} | |
get_bitrate (){ | |
original=$1 | |
filesize=$2 | |
max_audio_bitrate=${3:-64000} | |
seconds=`probe format=duration $original` | |
total_bitrate=`echo "scale=0; (8 * $filesize/$seconds)" | bc` # filesize in byte, bitrate in bit | |
bitrate=`python3 -c "import sys; sys.stdout.write(str(max($total_bitrate - $max_audio_bitrate, $total_bitrate * 3 // 4))); sys.stdout.flush()"` # filesize in byte, bitrate in bit | |
audio_bitrate=`echo "scale=0; ($total_bitrate - $bitrate)" | bc` | |
file_size=`echo "scale=2; ($total_bitrate * $seconds / 8 / 1024 / 1024)" | bc` | |
echo $bitrate $audio_bitrate | |
# echo | |
# echo "Encoding $seconds seconds of video with" | |
# echo " Video Bitrate: $bitrate" | |
# echo " Audio Bitrate: $audio_bitrate" | |
# echo " Total Bitrate: $total_bitrate" | |
# echo "Final file should be around $file_size MiB" | |
# echo | |
} | |
get_scale (){ | |
original=$1 | |
target_width=$2 | |
width=`probe stream=width $original` | |
height=`probe stream=height $original` | |
python3 -c "import sys; width=min($width, $target_width); sys.stdout.write(f'{width}:{int(width/$width*$height):d}'); sys.stdout.flush()" | |
} | |
encode () { | |
original=$1 | |
format=$2 | |
codec=$3 | |
args=$4 | |
width=$5 | |
filesize=${6:-8000000} # default: 8MByte - 5% overhead | otherwise specified value | |
echo $original $filesize | |
rates=`get_bitrate $original $filesize 64000` | |
echo $rates | |
rates=($rates) | |
echo $rates | |
bitrate=${rates[0]} | |
audio_bitrate=${rates[1]} | |
echo $bitrate $audio_bitrate | |
scale=`get_scale $original $width` | |
ffmpeg -hide_banner -y -i $original -vf "fps=film:round=near,scale=$scale" -c:v $codec $args -b:v $bitrate -pass 1 -an -f $format /dev/null | |
ffmpeg -hide_banner -y -i $original -vf "fps=film:round=near,scale=$scale" -c:v $codec $args -b:v $bitrate -pass 2 -c:a libopus -b:a $audio_bitrate -movflags +faststart -fflags +genpts out.$format | |
} | |
encodecrf () { | |
original=$1 | |
format=$2 | |
codec=$3 | |
crf=$4 | |
args=$5 | |
audio_bitrate=96000 | |
new_name=`python3 -c "import sys; sys.stdout.write('.'.join('$original'.split('.')[:-1]) + '_reencode.$format')"` | |
ffmpeg -hide_banner -y -i $original -crf $crf -c:v $codec $args -c:a libopus -b:a $audio_bitrate -movflags +faststart -fflags +genpts $new_name | |
} | |
encodecrfh265 () { | |
encodecrf $1 mp4 libx265 $2 "-preset slow" | |
} | |
encodecrfh264 () { | |
encodecrf $1 mp4 libx264 $2 "-preset slow" | |
} | |
encodecrfav1 () { | |
encodecrf $1 webm libaom-av1 $2 "" $3 | |
} | |
encodemp4 (){ | |
encode $1 mp4 libx264 "-preset veryslow" $2 $3 | |
} | |
encodeav1 (){ | |
encode $1 webm libaom-av1 "" $2 $3 | |
} | |
encodevp9 (){ | |
encode $1 webm libvpx-vp9 "" $2 $3 | |
} | |
download (){ | |
url=$1 | |
width=${2:-640} | |
size=${3:-8000000} | |
tmp="/tmp/.temp.webm" | |
yt-dlp $url --output $tmp | |
if [ -f $tmp ]; then | |
echo | |
else | |
tmp="${tmp}.mp4" | |
fi | |
echo "Using $tmp as temporary file" | |
encodevp9 $tmp $width $size | |
rm $tmp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment