DVR Post Processing Script MKV to MP4 (HDHomeRun EXTEND)

I’m having issues getting to work a DVR Post Processing Script on my Mac. Below is the script

#!/bin/sh

if [ ! -z "$1" ]; then 
   FILENAME=$1
   TEMPFILENAME="$(mktemp)"
   ffmpeg -i "$FILENAME" -vcodec copy -acodec copy "$TEMPFILENAME"
   rm -f "$FILENAME"
   mv -f "$TEMPFILENAME" "$FILENAME"
   chmod 777 "$FILENAME"
   echo "Done!"
fi

I saved the file at the user level and indicated so at the Plex DVR settings:

/Users/mmc/DVR2MP4.sh

After a recording, the video gets copied as it normally did before to the TV Shows section but in MKV, not the expected MP4 file.

If I do the conversion manually via terminal and FFmpeg the video gets converted successfully and it works through Plex server and clients.

Please help!

Try specifying the full paths to ffmpeg, possibly rm/mv/chmod also. The path environment may be different between your user and when plex user runs the script. I haven’t done this in awhile but worth a shot. I used to use a command to find the path “which ffmpeg” and it would show you where the file you’re executing is located. Then you could update your script to /usr/local/bin/ffmpeg instead of just ffmpeg (for example.)

Thanks for the tip. I’ll try it and report back in a couple days (Out for the holidays :slight_smile: Happy 4th!

Your script doesn’t do anything to account for the change in file extension. The variable “filename” therefore still has the .MKV extension and when you do the move command back at the end the file is renamed with that extension. You need to do a step to break the file name into parts. Namely the path, the name, and the extension. Then in the move step of the script concatenate the path and filename together with the new file extension that is expected.

You both were right. I got the code working off-Plex (in terminal) thanks to @mavrrick 's advice but later it did not work in Plex. Then I added the full path to FFmpeg following @rbuecker 's suggestion and BINGO!

Below is the final script (I’m not an expert so the code might not be as polished but it works!)

#!/bin/sh

if [ ! -z "$1" ]; then 

origFile=$1
fnameMKV=`basename $origFile`
fnameMP4=${fnameMKV%mkv}mp4
tempFile="/Volumes/DVR/zTEMP/$fnameMP4"

/usr/local/bin/ffmpeg -i "$origFile" -vcodec copy -acodec copy "$tempFile"

rm -f "$origFile"
mv -f "$tempFile" "${origFile%mkv}mp4"

echo "Done!"

fi

A couple tips for other folks. This MKV to MP4 script works only with the HDHomeRun EXTEND using any compression other than the ORIGINAL QUALITY. If you have a different DVR device not cable of direct H264 recording it will not work on Plex unless you modify the FFmpeg command to do a full conversion. Also, make sure to replace the “/Volumes/DVR/zTEMP/” with a path to your own “temp” folder (the use of this temp folder is to minimize Plex auto library scan when changes are detected)

Reporting back that converted videos do play well on my AppleTV but I’m noticing audio out of sync very often, something that did not happened before with the MKVs. When the audio gap becomes too annoying I just stop and resume the video and it gets back in sync.

Below is the MediaInfo for the converted file in hopes that someone can spot the issue

Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 2.85 GiB
Duration                                 : 1 h 59 min
Overall bit rate mode                    : Variable
Overall bit rate                         : 3 403 kb/s
Movie name                               : AMERICAN NINJA WARRIOR
Law rating                               : TV-PG (L)
Writing application                      : Lavf57.56.100

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4
Format settings, CABAC                   : Yes
Format settings, ReFrames                : 2 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 1 h 59 min
Bit rate mode                            : Variable
Bit rate                                 : 3 013 kb/s
Maximum bit rate                         : 12.0 Mb/s
Width                                    : 1 280 pixels
Height                                   : 540 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Variable
Frame rate                               : 29.920 FPS
Minimum frame rate                       : 0.384 FPS
Maximum frame rate                       : 30.303 FPS
Original frame rate                      : 29.970 (30000/1001) FPS
Standard                                 : Component
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.146
Stream size                              : 2.52 GiB (89%)
Color range                              : Limited

Audio
ID                                       : 2
Format                                   : AC-3
Format/Info                              : Audio Coding 3
Format settings, Endianness              : Big
Codec ID                                 : ac-3
Duration                                 : 1 h 59 min
Bit rate mode                            : Constant
Bit rate                                 : 384 kb/s
Channel(s)                               : 6 channels
Channel positions                        : Front: L C R, Side: L R, LFE
Sampling rate                            : 48.0 kHz
Frame rate                               : 31.250 FPS (1536 spf)
Bit depth                                : 16 bits
Compression mode                         : Lossy
Stream size                              : 329 MiB (11%)
Language                                 : English
Service kind                             : Complete Main
Default                                  : Yes
Alternate group                          : 1

Text #1
ID                                       : 1-CC1
Format                                   : EIA-608
Muxing mode                              : SCTE 128 / DTVCC Transport
Muxing mode, more info                   : Muxed in Video #1
Duration                                 : 1 h 59 min
Bit rate mode                            : Constant
Stream size                              : 0.00 Byte (0%)
Encoded stream size                      : 0.00 Byte (0%)
CaptionServiceName                       : CC1

Text #2
ID                                       : 1-1
Format                                   : EIA-708
Muxing mode                              : SCTE 128 / DTVCC Transport
Muxing mode, more info                   : Muxed in Video #1
Duration                                 : 1 h 59 min
Bit rate mode                            : Constant
Stream size                              : 0.00 Byte (0%)
Encoded stream size                      : 0.00 Byte (0%)

I have this script modified to use with HandBrakeCLI and it is working fine as long as i do not have parentheses in the filename. Unfortunately Plex names every DVR recording with the date in parentheses. I am getting the error…

bash: syntax error near unexpected token `(’

The interwebs tell me i need to curly bracket the variables so as to not interpret the () in the files names of said variables. I have put brackets around every variable any way i can think to do it and i still get the same error. Please help and thank you for this thread, it has helped me greatly.

#!/bin/sh

if [ ! -z “$1” ]; then

origFile=$1
fnameMPEG2=basename $origFile
fnameMP4=${fnameMPEG2%ts}mp4
tempFile="/Users/Branden/Movies/PlexTCT/${fnameMP4}"

/Applications/HandBrakeCLI -i “${origFile}” -o “${tempFile}” --preset=“Chromecast 1080p30 Surround”

rm -f “${origFile}”
mv -f “${tempFile}” “${origFile%ts}mp4”

echo “Done!”

fi