How to Install Comskip on a Raspberry Pi

The raspberry Pi makes a good platform for watching Live TV. Software packages like TVHeadend, OSMC/Kodi, NextPVR and Plex allow you to turn a normal Pi into a powerful media centre.

Recording your favourite TV shows is great, as is being able to skip through commercial breaks on playback. Every time an ad break arrives, just press “fast forward” a few times and hey presto, you are watching the next part of the show. You still have to actually press the button though, and sometimes rewind too, if the break is overshot. However, there are software tools that will skip the breaks for you, and one of these is Comskip.

This procedure explains how to install Comskip on a Raspberry Pi, with examples pertaining to TVHeadend. You might have come here from my earlier article “How to Watch and Record Live TV on a Raspberry Pi“, to which this article is an effective continuation. Or you might have some other existing system to which you want to add comskip. Even if you are using a different backend, like Plex, this article should still be useful.

Procedure

Preparation

Install some software tools. Type the following command to install three basic packages which will be used later on.

$ sudo apt-get update
$ sudo apt-get -y install autoconf automake libtool

Install dependencies. This step takes about 5 minutes.

$ sudo apt-get -y install -y git build-essential libargtable2-dev libavformat-dev libsdl1.2-dev libsdl2-dev ffmpeg

Lastly, install git. Git will later be used to download the comskip source code.

$ sudo apt-get -y install git

That completes the preparation of the environment.

Download an Compile Comskip

Its time to build and install comskip. Proceed as follows to pull the software from git and build the package.

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

The compilation will run for a few minutes, printing many messages as it goes, and finally creating the binary file “comskip”. Run the following command to move the binary into /usr/local/bin and complete the installation.

$ make install

Test Comskip

Test comskip by running it on a TV recording file. This example uses a recording of the show “Pawn Stars”, broadcast recently in the UK on the Freeview channel “Blaze”.

$ /usr/local/bin/comskip "Pawn Stars.2020-01-30.15-00.ts"
Comskip 0.82.010, made using ffmpeg
 Donator build
 The commandline used was:
 /usr/local/bin/comskip "Pawn Stars.2020-02-22.17-00.ts"
 No INI file found in current directory.  Searching PATH…
 INI file found at comskip.ini
 No INI file found anywhere!!!!
  0:34:56 - 101658 frames in 968.49 sec(104.97 fps), 1.00 sec(95.00 fps), 99%%
 101742 frames decoded in 969.33 seconds (104.96 fps)
 Commercials were found.

Note that the original recording (.ts) file is not modified in any way. All comskip does is create an additional file, having the same name but with a “.txt” suffix.

$ ls -l
-rw-r--r-- 1 pi pi 455395220 Mar 15 16:04 'Pawn Stars.2020-02-22.17-00.ts'
-rw-r--r-- 1 pi pi       111 Mar 16 10:43 'Pawn Stars.2020-02-22.17-00.txt'
$
$ cat  'Pawn Stars.2020-02-22.17-00.txt'
 FILE PROCESSING COMPLETE  52491 FRAMES AT  2500
 1       4753
 21317   29674
 43137   51119
 52076   52491

The .txt file is just a few lines long, showing the frame counts for commercial breaks. The example shows commercials from the start of the recording (frame 1) to when the show started (frame 4753), then an ad break from frame 21317 to frame 29674, a second ad break, followed by commercials after the end of the show at 52076.

Media centre softwares like Kodi and OSMC understand and honour comskip .txt files. Watch the above recording in OSMC, and every commercial break will be skipped. When the skip happens, a message appears in the TV screen for a few seconds saying, for example “Commercial 5:11”, indicating that an ad break of five minutes and 11 seconds has just been skipped.

Install a Comskip ini File

In the above example, notice the messages about a missing “ini” file. An “ini” file is a small text file containing some parameter settings designed to optimize the operation of the program to obtain more accurate skipping of commercials.

The ini file I am using currently was downloaded from the comskip forums. However, I have found that even with no ini file, comskip works well for most channels broadcast on Freeview in the UK. Depending on what country you live in, you might well find a suitable ini file the tuning section of the comskip forums, or just by searching the net. Or you might find, as I did, that the default behaviour is good enough.

