RemoveExtraAudioStreams

From MythTV Official Wiki
Jump to: navigation, search

The following script can be setup as a User Job to automatically remove extra audio tracks from a recording. This script will work with 0.26 and 0.27 and possibly others.

I wrote this script to address a problem I have with transcoded recordings not selecting the correct audio track on playback. It appears that mythtranscode reverses the order of audio tracks in recordings with two or more audio tracks. On some recordings this causes mythTV (and external players) to play the wrong track (ie. a track other than the first track from the original recording). This script can also be used to select an audio track other than the first by editing the code as indicated for language, format, etc.

I run this script before running mythtranscode to remove commercials (mpeg2 lossless transcode) on recordings that I plan to move to the Video Library and keep for an extended period of time.

Mike Stucky 20:50, 16 January 2014 (UTC)

The idea for the script came from a question by Brenden Conte about selecting the "best" audio stream from recordings with multiple audio streams. John Pilkington pointed Brenden to scripts he has written including http://www.mythtv.org/wiki/MythDVBcut. John's script does a lot more than just select audio streams and is not optimized for use as a User Job, so I used parts of MythDVBcut along with mythffmpeg to just remove audio streams. Here is the thread where I posted the original version of this code:

http://www.gossamer-threads.com/lists/mythtv/users/547560#547560

Please note that this script will save the original file as ${FILENAME}.old in the recordings directory ${RECDIR}. Running the mythtranscode job to remove commercials automatically removes this file (at least it does on my system ;^). However, if you don't run mythtranscode (the recording is damaged or you decide you don't want to keep the recording), you will want to periodically remove these files to save space. If you want to remove them automatically, you can uncomment this line near the end of the script:

rm -f "$FILENAME".old

Mike Stucky 17:44, 18 January 2014 (UTC)


Script.png rmExtraAudioStream.sh

#!/bin/bash -e

# 2014 Michael Stucky
# Script that works as a MythTV USERJOB to remove unwanted audio tracks from
# recordings. Create a USERJOB of the form:
# /<path to script>/rmExtraAudioStream.sh %DIR% %FILE%
# Where "rmExtraAudioStream.sh" is the name of this script

RECDIR=$1
FILENAME=$2

# Sanity checking, to make sure everything is in order.
if [ -z "$RECDIR" -o -z "$FILENAME" ] ; then
    echo "Usage: "$0" <RecordingDirectory> <FileName>"
    echo "<FileName> is an mpeg2 file recorded by MythTV with a valid DB entry."
    echo "e.g. 1234_20100405123400.mpg in the <RecordingDirectory>"
    echo "The output replaces the input which is renamed to <FileName>.old"
    exit 0
fi
if [ ! -f "$RECDIR/$FILENAME" ]; then
    echo "File does not exist: $RECDIR/$FILENAME"
    exit 1
fi

# exit if .old file exists
if  [ -f "$RECDIR/$FILENAME".old ] ; then
    exit 1
fi

cd $RECDIR

# Is it an mpeg-2 recording?

mythffmpeg -i "$FILENAME" 2>&1 | grep -B 2 -A 4 "mpeg2video (Main)" | tee "temp$$.txt"
mpeg=$(grep -c "mpeg2video (Main)" temp$$.txt)
if [ $mpeg != 1 ] ; then
    # Not mpeg2video (Main), or no or multiple video streams
    cd ~
    exit 1
fi

aud=$(grep -c "Audio" temp$$.txt)
if [ $aud = 1 ] ; then
    # Only one audio stream, no processing required
    rm -f temp$$.txt
    cd ~
    exit 0
fi

#################################
# This code was taken from John Pilkington's mythDVBcut script and
# will need to be edited if you require something other than the first
# video stream and the first audio stream.
#################################
#
##    Examples
##    Stream #0.0[0xe0]: Video: mpeg2video, yuv420p, 720x576 [PAR 64:45 DAR 16:9], 15000 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
##    Stream #0.1[0xc0](deu): Audio: mp2, 48000 Hz, 2 channels, s16, 256 kb/s
#
#   # Thanks to Christopher Meredith for the basic parsing magic here.

VSN=`grep "mpeg2video (Main)"  temp$$.txt | head -n1 | cut -f 1,1 -d'[' | sed 's+.*\#++g'`

# It has to be tweaked for multiple audio streams.  This (with head -n1 )
# selects the first listed by ffmpeg.
# You may alternatively wish to select for language, format, etc.
# May be channel, programme, user dependent.
ASN=`grep Audio  temp$$.txt | head -n1 | cut -f 1,1 -d'[' | sed 's+.*\#++g'`

#####################################

# Now do the actual processing

mv  "$FILENAME" "$FILENAME".old

# use Mythffmpeg to copy the streams.
ionice -c3 mythffmpeg -f mpegts-ffmpeg -i "$FILENAME".old -map "$VSN" -map "$ASN" -vcodec copy -acodec copy -f mpegts "$FILENAME"

# rebuild seek table and update filesize
ionice -c3 mythcommflag -q --rebuild --file "$FILENAME" --logpath /var/log/mythtv

rm -f temp$$.txt
#rm -f "$FILENAME".old

cd ~

exit 0