Putting this into a repo

This commit is contained in:
Neale Pickett 2023-02-28 21:46:21 -07:00
commit 71c712796c
3 changed files with 163 additions and 0 deletions

21
LICENSE.md Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2023 Neale Pickett
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. In no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising from,
out of or in connection with the software or the use or other dealings in the
software.

10
README.md Normal file
View File

@ -0,0 +1,10 @@
This is a shell script to transcode stuff to h.264.
It uses the Raspberry Pi's hardware acceleration
(`h264_v4l2m2m` codec)
to encode the h.264 files.
I don't know if the ffmpeg code is immature,
or if I just don't understand ffmpeg,
but this codec demands some special treatment,
which this shell script provides.

132
h264 Executable file
View File

@ -0,0 +1,132 @@
#! /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 \
-c:v copy \
-map 0:a \
-c:a copy \
-map 1:s? \
-c:s copy \
-y \
"$out"
echo rm "$intermediate"
done