Integrating Comskip with your TV/Media Centre

Most media centre systems allow a post processing script to be called after after a TV recording is made. For example, TVHeadend. To have all of your TVheadend recordings automatically processed by comskip, proceed as follows.

Download the Post Processing Script

Download this postprocess.sh script from github:

$ wget https://github.com/webtaster/PostRecord/raw/master/postrecord.sh
$ chmod 755 postrecord.sh
$ sudo mv postrecord.sh /usr/local/bin

The script, now installed in /usr/local/bin, is designed to run comskip after each TV recording and send you an email afterwards, containing information about the recorded program itself and the performance of comskip. If your Pi is not set up to send email, either edit the script and remove the mail command, or enable the system for email by following my earlier article “Enable Outgoing Mail on the Raspberry Pi“.

Configure TVHeadend

Log into the TVHeadend web interface. Among the tabs, select Configuration -> Recording. Where it says “Post-processor command”, enter the following string

/usr/local/bin/postrecord.sh "%t" "%s" "%d" "%c" "%p" "%g" "%i" "%Z" "%e" "%f"

The script will be called with every subsequent TV recording. A log file is also maintained at /home/osmc/scripts/log, detailing program run.

Performance of Comskip

In testing, comskip runs on a Raspberry Pi 2 at roughly 100 frames per second, although there is a lot of variation. It will take about 10 minutes to process a 35 minute TV show. By contrast, my quad core i7 laptop runs at over 1000 FPS.

Having said that, the Pi handles the load pretty well. Comskip is multi-threaded, and will take between 100% and 200% cpu usage, according to “top“. In other words it uses between 1 and 2 of the Pi’s 4 cores. TV viewing can be carried on at the same time without any perceptible slow down or ill effects, as it is handled well by those remaining two or three cores, even with both TVHeadend and OSMC/Kodi running in the same Pi 2.

Performance is likely to be slightly faster on a Pi 3, faster still on a Pi 4 but very slow on a Pi 1 or Pi zero.

Efficacy of Comskip

In testing, comskip efficiently identifies commercial breaks in all UK Freeview channels, except for ITV2. On that channel, breaks are missed or (worse) found where none exist – your viewing of the recording is then interrupted when it skips forward several minutes for no reason.

The postprocess.sh script could be modified to ignore ITV2 recordings, and/or other troublesome channels, but has not been modified, as you might have more success.

Batch Processing

If comskip works well on your system, you might wish to have it process a large number of existing TV recordings.

To process all recordings in the current directory, a command such as the following is suggested. Here, 39 episodes of “Pawn Stars” would be processed. First, check the command:

$ cd "/home/osmc/Recordings/Pawn Stars"
$ ls *ts | wc -l
 39
$ ls *ts | awk '{print "comskip \"" $0 "\""}' | head -3
 comskip "Pawn Stars.2020-02-05.15-00.ts"
 comskip "Pawn Stars.2020-02-05.15-30.ts"
 comskip "Pawn Stars.2020-02-05.20-00.ts"

Then run the 39 comskip commands. On a Pi 2 this could take over 6 hours

$ ls *ts | awk '{print "comskip \"" $0 "\""}' | sh

To obtain a faster conversion, I shut down the Pi running OSMC, removed the thumb drive where recordings were stored and put it into a Linux laptop running Linux Mint 19.3 After installing comskip on that (the procedure is the same as above), the batch conversions ran about 10 times faster than on the Pi, ie. just over 30 minutes instead of 6 hours. It was thus possible to process hundreds of recordings in a reasonable time.

Acknowledgements

I hope the above procedure is useful. It was derived from this traxstar 3 Reddit post and the comskip readme documentation, but with dependency changes for the Raspberry Pi – basically installing software tools and replacing libavformat-ffmpeg-dev with libavformat-dev.

