Setting up Comchap’s Comcut with ComSkip, ffmpeg, and HandbrakeCLI

I’ve been running Plex for years now and with their addition of Plex DVR last year I started recording many of the television shows I’d been acquiring other ways before. It saves on bandwidth, it saves on time spent searching for the right quality from a trusted group, but what it doesn’t save on is space. An hour show can easily run 6GB if recorded at 1080p. To help with that I’ve implemented a two stage strategy.

  1. Remove the commercials, which take up almost a third of a show.
  2. Compress the show a bit with x264 using Handbrake.

First we’ll need to cover exactly what we’re going to need:

  1. A linux server which will do the compression for you — I’m running Ubuntu
  2. git — we’ll be getting a few libraries from github repos.
    1. ComSkip: https://github.com/erikkaashoek/Comskip
    2. ComChap: https://github.com/BrettSheleski/comchap
  3. enough hard drive space to do all this — 20GB should work for a work space.

Ok, so first things first. We’ll want to make sure that your server is set up to build code as well as some other dependencies.

For my version of Ubuntu I installed the following:

$ sudo apt install -y git handbrake-cli build-essential libargtable2-dev libavformat-ffmpeg-dev libsdl1.2-dev autoconf automake libass-dev libfreetype6-dev libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo zlib1g-dev

Next, we’ll want to ensure we have a version of ffmpeg that will work. I’ve personally run into issues using ffmpeg which shipped with my release of Ubuntu (I’m running 16.04.02). The simplest way that I’ve found to get around this is to compile ffmpeg from source. I followed the directions here: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

Some things to keep in mind. If you’re going to need to download the sources for any of the dependencies in that guide instead of just installing them via the package manager, you’ll probably want to make sure that you’re getting the latest version of that library instead of what’s listed in the guide. Some of those libraries seem quite old.

After ffmpeg is installed, it should reside in your users’ ~/bin folder. If you want, you can link/copy it to somewhere more accessible if more than that user need to use it (something like /usr/local/bin?)

Now we’re ready for the main event.

First, grab ComSkip from the github repository:

$ cd ~/
$ git clone git://github.com/erikkaashoek/Comskip
$ cd Comskip
$ ./autogen.sh
$ ./configure
$ make

This will create a comskip executable in the Comskip directory. For other applications to use it we’ll need to move it into a directory which is typically in everyone’s $PATH. I copied mine to /usr/local/bin/comskip. Remember to check and make sure it’s executable (chmod +x comskip)

Second, we’ll grab the Comchap library from github as well:

$ cd ~/
$ git clone git://github.com/BrettSheleski/comchap

Third, we’ll need to get a script to tie this all together.

$ nano convertTStoMKV.sh

#!/bin/bash
PATH=$PATH:/usr/local/bin
searchDir=$1
fileExt="ts"

if [ -d "$searchDir" ]
then
find "$searchDir" -name "*.$fileExt" -type f -exec sh -c '
file="$0"
fileDir=$(dirname "$file")
fileName=$(basename "$file")
fileNameNoExt="${fileName%.*}"
tmpFileDir="/your/path/to/the/working/directory"
newFileExt="mkv"
newFileName="$fileNameNoExt.$newFileExt"
workingFilePath="$tmpFileDir/$fileName"
tmpFilePath="$tmpFileDir/$newFileName"
finalDestination="/your/path/to/where/the/file/is/dropped"
echo "moving file to work dir: $file"
mv "$file" "$workingFilePath"
if [ $? -eq 0 ]
then
echo "Running ComCut on file: $workingFilePath"
/your/path/to/comchap/comcut "$workingFilePath"
echo "processing: $workingFilePath"
HandBrakeCLI -e x264 --encoder-preset "faster" -q 24 -2 -T -E copy -B 192 -5 -i "$workingFilePath" -o "$tmpFilePath"
if [ $? -eq 0 ]
then
echo "Moving file to final destination: $finalDestination/$newFileName"
mv "$tmpFilePath" "$finalDestination/$newFileName"
if [ $? -eq 0 ]
then
echo "Removing old file: $workingFilePath"
rm "$workingFilePath"
fi
fi
fi
' {} ';'
else
echo "dir doesn't exist: $searchDir"
fi

Remember to update all the relevant paths. I’ve made some of them generic to explain what they are. Once that’s done, then set up the script to run at some point every night (I used cron) and all the new .TS files will have their commercials cut, be compressed and converted to .MKV files.

Published by Dan

I'm a technophile at heart. I love tech and how it can make our lives better. Let's take a little trip into my mind :P

Join the Conversation

2 Comments

  1. The libavformat-ffmpeg-dev package included below fails to install. Thoughts?

    $ sudo apt install -y git handbrake-cli build-essential libargtable2-dev libavformat-ffmpeg-dev libsdl1.2-dev autoconf automake libass-dev libfreetype6-dev libsdl2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev pkg-config texinfo zlib1g-dev

  2. I hope this will help someone. I used the code posted here and modified my PostProcessing.sh file that I use for NextPVR. The file goes into the /scripts folder of your NextPVR installation. I’m running Linux and had a difficult time finding relevant information. I wrote this and it works! It uses comskip to skip the commercials, comcut to remove them from the file and handbrake to reduce the filesize.

    PostProcessing.sh
    #!/bin/bash
    # ————————–
    # ParallelProcessing.sh
    # D.Little, C. Swiney
    # Verson 101 – 2nd Linux shell translation
    # dated 02/08/2022
    # ————————–

    echo _____________________________________________________________________________________________________________________ >>/var/opt/nextpvr/logs/postprocessing.log
    echo $(date “+%d/%m/%Y %T”) – $3 – Recording started on $1 recorded channel $2 on tuner $4 >>/var/opt/nextpvr/logs/postprocessing.log

    #Make work directory writable from any PC so files can be copied, written to etc.
    workdir=$(dirname “$1”)
    chmod 777 “$workdir”
    /usr/bin/nice -n 19 /usr/local/bin/comskip –ts –ini=/etc/comskip.ini “$1″ > /var/opt/nextpvr/logs/”comskip-${5}.log”
    /home/clint/comchap/comcut “$1″
    file=”$0”
    fileName=$(basename “$1″)
    fileNameNoExt=”${fileName%.*}”
    newFileExt=”mkv”
    newFileName=”$fileNameNoExt.$newFileExt”
    outputFilePath=”$workdir/$newFileName”
    /usr/bin/HandBrakeCLI -e x264 –encoder-preset “faster” -q 24 -2 -T -E copy -B 192 -i “$1” -o “$outputFilePath”
    if [ $? -eq 0 ]
    then
    echo “Removing ts file: $1”
    rm “$1”
    fi
    # End of script
    exit 0

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.