#!/bin/sh ##### Plex Export Script for MythTV, updated February 6th, 2015 ### ## Since this script requires the User Job to pass additional arguments under MythTV 0.25, ## use following User Job syntax for userjobs under all versions: ## ## 'removecommercials.sh %DIR% %FILE% %CHANID% %STARTTIMEUTC% "%TITLE%" %SEASON% %EPISODE%' ## ## Initial development: Zwhite, Ricks03, Waxrat @ www.mythtv.org/wiki ## Modifications: Scott Gruby ### ##### VIDEODIR=$1 #MythTV-024 %DIR% FILENAME=$2 #MythTV-024 %FILE% CHANID=$3 STARTTIME=$4 TITLE=$5 SEASON=$6 EPISODE=$7 TEMPDIR="/Users/mediacenter/mythtemp" PLEXDIR="/Users/mediacenter/Plex" # Sanity checking, to make sure everything is in order. if [ -z "$VIDEODIR" -o -z "$FILENAME" -o -z "$CHANID" -o -z "$STARTTIME" -o -z "$TITLE" ]; then echo "Usage: $0 " exit 5 fi if [ ! -f "$VIDEODIR/$FILENAME" ]; then echo "File does not exist: $VIDEODIR/$FILENAME" exit 6 fi # Strip out illegal characters TITLE=`echo ${TITLE} | sed 's/:/ -/g'` TITLE=`echo ${TITLE} | sed 's/\// -/g'` # Create some directories that we need mkdir -p "${PLEXDIR}/${TITLE}/" mkdir -p ${TEMPDIR} OUTPUT_FILENAME="${TITLE} - s${SEASON}e${EPISODE}.mp4" YEAR=`echo ${STARTTIME} | awk '{print substr($0,0,4)}'` MONTH=`echo ${STARTTIME} | awk '{print substr($0,5,2)}'` DAY=`echo ${STARTTIME} | awk '{print substr($0,7,2)}'` ## Make sure a season and episode is set if [ -z "$SEASON" -o -z "$EPISODE"]; then OUTPUT_FILENAME="${TITLE} - ${YEAR}-${MONTH}-${DAY}.mp4" fi if [ "$SEASON" == "0" -o "$EPISODE" == "0" ]; then OUTPUT_FILENAME="${TITLE} - ${YEAR}-${MONTH}-${DAY}.mp4" fi # Cleanup before we start rm -f ${TEMPDIR}/$FILENAME rm -f ${TEMPDIR}/$FILENAME.map rm -f "${PLEXDIR}/${TITLE}/${OUTPUT_FILENAME}" # This requires that commercials are already marked and will make add the cut # list to the video. echo "Generating cutlist..." mythutil --gencutlist --chanid $CHANID --starttime $STARTTIME ERROR=$? if [ $ERROR -ne 0 ]; then echo "Copying cutlist failed for ${FILENAME} with error $ERROR" exit $ERROR fi # Do a lossless transcode with the cutlist to a temp file echo "Transcoding..." mythtranscode --honorcutlist --showprogress --mpeg2 --chanid $CHANID --starttime $STARTTIME -o ${TEMPDIR}/$FILENAME ERROR=$? if [ $ERROR -ne 0 ]; then echo "Transcoding failed for ${FILENAME} with error $ERROR" exit $ERROR fi # For some reason, ffmpeg mangles the duration, so if we grab the duration before doing ffmpeg # and explicitly set it, we don't get 9 hour recordings. DURATION=`ffprobe -show_streams ${TEMPDIR}/$FILENAME | grep duration= | head -1 | sed 's/^duration=\([0-9]*\)/\1/'` # Run ffmpeg using my magic settings /usr/local/bin/ffmpeg -y -vsync passthrough -analyzeduration 999999999 -t ${DURATION} -i ${TEMPDIR}/$FILENAME -codec:v libx264 -profile:v high -preset ultrafast -crf 16 -minrate 30M -maxrate 30M -bufsize 15 -metadata:s:a:0 language=eng -c:a ac3 -b:a 384k -threads 2 ${TEMPDIR}/${FILENAME}.mp4 # Extract the closed captioning and turn it into subtitles /usr/local/bin/ffmpeg -f lavfi -i "movie=${TEMPDIR}/${FILENAME}[out0+subcc]" -map s ${TEMPDIR}/${FILENAME}.srt # If the subtitle file was properly extracted, merge it into the mp4 if [ -f ${TEMPDIR}/${FILENAME}.srt ] then /usr/local/bin/ffmpeg -i ${TEMPDIR}/${FILENAME}.mp4 -f srt -i ${TEMPDIR}/${FILENAME}.srt -metadata:s:s:0 language=eng -c:v copy -c:a copy -c:s mov_text "${PLEXDIR}/${TITLE}/${OUTPUT_FILENAME}" fi # If for some reason the merging of the subtitle file failed, just move the mp4 into the Plex directory if [ ! -f "${PLEXDIR}/${TITLE}/${OUTPUT_FILENAME}" ] then # Move the transcoded file to the right place in Plex mv ${TEMPDIR}/${FILENAME}.mp4 "${PLEXDIR}/${TITLE}/${OUTPUT_FILENAME}" fi # Cleanup rm -rf ${TEMPDIR}/$FILENAME rm -rf ${TEMPDIR}/$FILENAME.srt rm -rf ${TEMPDIR}/$FILENAME.mp4 rm -rf ${TEMPDIR}/$FILENAME.map # This script is run as root, so we need to reset the permissions chown -R mediacenter:staff "${PLEXDIR}/${TITLE}" exit 0