14 thoughts on “How to Install Comskip on a Raspberry Pi

  1. Pingback: How to Watch and Record Live TV with a Raspberry Pi | Unix etc.

  2. Hi. Great guide, thanks. Relatively unskilled at linux but I’ve followed your guide to successfully install comskip on Lubuntu linux running TVHeadend and Kodi.

    Re the following line in your guide:
    “The script will be called with every subsequent TV recording. A log file is also maintained at /home/osmc/scripts/log, detailing program run.”

    I don’t have a /home/osmc/scripts folder. Presumably I can change the path by creating one and editing the postprocess bash script to reflect the path I’ve chosen??

    Thanks again.

    • Hi Rich,

      Yes, exactly. Change this line, near the start of the script:

      LOG=/home/osmc/scripts/log

      to wherever you want the log to be.

      Cheers,
      Jim

  3. Hi There, thank you for this guide, I believe I’m close. Two questions, one is how do I edit the script as I’m using Raspian and not OSMC? Secondly how do I go about downloading a ini onto the Pi? Thanks in advance!

    • Hi Brian
      Editing a script is the same on Raspbian as it is on OSMC. Just use your favourite editor, such as “vi” or “nano”. Both are installed by default.

      To download the INI, follow the instructions under the section entitled “Install a Comskip INI file”. For example, browse to this link from your PC (or from the PI if you are logged into the Raspbian graphical desktop) to this link http://www.kaashoek.com/comskip/viewtopic.php?f=2&t=439#

      and copy the contents of the file you see there. Then paste it into an editor on the pi (ssh to the pi from your PC, or open an editor if you’re on the Pi graphical desktop), and save the file.

      Hope that helps
      Cheers,
      Jim

  4. Nice guide!

    Do you have a suggestion to auto run a video compression app after comskip that will compress the video to something reasonable [6 GB per hour is excessive for long term storage] and cut out the what comskip flags? Ideally, keep the audio in AC3 format and make the output file ~ 1GB.

    Thanks!

    • Hi Jay, thanks. For standard definition (SD) recordings, I get files sizes of about 500 MB/hour. For high definition (HD), about 2 GB/hour. This is for Freeview broadcasts in the UK.

      6 GB/hour would indeed be excessive. I am not sure why your files are that big. Unfortunately I don’t know of a compression app, or if it is possible to compress recordings without a loss of quality.

      Cheers,
      Jim

      • You can indeed compress but tvheadend will still keep looking for the original filename associated with the recording so you’d have to output it back to where it was. I’m still prototyping this but ffmpeg can encode to x264 to keep comparable video quality and you can choose to copy the original audio stream (or compress that if you wish too). I’m also choosing to copy the subtitle stream so it is embedded in the compressed file too.

        /usr/bin/ffmpeg -i %f -c:s copy -vcodec libx264 -acodec copy /home/hts/x264/%b

        • Hi Disgruntled. Interesting. Though if Jay was getting 6 GB/hour media files, I think there is some other issue with his setup.

          Jim

  5. Thank you for writing up this guide.

    Do you know ihow to adapt this to work in the latest release of OSMC (Aug 2023) on an RPi3? I attempted to install the dependencies and received an error that some packages could not be installed. Specifically, these packages have unmet dependencies:

    rpb2-mesa-osmc : Conflicts: libegl1, libgl1, and libg1x0

    Do you have any suggestions on how to make this work?

    Thanks again!

    • Hi Kevin, I don’t have a proven process for the latest release of OSMC.

      Personally, I switched to libreelec some time ago, due to poor support for OSMC on the Raspberry Pi. I recommend libreelec for watching and recording live TV.

      Thanks,
      Jim

      • Thank you, Jim.

        I’ve had great success with it and learned earlier today that the next release of OSMC will fix the issue reported above.

        • Just to qualify my last comment: I had good experience using OSMC to watch/record TV for years, on a Pi2. After migrating to a Pi4, issues with support (for hardware de-interlacing IIRC on that platform) made me switch to Libreelec. But they are both good.

          Jim

  6. Pingback: Install Docker on Raspberry Pi | Unix etc.

Leave a Reply

Your email address will not be published. Required fields are marked *

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