summaryrefslogtreecommitdiff
path: root/two-pass.sh
diff options
context:
space:
mode:
authorSteph Enders <steph@senders.io>2024-12-11 23:19:40 -0500
committerSteph Enders <steph@senders.io>2024-12-11 23:21:59 -0500
commit4bdc8d645f52e2c7d9e826c38ce7abd53eae79ca (patch)
tree45abac4a2c75ecf918af89732e27edd8068c408f /two-pass.sh
Create script to two-pass render a video file to a target sizemain
pass in the input, output, and target size to the script. Sizes are in the form of: 100M, 1G, 1500k
Diffstat (limited to 'two-pass.sh')
-rwxr-xr-xtwo-pass.sh59
1 files changed, 59 insertions, 0 deletions
diff --git a/two-pass.sh b/two-pass.sh
new file mode 100755
index 0000000..e9e3e15
--- /dev/null
+++ b/two-pass.sh
@@ -0,0 +1,59 @@
+#!/usr/bin/env bash
+set -x
+INPUT=$1 # input file
+OUTPUT=$2 # target file
+SIZE=$3 # [0-9]+[GgMmKk]
+TEMP=$(mktemp)
+DURATION=$(mediainfo --Inform="Video;%Duration%" $INPUT)
+durS=$(($DURATION/1000))
+if [[ "$SIZE" =~ [0-9]+[Gg] ]]; then
+ gb=${SIZE/[Gg]/}
+ kb=$(($gb * 8 * 1024 * 1024))
+fi
+if [[ "$SIZE" =~ [0-9]+[Mm] ]]; then
+ mb=${SIZE/[Mm]/}
+ kb=$(($mb * 8 * 1024)) # doing 1M = 1000K just to build in some overhead
+fi
+if [[ "$SIZE" =~ [0-9]+[Kk] ]]; then
+ k=${SIZE/[Kk]/}
+ kb=$((k * 8))
+
+fi
+if [ ! $kb ]; then
+ echo "unable to calculate bitrate"
+ exit
+fi
+audiosize=$((160*durS))
+target=$(($kb-$audiosize))
+bitrate=$(($target/$durS))
+
+
+ffmpeg -i $1 \
+ -y \
+ -f mp4 \
+ -g 15 \
+ -movflags +faststart \
+ -preset ultrafast \
+ -threads 9 \
+ -c:v libx264 \
+ -b:v ${bitrate}k \
+ -pass 1 \
+ -an \
+ -passlogfile $TEMP \
+ /dev/null
+
+ffmpeg -i $1 \
+ -y \
+ -b:a 160k \
+ -c:a aac \
+ -ac 2 \
+ -f mp4 \
+ -g 15 \
+ -movflags +faststart \
+ -preset ultrafast \
+ -threads 9 \
+ -c:v libx264 \
+ -b:v ${bitrate}k \
+ -pass 2 \
+ -passlogfile $TEMP \
+ $2