Skip to content

Instantly share code, notes, and snippets.

@xDShot
Forked from CallumDev/discordencode.sh
Last active September 13, 2022 03:54
Show Gist options
  • Save xDShot/142428f8fe8a2f2ea19a7582f3534a43 to your computer and use it in GitHub Desktop.
Save xDShot/142428f8fe8a2f2ea19a7582f3534a43 to your computer and use it in GitHub Desktop.
Bash script to encode videos with ffmpeg to under discord's 8MiB file limit
#!/bin/bash
# discordencode - encodes a video file to under 8MiB for Discord
usage () {
echo "Usage: [NOAUDIO=1] [VPRESET=x264-preset] discordencode input output.mp4"
}
if [ -z "$1" ]
then
usage
exit 1
fi
if [ -z "$2" ]
then
usage
exit 1
fi
if [ ! -f "$1" ]; then
echo "$1 does not exist"
exit 2
fi
# Bitrate math functions
# Bc and ffprobe are a bit arcane
duration_seconds () {
ffprobe -i "$1" -show_entries format=duration -v quiet -of csv="p=0"
}
subtract_and_truncate () {
echo "x = $1; scale = 0; x / 1 - $2" | bc -l
}
divide_floats() {
echo "scale=2; $1 / $2" | bc
}
audioargs="-c:a aac -b:a 128k"
audiobits=128
if [[ $NOAUDIO ]] && [ "$NOAUDIO" -eq "1" ]
then
audioargs="-an"
audiobits=0
echo "Using no audio"
fi
if [ -z "$VPRESET" ]
then
VPRESET=veryslow
fi
duration=$(duration_seconds "$1")
echo $1
echo Duration is $duration
kbits=$(subtract_and_truncate $(divide_floats 45473.1 $duration) $audiobits)
echo Video bitrate is $kbits kb/s
set -x
ffmpeg -y -i "$1" -c:v libx264 -preset $VPRESET -threads 0 -b:v ${kbits}k -pass 1 -an -f mp4 /dev/null
ffmpeg -i "$1" -c:v libx264 -preset $VPRESET -threads 0 -b:v ${kbits}k -pass 2 $audioargs "$2"
@N72826
Copy link

N72826 commented Sep 13, 2022

Appreciate you for forking and modifying it for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment