Difference between revisions of "Mythbrake"

From MythTV Official Wiki
Jump to: navigation, search
m (one round of typo fixes and some consistency)
Line 1: Line 1:
This Script shall be called as MythTV userjob. It transcodes the DVB recordings (mpeg files) using [http://handbrake.fr Handbrake]. Using [http://mediainfo.sourceforge.net/ Mediainfo] it first checks wether the recording is HDTV.  
+
This script shall be called as MythTV [[User Jobs|user job]]. It transcodes the DVB recordings (mpeg files) using [http://handbrake.fr Handbrake]. Using [http://mediainfo.sourceforge.net/ Mediainfo] it first checks wether the recording is HDTV.  
  
'''HDTV''' will be reencoded with Mpeg4 AVC H264 to save space. All HDTC channels I can recieve here in Germany are without commercials, so I don't bother running myhtcommflag on them. In case your situation differs you'll have to modify the script.
+
'''HDTV''' will be reencoded with Mpeg4 AVC H264 to save space. All HDTV channels I can receive here in Germany are without commercials, so I don't bother running mythcommflag on them. In case your situation differs you'll have to modify the script.
  
'''SDTV''' will have commercials cut out if necessary and will then be transcoded to Mpeg4 ASP (H263, Xvid, DivX). The scipt will check the ChanID of the recording to decide wethere there are commercials, so you have to adapt the Channel-ID List to your setup.
+
'''SDTV''' will have commercials cut out if necessary and will then be transcoded to Mpeg4 ASP (H263, Xvid, DivX). The scipt will check the ChanID of the recording to decide wethere there are commercials, so you have to adapt the list of ChanIDs to your setup.
I use Mpeg4 ASP instead of Mpeg4 AVC on SDTV recordings because the transcoding is much faster and it is easier to cut later on as mythcommflag isn't perfect. For manual cutting afterwards i use [http://avidemux.org/ Avidemux]
+
I use Mpeg4 ASP instead of Mpeg4 AVC on SDTV recordings because the transcoding is much faster and it is easier to cut later on as mythcommflag isn't perfect. For manual cutting afterwards I use [http://avidemux.org/ Avidemux]
  
'''Audio:''' Currently the scipt just takes the first audio stream from the source file and transcodes it to Mp3 and names the stream "Deutsch-Stero". You might want to at least adjust the stream name. I am planing to improve this script to preserver surround sound (AC3). As the script directly accesses the recording file no hacking / patching of mythtv is requiered, this script just needs some logic to detect if surround sound is present.
+
'''Audio''': Currently the scipt just takes the first audio stream from the source file, transcodes it to Mp3 and names the stream "Deutsch-Stereo". You might want to at least adjust the stream name. I am planning to improve this script to preserve surround sound (AC3). As the script directly accesses the recording file no hacking / patching of MythTV is required, this script just needs some logic to detect if surround sound is present.
  
  
Line 21: Line 21:
 
# Written by Ares Drake, ares.drake@gmail.com
 
# Written by Ares Drake, ares.drake@gmail.com
 
# Licenced under GPL v3
 
# Licenced under GPL v3
# This Script shall ben called as MythTV userjob. It transcodes the DVB recordings (mpeg files) using Handbrake. It first checks wether the recording is HDTV. If so it will be reencoded with H264 to save space. SDTV will have commercials cut out if necessary and will then be transcoded to H263 (Xvid, DivX).  
+
# This Script shall be called as MythTV user job. It transcodes the DVB recordings (mpeg files) using Handbrake. It first checks whether the recording is HDTV. If so it will be reencoded with H.264 to save space. SDTV will have commercials cut out if necessary and will then be transcoded to H.263 (Xvid, DivX).  
 
#
 
#
 
#USAGE:######################
 
#USAGE:######################
#This Sript shall be called as a MythTv user job like the following:
+
# This Sript shall be called as a MythTV user job like as follows:
#/path/to/umwandeln.sh "%FILE%" "%SUBTITLE%" "%CHANID%" "%STARTTIME%" "%TITLE%"
+
# /path/to/umwandeln.sh "%FILE%" "%SUBTITLE%" "%CHANID%" "%STARTTIME%" "%TITLE%"
 
#############################
 
#############################
 
#
 
#
 
#
 
#
 
#REQUIREMENTS################
 
#REQUIREMENTS################
# U need to have the following programs installed:
+
# You need to have the following programs installed:
 
# mediainfo: http://mediainfo.sourceforge.net/
 
# mediainfo: http://mediainfo.sourceforge.net/
 
# handbrake with dependencies: http://www.handbrake.fr
 
# handbrake with dependencies: http://www.handbrake.fr
Line 38: Line 38:
  
  
###Define some basic varibles
+
###Define some basic variables
 
scriptstarttime=$(date +%F-%H%M%S)
 
scriptstarttime=$(date +%F-%H%M%S)
 
file="$1"
 
file="$1"
Line 52: Line 52:
 
chown mythtv:mythtv "$logfile"
 
chown mythtv:mythtv "$logfile"
 
chmod a+rw "$logfile"
 
chmod a+rw "$logfile"
errormail="youremail@adress.com" # this email adress will be informed in case of errors
+
errormail="youremail@adress.com" # this email address will be informed in case of errors
mythrecordingsdir="/some/path" # specify directory where mythtv stores its recordings
+
mythrecordingsdir="/some/path" # specify directory where MythTV stores its recordings
 
outdir="/where/you/want/it" # specify directory where you want the transcoded file to be placed
 
outdir="/where/you/want/it" # specify directory where you want the transcoded file to be placed
 
filename=$title.mkv" # can be customized
 
filename=$title.mkv" # can be customized
Line 64: Line 64:
  
 
###Do some logging
 
###Do some logging
echo "Transcodejob $title starting at $scriptstarttime" >> "$logfile"
+
echo "Transcode job $title starting at $scriptstarttime" >> "$logfile"
 
echo "Original file: $mythrecordingsdir/$file" >> "$logfile"
 
echo "Original file: $mythrecordingsdir/$file" >> "$logfile"
 
echo "Target file: $outfile" >> "$logfile"
 
echo "Target file: $outfile" >> "$logfile"
echo "ChanId: $chanid Zeit: $starttime" >> "$logfile"
+
echo "ChanId: $chanid Time: $starttime" >> "$logfile"
 
####################
 
####################
#get width: u need mediainfo installed, see mediainfo.sourceforge.net
+
#get width: you need mediainfo installed, see mediainfo.sourceforge.net
 
width=$(mediainfo --Inform="Video;%Width%" "$mythrecordingsdir/$file")
 
width=$(mediainfo --Inform="Video;%Width%" "$mythrecordingsdir/$file")
 
   if [ $? != 0 ]
 
   if [ $? != 0 ]
Line 82: Line 82:
 
     exit 1
 
     exit 1
 
     else
 
     else
     echo "Mediainfo Run successfull." >> "$logfile"
+
     echo "Mediainfo Run successful." >> "$logfile"
 
   fi
 
   fi
 
fullmediainfo=(mediainfo $mythrecordingsdir/"$file")
 
fullmediainfo=(mediainfo $mythrecordingsdir/"$file")
Line 94: Line 94:
 
# width >=1280
 
# width >=1280
 
# currently this can only be ARD HD, ZDF HD or ARTE HD, so no commercials
 
# currently this can only be ARD HD, ZDF HD or ARTE HD, so no commercials
# Userjob for HD-TV: Re-Encode in AVC H264: saves space, but keeps H264, x264 via HandbrakeCLI
+
# Userjob for HDTV: Re-Encode in AVC H.264: saves space, but keeps H.264, x264 via HandbrakeCLI
 
if [ $width -ge 1280 ]
 
if [ $width -ge 1280 ]
 
   then
 
   then
Line 120: Line 120:
 
elif [ $width -le 720 ]
 
elif [ $width -le 720 ]
 
   then
 
   then
  # this is SD-TV, so it could be either with or without commercials. We check for commercials by comparing to chanid list.
+
  # this is SDTV, so it could be either with or without commercials. We check for commercials by comparing to ChanID list.
  
 
     if [ $chanid == 3007 -o $chanid == 29014 -o $chanid == 30014 -o $chanid == 30107 ]
 
     if [ $chanid == 3007 -o $chanid == 29014 -o $chanid == 30014 -o $chanid == 30107 ]
     #chanid without commercials: 3007 3sat; ZDF info; 29014 & 3014 ZDF neo; ZDF theater; 30107 BayrFS Nord; BayrFS Süd; SWR BW; RBB Berlin; WDR Köln; BR alpha; SR
+
     #ChanID without commercials: 3007 3sat; ZDF info; 29014 & 3014 ZDF neo; ZDF theater; 30107 BayrFS Nord; BayrFS Süd; SWR BW; RBB Berlin; WDR Köln; BR alpha; SR
 
       then  
 
       then  
 
       #This is a channel without commercials
 
       #This is a channel without commercials
Line 145: Line 145:
 
exit 1
 
exit 1
 
else
 
else
echo "Transcode Run successfull." >> "$logfile"     
+
echo "Transcode Run successful." >> "$logfile"     
 
       fi
 
       fi
  
Line 175: Line 175:
 
exit 1
 
exit 1
 
else
 
else
echo "Transcode Run successfull." >> "$logfile"
+
echo "Transcode Run successful." >> "$logfile"
 
       fi     
 
       fi     
 
     fi
 
     fi

Revision as of 17:05, 29 November 2011

This script shall be called as MythTV user job. It transcodes the DVB recordings (mpeg files) using Handbrake. Using Mediainfo it first checks wether the recording is HDTV.

HDTV will be reencoded with Mpeg4 AVC H264 to save space. All HDTV channels I can receive here in Germany are without commercials, so I don't bother running mythcommflag on them. In case your situation differs you'll have to modify the script.

SDTV will have commercials cut out if necessary and will then be transcoded to Mpeg4 ASP (H263, Xvid, DivX). The scipt will check the ChanID of the recording to decide wethere there are commercials, so you have to adapt the list of ChanIDs to your setup. I use Mpeg4 ASP instead of Mpeg4 AVC on SDTV recordings because the transcoding is much faster and it is easier to cut later on as mythcommflag isn't perfect. For manual cutting afterwards I use Avidemux

Audio: Currently the scipt just takes the first audio stream from the source file, transcodes it to Mp3 and names the stream "Deutsch-Stereo". You might want to at least adjust the stream name. I am planning to improve this script to preserve surround sound (AC3). As the script directly accesses the recording file no hacking / patching of MythTV is required, this script just needs some logic to detect if surround sound is present.



If you find any bugs or problems feel free to mail me at ares.drake@gmail.com



#!/bin/bash
# Save this as umwandeln.sh and make the file executable
# Written by Ares Drake, ares.drake@gmail.com
# Licenced under GPL v3
# This Script shall be called as MythTV user job. It transcodes the DVB recordings (mpeg files) using Handbrake. It first checks whether the recording is HDTV. If so it will be reencoded with H.264 to save space. SDTV will have commercials cut out if necessary and will then be transcoded to H.263 (Xvid, DivX). 
#
#USAGE:######################
# This Sript shall be called as a MythTV user job like as follows:
# /path/to/umwandeln.sh "%FILE%" "%SUBTITLE%" "%CHANID%" "%STARTTIME%" "%TITLE%"
#############################
#
#
#REQUIREMENTS################
# You need to have the following programs installed:
# mediainfo: http://mediainfo.sourceforge.net/
# handbrake with dependencies: http://www.handbrake.fr
#############################




###Define some basic variables
scriptstarttime=$(date +%F-%H%M%S)
file="$1"
subtitle="$(echo "$2" | sed 's/(//g' | sed 's/)//g' | sed 's/://g')"
#| sed 's///_/g' | sed 's/?//g' | sed s/"'"//g
chanid="$3"
starttime="$4"
title="$(echo "$5" | sed 's/(//g' | sed 's/)//g' | sed 's/:/_/g' | sed 's%/%_%g')"
#| sed 's/"/"/_/g' | sed 's/?//g' | sed s/"'"/""/g
logdir="/opt/mythtv/transcodelogs"
logfile="$logdir/$scriptstarttime-$title.log"
touch "$logfile"
chown mythtv:mythtv "$logfile"
chmod a+rw "$logfile"
errormail="youremail@adress.com" # this email address will be informed in case of errors
mythrecordingsdir="/some/path" # specify directory where MythTV stores its recordings
outdir="/where/you/want/it" # specify directory where you want the transcoded file to be placed
filename=$title.mkv" # can be customized
if [ -f "$outdir/$filename" ]
 # do not overwrite outfile, if already exists, change name
 then
 filename="filename-$scriptstarttime"
fi
outfile="$outdir/$filename"

###Do some logging
echo "Transcode job $title starting at $scriptstarttime" >> "$logfile"
echo "Original file: $mythrecordingsdir/$file" >> "$logfile"
echo "Target file: $outfile" >> "$logfile"
echo "ChanId: $chanid Time: $starttime" >> "$logfile"
####################
#get width: you need mediainfo installed, see mediainfo.sourceforge.net
width=$(mediainfo --Inform="Video;%Width%" "$mythrecordingsdir/$file")
  if [ $? != 0 ]
    # There were errors with Mediainfo. 
    then
    scriptstoptime=$(date +%F-%H:%M:%S)
    echo "Error prior to encoding at $scriptstoptime" >> "$logfile"
    echo "Mediainfo encountered an error. Maybe mediainfo is not installed, or not in your path" >> "$logfile"
    echo "Mediainfo encountered an error. Maybe mediainfo is not installed, or not in your path"
    mail -s "Mythtv Mediainfo Error on $HOSTNAME" "$errormail" < "$logfile"
    mv "$logfile" "$logfile-FAILED"
    exit 1
    else
    echo "Mediainfo Run successful." >> "$logfile"
  fi
fullmediainfo=(mediainfo $mythrecordingsdir/"$file")
##############################################################



### Transcoding starts here, in 3 differend versions: HDTV w/o commercials, SDTV w/ and w/o commercials.


# width >=1280
# currently this can only be ARD HD, ZDF HD or ARTE HD, so no commercials
# Userjob for HDTV: Re-Encode in AVC H.264: saves space, but keeps H.264, x264 via HandbrakeCLI
if [ $width -ge 1280 ]
  then
  echo "Userjob HD-TV starts because of with of $width" >> "$logfile"
  # $HDCMDLINE is the commandline for HDTV
  HDCMDLINE='HandBrakeCLI -q 20.0 -e x264 -r 25 -a 1 -A Deutsch-Stereo -E MP3 -B 128 -R 48 --mixdown dpl2 -f mkv --crop 0:0:0:0 -d -m -x b-adapt=2:rc-lookahead=50:ref=3:bframes=3:me=umh:subme=8:trellis=1:merange=20:direct=auto -i "$mythrecordingsdir/$file" -o "$outfile" 2>> "$logfile"'
  HandBrakeCLI -q 20.0 -e x264 -r 25 -a 1 -A Deutsch-Stereo -E MP3 -B 128 -R 48 --mixdown dpl2 -f mkv --crop 0:0:0:0 -d -m -x b-adapt=2:rc-lookahead=50:ref=3:bframes=3:me=umh:subme=8:trellis=1:merange=20:direct=auto -i "$mythrecordingsdir/$file" -o "$outfile" 2>> "$logfile"
  if [ $? != 0 ]
	# There were errors in the Handbrake Run. 
	then
	scriptstoptime=$(date +%F-%H:%M:%S)
	echo "Transcoding-Error at $scriptstoptime" >> "$logfile"
	echo "Interrupted file $outfile" >> "$logfile"
	echo "###################################" >> "$logfile"
	echo $fullmediainfo >> "$logfile"
	mail -s "Mythtv Transcoding Error on $HOSTNAME" "$errormail" < "$logfile"
        mv "$logfile" "$logfile-FAILED"
	exit 1
	else
	echo "Transcode Run successfull." >> "$logfile"
      fi


#width <= 720
elif [ $width -le 720 ]
  then
 # this is SDTV, so it could be either with or without commercials. We check for commercials by comparing to ChanID list.

    if [ $chanid == 3007 -o $chanid == 29014 -o $chanid == 30014 -o $chanid == 30107 ]
    #ChanID without commercials: 3007 3sat; ZDF info; 29014 & 3014 ZDF neo; ZDF theater; 30107 BayrFS Nord; BayrFS Süd; SWR BW; RBB Berlin; WDR Köln; BR alpha; SR
      then 
      #This is a channel without commercials
      echo "Userjob SD-TV ohne Werbung startet" >> "$logfile"
      #SD-TV Userjob is encoding to MPEG4 ASP aka DivX aka Xvid via FFMPEG via HandBrakeCLI
      
      # $SDFCMDLINE is the commandline for SDtv-commercialFree
      SDFCMDLINE="/usr/bin/HandBrakeCLI -q 3 -r 25 -a 1 -A Deutsch-Stereo -E MP3 -B 128 -R 48 --mixdown dpl2 -f mkv --crop 0:0:0:0 -d -m -i $mythrecordingsdir/$file -o $outfile" 
      echo "Commandline: $SDFCMDLINE" >> "$logfile"
      HandBrakeCLI -q 3 -r 25 -a 1 -A Deutsch-Stereo -E MP3 -B 128 -R 48 --mixdown dpl2 -f mkv --crop 0:0:0:0 -d -m -i "$mythrecordingsdir/$file" -o "$outfile"  2>> "$logfile"
      if [ $? != 0 ]
	# There were errors in the Handbrake Run. 
	then
	scriptstoptime=$(date +%F-%H:%M:%S)
	echo "Transcoding-Error at $scriptstoptime" >> "$logfile"
	echo "Broken File $outfile" >> "$logfile"
	echo "###################################" >> "$logfile"
	echo $fullmediainfo >> "$logfile"
	mail -s "Mythtv Transcoding Error on $HOSTNAME" "$errormail" < "$logfile"
        mv "$logfile" "$logfile-FAILED"
	exit 1
	else
	echo "Transcode Run successful." >> "$logfile"     
      fi

    else
      # We have a channel with commercials, so flag & cut them out first.
      echo "Userjob SD-TV mit Werbung startet" >> "$logfile"
      /usr/bin/mythcommflag -c "$chanid" -s "$starttime" --gencutlist
      /usr/bin/mythtranscode --chanid "$chanid" --starttime "$starttime" --mpeg2 --honorcutlist
      /usr/bin/mythcommflag --file "$file" --rebuild
      #Finished commercial cutting, following is encoding as above

      #SD-TV Userjob is encoding to MPEG4 ASP aka DivX aka Xvid via FFMPEG via HandBrakeCLI
      # $SDCCMDLINE is the commandline for SDtv-Commercials
      SDCCMDLINE='/usr/bin/HandBrakeCLI -q 3 -r 25 -a 1 -A Deutsch-Stereo -E MP3 -B 128 -R 48 --mixdown dpl2 -f mkv --crop 0:0:0:0 -d -m -i "$mythrecordingsdir/$file" -o "$outfile" 2>> "$logfile"' 
      
      echo "Commandline: $SDCCMDLINE" >> "$logfile"
      HandBrakeCLI -q 3 -r 25 -a 1 -A Deutsch-Stereo -E MP3 -B 128 -R 48 --mixdown dpl2 -f mkv --crop 0:0:0:0 -d -m -i "$mythrecordingsdir/$file" -o "$outfile"  2>> "$logfile"      
      
      if [ $? != 0 ]
	# There were errors in the Handbrake Run. 
	then
	scriptstoptime=$(date +%F-%H:%M:%S)
	echo "Transcoding-Error at $scriptstoptime" >> "$logfile"
	echo "Broken File $outfile" >> "$logfile"
	echo "###################################" >> "$logfile"
	echo $fullmediainfo >> "$logfile"
	mail -s "Mythtv Transcoding Error on $HOSTNAME" "$errormail" < "$logfile"
        mv "$logfile" "$logfile-FAILED"
	exit 1
	else
	echo "Transcode Run successful." >> "$logfile"
      fi    
    fi

#720<width<1280 or error getting width: dunno whats going on here, abort
else
    echo "Error: 720<width<1280, undefined condition, aborting" >> "$logfile"
    echo "###################################" >> "$logfile"
    echo $fullmediainfo >> "$logfile"
    mail -s "Mythtv Transcoding Error on $HOSTNAME" "$errormail" < "$logfile"
    mv "$logfile" "$logfile-FAILED"
    exit 1
fi

  scriptstoptime=$(date +%F-%H:%M:%S)
  echo "Successfully finished at $scriptstoptime" >> "$logfile"
  echo "Transcoded file: $outfile" >> "$logfile"

exit 0