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.

1 thought on “How to Install Comskip on a Raspberry Pi

  1. Pingback: How to Watch and Record Live TV with a 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.