LinHES Forums
http://forum.linhes.org/

Fixing Problematic .ts Video files
http://forum.linhes.org/viewtopic.php?f=24&t=24820
Page 1 of 1

Author:  bruce_s01 [ Mon Feb 04, 2019 8:05 am ]
Post subject:  Fixing Problematic .ts Video files

I had a few problematic .ts files that when playing back, would work but would not Fast Forward properly (The built in player would freeze for a bit then exit to the Title Done/Play screen). I tried using my demuxmux script, but that would fail with ProjectX not liking files with a mono sound track. So finally, after playing around with ffmpeg I came up with a couple of scripts, though I don't know if they might be useful for somebody else:

The first one is fixvid.sh which just cleans up the file, and gives the option to reencode the audio from mono mp2 to stereo ac3
Code:
#!/bin/bash
# to fix problematic video files

USAGE="Usage: `basename $0` [-v] [-h] [-m] [-t | -a] -f filename
   -v Verbose
   -h This Message
   -m Change Mapping
   -t Transcode to 3800k, 720x576 192k ac3
   -a Transcode Audio Only to 192k ac3
   -f filename"

VERBOSE=false
OUTPUTDEV="/dev/null"
TEST=false
ISMAP=false
USETCODE=false
ACODE=false



ID=false
PARAMETER=" "

while getopts f:vmath OPTION ; do
   case "$OPTION" in
       f)SOURCEFILE="$OPTARG" ;;
       m)ISMAP=true ;;
       t)USETCODE=true ;;
       a)ACODE=true ;;
       v)VERBOSE=true ;;
       h)echo "$USAGE" ;
        exit 1
        ;;
   esac
done

#Now we have arguments and options, we now need to check
#what parameters have been passed and do we need to process them

#Very little preprocessing for input filename, should really put in some file name
#sanity checking

SOURCENAME=${SOURCEFILE}
TEMPNAME=${SOURCEFILE%.mpg}
SOURCEFILE=${TEMPNAME}
TEMPNAME=${SOURCEFILE%.ts}
SOURCEFILE=${TEMPNAME}


#Add the extensions and the temporary file name part
#SOURCENAME=${TEMPNAME}.mpg
#TEMPFILENAME=${TEMPNAME}.tmp.mpg
OPNAME=${SOURCEFILE}_0.mpg

if [ "$ISMAP" = true ] ; then
   MAP1="0:1"
   MAP2="0:0"
else
   MAP1="0:0"
   MAP2="0:1"
fi

if [ "$VERBOSE" = true ] ; then
   echo "SOURCEfile = $SOURCENAME"
   echo "Output file = $OPNAME"
fi

if [ ! -e "$SOURCENAME" ] ; then
   #Check for the existence of the input file
   echo "$SOURCENAME not found."
   exit 1
fi

if [ "$USETCODE" = true ] && [ "$ACODE" = false ] ; then
   echo "Transcoding Sound + Video"
   ffmpeg -threads 2 -v 1 -i "$SOURCENAME" -r pal -target dvd -b 3800k -s 720x576 -acodec ac3 -ab 192k -ac 2 -copyts -aspect 16:9   "$OPNAME" -map $MAP1 -map $MAP2
elif [ "$USETCODE" = false ] && [ "$ACODE" = false ] ; then
   echo "Fixing File"
   ffmpeg -i "$SOURCENAME"  -acodec copy -vcodec copy "$OPNAME"
elif [ "$USETCODE" = false ] && [ "$ACODE" = true ] ; then
   echo "Reencoding Sound"
   ffmpeg -threads 2 -v 1 -i "$SOURCENAME"  -r pal -target dvd -vcodec copy -acodec ac3 -ab 192k -ac 2  "$OPNAME" -map $MAP1 -map $MAP2
fi

if [ $? -ne 0 ]
then
   #either ran out of space or file too damaged.
   echo "ffmpeg failed!"
   exit 1
fi


exit 0


The next is repvid.sh, which calls fixvid in two different modes and then finishes of with calling demuxmux.sh to tidy up the file.

Code:
#!/bin/bash


USAGE="Usage: `basename $0` [-v] [-c] [-h] -f filename
   -v Verbose
   -c Cleanup Intermediate Files
   -m Change Mapping
   -h This Message
   -f filename"

VERBOSE=false
OUTPUTDEV="/dev/null"
TEST=false
ISMAP=false
ISCLEANUP=false


