h264/h264

131 lines
2.5 KiB
Plaintext
Raw Normal View History

2023-02-28 21:46:21 -07:00
#! /bin/sh
set -e
usage () { cat <<EOD 1>&2; }
Usage: $0 [OPTIONS...] SRC [SRC...]
Re-encodes to h.264.
Output will be written to \$SRC.h264.mp4
-width W Rescale to no wider than W
-height H Rescale to no taller than H
-1080p Rescale to no bigger than 1080p
-720p Rescale to no bigger than 720p
-480p Rescale to no bigger than 480p
-swenc Software encode instead of hardware
In addition to transcoding video,
this will force any subtitles into mov_text.
This is due to some odd interaction between
ffmpeg, h264_v4l2m2m, and mp4.
EOD
width=iw
height=ih
codec=h264_v4l2m2m
bitrate="-b:v 2048k"
while true; do
case "$1" in
-width|--width)
width=$1
shift
;;
-height|--height)
height=$1
shift
;;
-1080p|--1080p)
width=1920
height=1080
bitrate="-b:v 2048k"
shift
;;
-720p|--720p)
width=1280
height=720
bitrate="-b:v 3000k"
shift
;;
-480p|--480p)
width=640
height=480
bitrate="-b:v 512k"
shift
;;
-swenc|--swenc)
codec=h264
shift
;;
-*)
usage
exit
;;
*)
break
;;
esac
done
if [ "$codec" = "h264" ]; then
bitrate="-crf 28"
fi
log () {
echo "=== $*"
}
cmd () {
nice "$@"
}
# This got a height of 719 (n%2 must = 0)
#-vf "scale='min($width,iw)':'min($height,ih)':force_original_aspect_ratio=decrease,format=yuv420p" \
for fn in "$@"; do
base=${fn%.*}
filename=$(basename "$fn")
intermediate="$base.nut"
out="$base.h264.mkv"
[ -f "$out" ] && continue
case "$fn" in *.h264.mp4) continue ;; esac
# ffmpeg has some bizarro thing where the h264_v4l2m2m codec won't write to a mkv file,
# In addition, some subtitles interfere with it too.
# Solution: transcode video + audio into an intermediate file,
# then add back any subtitles.
log "$filename - Scale/Transcode"
cmd ffmpeg \
-hide_banner \
-loglevel warning \
-stats \
-i "$fn" \
-map 0:v \
-vf "scale=-2:'min($height,ih)':force_original_aspect_ratio=decrease,format=yuv420p" \
-c:v $codec \
$bitrate \
-map 0:a \
-c:a copy \
-num_capture_buffers 16 \
-num_output_buffers 32 \
-y \
"$intermediate"
log "$filename - Add subtitles"
cmd ffmpeg \
-hide_banner \
-loglevel warning \
-stats \
-i "$intermediate" \
-i "$fn" \
-map 0:v \
-map 0:a \
-map 1:s? \
2023-02-28 22:00:45 -07:00
-c copy \
2023-02-28 21:46:21 -07:00
-y \
"$out"
echo rm "$intermediate"
done