From d968d203480e6ba7ef5485ae21c186faea4a05dc Mon Sep 17 00:00:00 2001 From: Cody Cook Date: Thu, 30 May 2019 22:38:07 -0700 Subject: [PATCH] More features --- ts-to-mkv.sh | 106 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 11 deletions(-) diff --git a/ts-to-mkv.sh b/ts-to-mkv.sh index 5e5af78..2839ad9 100644 --- a/ts-to-mkv.sh +++ b/ts-to-mkv.sh @@ -1,27 +1,105 @@ -#!/bin/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") +FFMPEG="/data/sourcecode/ffmpeg/ffmpeg-4.1.3-amd64-static/ffmpeg" +if [[ ! -f "$FFMPEG" ]]; then + FFMPEG=$(/usr/bin/env which ffmpeg) +fi -echo "[info] convert .ts to .mkv; v1 start" +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:hk:p: 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 ;; + h) + echo "[help] -d, path to custom ffmpeg (default: $FFMPEG)" + echo "[help] -k, keep the original ts file" + echo "[help] -p, custom path to scan recursively (default: $WORKPATH)" + exit 0;; + k) + echo "[debug] keeping the original file" + keepsource="1";; + p) + if [[ -e "$OPTARG" ]]; then + WORKPATH="$OPTARG" + echo "[debug] custom pathing: $OPTARG" + else + echo "[debug] no custom pathing; using $WORKPATH" + fi ;; + esac +done + +shift $(($OPTIND-1)) echo "[info] finding files" -filelist="$(find ./ -name "*.ts")" +filelist="$(find $WORKPATH -name "*.ts")" if [[ "$filelist" ]]; then for i in $filelist; do - if [[ "$i" == "./*/*.ts" ]] || [[ "$i" == "./*.ts" ]] || [[ "$i" == "./*/*/*.ts" ]]; then - echo "[exit] no files found." - exit 0 - fi - sleep 2 echo "[info] working on $i" INFILE="$i" echo "[debug] Infile: $INFILE" OUTFILE="${i//.ts/.mkv}" echo "[debug] Outfile: $OUTFILE" sleep 2 - INFILE2="${INFILE//\'/\'\\\\\\\'\'}" - /data/sourcecode/ffmpeg/ffmpeg-4.1.3-amd64-static/ffmpeg \ + "$FFMPEG" \ -i "${INFILE}" \ -f lavfi -i movie="'${INFILE2}'[out+subcc]" \ -map 0 -map 1:s \ @@ -33,9 +111,15 @@ if [[ "$filelist" ]]; then 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] PROBLEM FOUND IN $INFILE, moving on..." + echo "[error] $INFILE is corrupted, abandoning..." fi done else