while getopts f:vcmh OPTION ; do
   case "$OPTION" in
       f)SOURCEFILE="$OPTARG" ;;
       v)VERBOSE=true ;;
       m)ISMAP=true ;;
       c)ISCLEANUP=true ;;
       h)echo "$USAGE" ;
        exit 1
        ;;
   esac
done

SOURCENAME=${SOURCEFILE}
TEMPNAME=${SOURCEFILE%.ts}
SOURCEFILE=${TEMPNAME}


OP1NAME=${SOURCEFILE}_0.mpg
IP2NAME=${SOURCEFILE}_f.mpg
OP2NAME=${SOURCEFILE}_f_0.mpg
IP3NAME=${SOURCEFILE}_a.mpg
INTERVID=${SOURCEFILE}_a.m2v
INTERAUD=${SOURCEFILE}_a.ac3
OP3NAME=${SOURCEFILE}_a_1.mpg

OP4NAME=${SOURCEFILE}_1.mpg

if [ "$VERBOSE" = true ] ; then
   echo "SOURCEfile = $SOURCENAME"
   echo "Output file = $OP1NAME"
   echo "Input file  = $IP2NAME"
   echo "Output file = $OP2NAME"
   echo "Input file  = $IP3NAME"
   echo "Output file = $OP3NAME"
   echo "Inter Vid F = $INTERVID"
   echo "Inter Aud F = $INTERAUD"
   echo "Output file = $OP4NAME"
   
fi


if [ ! -e "$SOURCENAME" ] ; then
   #Check for the existence of the input file
   echo "$SOURCENAME not found."
   exit 1
fi


#Do Inital file Fix
echo "Initial File Fix"

~/bin/fixvid.sh -v  -f "$SOURCENAME"

if [ ! -e "$OP1NAME" ] ; then
   #Check for the existence of the input file
   echo "$OP1NAME not found."
   exit 1
fi

if [ "$VERBOSE" = true ] ; then
   echo "Renaming $OP1NAME to $IP2NAME"
   mv -vf "$OP1NAME" "$IP2NAME"
else
   mv -f "$OP1NAME" "$IP2NAME"
fi

if [ $? -ne 0 ]
then
      #check that move completed
      echo "File Moved failed!"
      exit 1
else
      echo "Move OK"
fi


# Reencode Sound to AC3
echo "Reencoding Audio to AC3"

if [ "$ISMAP" = true ] ; then
   ~/bin/fixvid.sh -v -a -m -f "$IP2NAME"
else
   ~/bin/fixvid.sh -v -a -f "$IP2NAME"
fi

if [ ! -e "$OP2NAME" ] ; then
   #Check for the existence of the input file
   echo "$OP2NAME not found."
   exit 1
fi

if [ "$VERBOSE" = true ] ; then
   echo "Renaming $OP2NAME to $IP3NAME"
   mv -vf "$OP2NAME" "$IP3NAME"
else
   mv -f "$OP2NAME" "$IP3NAME"
fi

if [ $? -ne 0 ]
then
      #check that move completed
      echo "File Moved failed!"
      exit 1
else
      echo "Move OK"
fi

echo "Tiding Up File"
   # Demux & Remux File to finally clean it up
   ~/bin/demuxmux.sh -v -o -c   -f "$IP3NAME"

      
if [ ! -e "$OP3NAME" ] ; then
   #Check for the existence of the output file
   echo "$OP3NAME not found."
   exit 1
fi

if [ "$VERBOSE" = true ] ; then
   echo "Renaming $OP3NAME to $OP4NAME"
   mv -vf "$OP3NAME" "$OP4NAME"
else
   mv -f "$OP3NAME" "$OP4NAME"
fi

if [ $? -ne 0 ]
then
      #check that move completed
      echo "File Moved failed!"
      exit 1
else
      echo "Move OK"
fi

if [ "$ISCLEANUP" = true ] ; then
   if [ "$VERBOSE" = true ] ; then
      echo "Cleaning up Files..."
      rm -v "$IP2NAME"
      rm -v "$IP3NAME"
      rm -v "$INTERVID"
      rm -v "$INTERAUD"
   else
      echo "Cleaning up Files..."
      rm "$IP2NAME"
      rm "$IP3NAME"
      rm "$INTERVID"
      rm "$INTERAUD"
   fi
fi



The final one is just a script to cycle through the files to be fixed:

Code:
#!/bin/bash

for list in  *.ts; do
   echo "File is $list"

   #Do Inital file Fix
   ~/bin/repvid.sh -v -c -f $list
      
   fi
done


Of course, these will have to be modified for a particular workstation, but hope they might be useful for someone.

HTH Bruce S.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/