Script to repackage recordings for Plex commercial skipping


#1

I want to share a bash script I wrote to repackage TV recordings for Plex so you can utilize the skip commercials on the Plex side. I have only tested this on Mac environment and it is very rough script with minimal error checking.

I also read, but can't find that Channels devs are working on post record webhook/trigger? This would be great to allow this to run after a recording has finished. Right now the script is run against the comskip directory and processes everything there. It tries to determine if a recording has already been processed and skips that one. If not create a mp4 container with chapter markers for recording and then replace the original recording in channels.

I have plex pointing to my recording directories, so after the replace plex 'just' picks up the updated file.

I am open to thoughts and suggestions.

#!/bin/bash

ROOT_DIR="/Volumes/ChannelsDVR/recordings/Logs/comskip/"
FFMPEG="/usr/local/bin/FFMPEG"
MEDIAINFO="/usr/local/bin/mediainfo"
MVCMD="/bin/mv"
LINKCMD="/usr/bin/readlink"

for i in `ls -d $ROOT_DIR*/`
do
   echo "Processing Directory $i"

   echo "Checking for Media Type"

   OUTPUT=`$MEDIAINFO ${i}video.mpg | grep MPEG-TS`
   if [ $? -eq 0 ]
   then
  echo "$? This needs to be processed."
  sleep 1;
  
  echo "Creating video file with commercial markers."
  `$FFMPEG -v quiet -stats -i ${i}video.mpg -i ${i}video.ffmeta -c copy ${i}video.mp4`
  
  if [ $? -eq 0 ]
  then
     echo "Moving Video File Into Place."
     LINKEDFILE=`$LINKCMD ${i}video.mpg`
     `$MVCMD -f ${i}video.mp4 "$LINKEDFILE"`
  else
     echo "Error Adding Channel Markers To Video File."
     sleep 5;
     continue;
  fi
   else
  echo "$? This has already been processed."
  sleep 1;
   fi
done

#2

I do this by processing the files after Channels puts a .edl file in the recording directory, and then I move the file (after processing) to a new directory that Channels has as a secondary location.

Also, ewww you're using `` and not $() :stuck_out_tongue:


#3

How do you know that the edl file has been dropped into the comskip directory? Is that a sure fire way to know that the recording is finished and is ok to process?

OG Backtick


#4

You can use this to have the edl file put next to your videos:

curl -XPUT http://127.0.0.1:8089/dvr/hacks/edl

After that just a quick test if there's an edl file in my for loop:

EDL_FILE="${file%.*}.edl"
...
if [ ! -f "${EDL_FILE}" ]; then
  continue
fi
...

#5

Thanks.

Is there a way to trigger this when an edl is written to the directory? Otherwise I guess I would have a scheduled task run every hour or at sometime overnight to ‘catch’ the new recordings. I want to try to get closer to real-time when a recording ends.


#6

I use an app on my Mac called Hazel to trigger based on the EDL file being there and if it's before 6pm. I don't want it moving things that I'm currently watching but behind on (i.e. if I started it late or just paused it for an extended period of time), but if you never watch live then you could easily just have it trigger off an EDL being there.


#7

So I created this rule to look for new edl files 1 level under comskip. Would this be right? Also, can you pass variables to shell scripts that Hazel runs?

00%20PM


#8

Since I use the EDL hack, I don't look in that folder, I look in the 'ChannelsDVR/TV' for a new EDL file and then process my script if one's found.
image image

And I think the only variable it passes is $1, which would be the file it's processing.


#9

Thanks for you help. I think I got mine running from the comskip directory. Here is my Hazel rule and script. BTW no more backticks, :stuck_out_tongue_winking_eye:

17%20AM

45%20AM

ROOT_DIR=$(dirname $1)/
FFMPEG="/usr/local/bin/FFMPEG"
MEDIAINFO="/usr/local/bin/mediainfo"
MVCMD="/bin/mv"
LINKCMD="/usr/bin/readlink"
DIRNAMECMD="/usr/bin/dirname"

echo "Processing Directory $ROOT_DIR"
echo "Checking for Media Type"

OUTPUT=$($MEDIAINFO ${ROOT_DIR}video.mpg | grep MPEG-TS)
if [ $? -eq 0 ]
then
   echo "Creating video file with commercial markers."
   $($FFMPEG -v quiet -stats -y -i ${ROOT_DIR}video.mpg -i ${ROOT_DIR}video.ffmeta -c copy ${ROOT_DIR}video.mp4)
      
   if [ $? -eq 0 ]
   then
      echo "Moving Video File Into Place."
      LINKEDFILE=$($LINKCMD ${ROOT_DIR}video.mpg)
      $($MVCMD -f ${ROOT_DIR}video.mp4 "$LINKEDFILE")
   else
      echo "Error Adding Channel Markers To Video File."
      continue;
   fi
else
   echo "This has already been processed."
fi

#10

How do you get Plex to recognize the .edl file for skipping or chapter selection?


#11

Plex doesn't recognize them, but MrMC and Kodi does (if you use SMB or NFS shares). What I do is a little different than you, in that instead of converting it into a MP4 with chapters I use MKV. After it's done I rename the MKV to mpg and put it on my NAS and set that as an alternate location for Channels (and generally still use Channels). I have Plex pointed to it as well, but instead of automatic chapter skipping like MrMC (or the Channels beta) you have to actually tell the Plex client to skip to the next chapter.

Hope that makes sense...


#12

I noticed my Plex is no longer recognizing chapters in the files any longer. It seems it started about 1 week ago. All my old recordings Plex still recognizes the chapters. Just not new ones. Any ideas? Maybe a Channels server update installed a new version of comskip or ffmpeg?

The new recordings still have chapter information embedded in the file according to command 'media info'.


#13

I tried some debugging and noticed the comskip.ini file has a new parameter
lowres=1. But when I tried to modify the comskip.ini to rerun and see if that was the problem it does get overwritten.

Here was my diff output:

bash-3.2$ diff ./3886/comskip.ini ./4086/comskip.ini
17a18
> lowres=1
\ No newline at end of file

#14

lowres has been in our config for years, it just speeds up the processing.

If your generated files still have chapter markers it seems more like a Plex bug. Maybe it only happens when their new enhanced player is enabled?


#15

I am not sure where the problem is. I have old recordings that plex will recognize the chapter markers. But all new recordings since about a week ago, plex does not recognize the chapter markers. If the enhanced play was the problem, wouldn't all recordings chapter markers not be recognized not just the ones recently recorded?


#16

This was a Plex bug and it has since been fixed in the server release.