ts-to-mkv.sh/ts-to-mkv.sh
Cody Cook 80ed1d3156 fix variables for loglevel
dryrun doesn't work, doesn't like /dev/null or null... the examples use -f but this command doesn't... have to reconsider
2019-05-31 16:49:17 -07:00

153 lines
No EOL
4 KiB
Bash

#!/usr/bin/env bash
########################
## functions ##
vercomp () {
if [[ "$1" == "$2" ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
ffmpegcheck(){
ffmpegversion=$("$FFMPEG" -version | head -n1 | awk '{print $3}')
vercomp "$ffmpegversion" "4"
}
##/functions ##
## Env ##
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
loglevel="-loglevel level+info"
FFMPEG="/data/sourcecode/ffmpeg/ffmpeg-4.1.3-amd64-static/ffmpeg"
if [[ ! -f "$FFMPEG" ]]; then
FFMPEG=$(/usr/bin/env which ffmpeg)
fi
ffmpegcheck
if [[ "$?" == "2" ]]; then
echo "[error] ffmpeg version too low, need to use version 4.0.0 or newer."
exit 1
fi
WORKPATH="./"
keepsource="0"
version="2"
##/Env ##
## Start ##
echo "[info] $0 -- v$version"
while getopts d:p:fnXhkq OPT; do
case "$OPT" in
d)
if [[ -e "$OPTARG" ]]; then
FFMPEG="$OPTARG"
echo "[debug] custom daemon: $OPTARG"
else
echo "[debug] no custom daemon; using $FFMPEG"
fi ;;
f)
force="-y"
echo "[debug] overwriting existing files " ;;
h)
echo "[help] -d, path to custom ffmpeg (default: $FFMPEG)"
echo "[help] -f, force overwriting existing files"
echo "[help} -n, never overwrite existing files"
echo "[help] -k, keep the original ts file"
echo "[help] -q, disable output from ffmpeg (default: info)"
echo "[help] -p, custom path to scan recursively (default: $WORKPATH)"
echo "[help] -X, dry run"
exit 0 ;;
k)
echo "[debug] keeping the original file"
keepsource="1" ;;
n)
force="-n"
echo "[debug] not overwriting existing files " ;;
p)
if [[ -d "$OPTARG" ]]; then
WORKPATH="$OPTARG"
echo "[debug] custom pathing: $OPTARG"
else
echo "[debug] no custom pathing; using $WORKPATH"
fi ;;
q)
loglevel="-loglevel quiet"
echo "[debug] disabling ffmpeg output" ;;
X)
dryrun="1"
echo "[debug] dry run enabled";;
esac
done
shift $((OPTIND-1))
echo "[info] finding files"
filelist="$(find "$WORKPATH" -name "*.ts")"
if [[ "$filelist" ]]; then
for i in $filelist;
do
echo "[info] working on $i"
INFILE="$i"
echo "[debug] Infile: $INFILE"
if [[ "$dryrun" == "1" ]]; then
OUTFILE="/dev/null"
echo "[debug] overriding OUTFILE to $OUTFILE"
else
OUTFILE="${i//.ts/.mkv}"
fi
echo "[debug] Outfile: $OUTFILE"
sleep 2
INFILE2="${INFILE//\'/\'\\\\\\\'\'}"
"$FFMPEG" \
-i "${INFILE}" \
-f lavfi -i movie="'${INFILE2}'[out+subcc]" \
-map 0 -map 1:s \
-codec:v libx264 \
-codec:a copy \
-codec:s srt \
-metadata:s:s:0 language=eng \
$force \
$loglevel \
"${OUTFILE}"
exitcode="$?"
if [[ $exitcode -eq 0 ]]; then
echo "[info] successful conversion"
if [[ "$keepsource" == "0" ]]; then
echo "[debug] deleting original file"
rm -v "$INFILE"
else
echo "[info] retaining original file"
fi
else
echo "[error] $INFILE is corrupted, abandoning..."
fi
done
else
echo "[info] no files found."
fi
IFS=$SAVEIFS