Jump to content

ComSkipper ... A Emby Plugin that skips commercials


BillOatman

Recommended Posts

reneboulard
22 hours ago, vdrover said:

@reneboulard Mind sharing your preprocessing script?

Find enclosed my preprocessing script.

 

It is very basic but does the work.

It allow you to use a default comskip.ini file or a specific one per show.

Also it will use a previous logo file if available so processing will start faster.

Have fun.

preprocessing.sh

Link to comment
Share on other sites

49 minutes ago, reneboulard said:

It is very basic but does the work.

It allow you to use a default comskip.ini file or a specific one per show.

Also it will use a previous logo file if available so processing will start faster.

preprocessing.sh 3.73 kB · 1 download

TY. I will give it a try.

I love the idea of putting a show-specific .ini file in a show folder!

Edited by vdrover
Link to comment
Share on other sites

Detecting & skipping commercials on Ubuntu 

I wanted to contribute by sharing the details of setting up comskip on Ubuntu 20.04.3 LTS. Other flavors of linux will be similar I suspect. 

I'm a bit new to Emby, so this how-to is very detailed as I was learning and experimenting as i was going. I hope you find it useful!

Step 1 — Preparation

Before starting, you should have Live TV working in Emby and be able to record TV shows. You will also need to know the location of your emby server. Mine is installed in "/opt/emby-server". To check where your is installed, you can use this command from your terminal. 

$ sudo find / -type d -name "emby-server"

And since you will be testing in some steps, it will be helpful to have already recorded a show and know the full path. I will use the example file path below, but you should replace it with the real path to your file:

/mnt/Emby/TV Shows/Seinfeld (1981)/Season 8/Seinfeld S08E05.ts

Step 2 — Install comskip

In this step you will install the "donator version" comskip. I strongly recommend that you support the author my making a donation here

This step does not involve Emby in any way. The instructions were borrowed from here.

  1. From a terminal, enter the following commands.
    $ sudo apt-get update
    $ sudo apt-get install ffmpeg libavcodec-dev libavformat-dev libavutil-dev autoconf automake git libargtable2-dev libtool
    $ git clone git://github.com/erikkaashoek/Comskip
    $ cd Comskip
    $ ./autogen.sh
    $ ./configure
    $ make
    $ sudo make install
  2. Verify that comskip is working by entering the command below. The basic instructions for using comskip should be displayed.
    $ comskip -h
  3. Verify the path of comskip by entering the command below. If different than shown below, be sure to substitute this path when needed in Step 5.
    $ which comskip
    /usr/local/bin/comskip

Step 3 — Create a comskip configuration file 

comskip requires the use of a configuration file to accurately detect commercials. For example, here is a basic configuration file that specifies some parameters:

detect_method=107
output_edl=1
live_tv=1
edl_skip_field=3
max_commercialbreak=300
verbose=0
output_default=0
delete_logo_file=1

These parameters are stored in a file called comskip.ini. You need to create a comskip.ini file and note the full path to this file for use in Step 5.

  1. Download the attached comskip.ini file to your downloads folder.
  2. Copy the file to your /etc folder with the following command:
    $ sudo cp ~/Downloads/comskip.ini /etc
  3. The path of your config file will be:
    /etc/comskip.ini

Step 4 — Test that comskip is working as expected

This step does not involve Emby in any way, but it is important to help troubleshoot any issues later on when we ask Emby to trigger comskip (Step 5).

We will now use the path to your recorded show from Step 1 and the path to the comskip.ini file from Step 3 to test comskip.

  1. From your terminal, enter the following command:
    $ comskip --ini=/etc/comskip.ini '/mnt/Emby/TV Shows/Seinfeld (1981)/Season 8/Seinfeld S08E05.ts'
  2. If comskip is working as expected, the output should be similar to that shown below with a slowly increasing progress indicator. Wait for this process to finish. "Commercials were found" should be the last line of the output.
    Comskip 0.82.010, made using ffmpeg
    Donator build
    The commandline used was:
    	comskip
    	--ini=/etc/comskip.ini
    	"/mnt/Emby/TV Shows/Seinfeld (1981)/Season 8/Seinfeld S08E05.ts"
    
    Setting ini file to /etc/comskip.ini as per commandline
    Using /etc/comskip.ini for initiation values.
    0:41:16 - 148470 frames in 685.90 sec(216.46 fps), 1.00 sec(248.00 fps), 99%
    148549 frames decoded in 704.88 seconds (210.74 fps)
    Commercials were found.
  3. Using your file manager, navigate to the folder containing your recorded show. You should now see a file named "Seinfeld S08E05.edl" (note the EDL filename extension). You can view the file contents to confirm the start and end times (in seconds) for the commercials.
    0.00	77.54	0
    2167.10	2293.04	0

Step 5 — Launch comskip when a recording is complete.

This is where we start to connect Emby and comskip. In this step we will configure the LiveTV section of Emby to launch the comskip application you installed and tested above.

There are two ways to go here: (A) launch comskip when a recording completes, or (B) launch comskip when a recording begins and look for commercials in real time. CHOOSE ONE METHOD OR THE OTHER, NOT BOTH.

Step 5(A) — Post-processing: running comskip when a recording is complete.

  1. From your Emby instance, navigate to the Advanced LiveTV settings as follows: Manage server > Live TV / DVR > Advanced.
  2. Scroll down. Locate the following 2 fields and enter the values shown (see Step 2 for the correct path for comskip and Step 3 for the path to comskip.ini):
    • Post-processing application: /usr/local/bin/comskip
    • Post-processor command line arguments: --ini=/etc/comskip.ini "{path}"
  3. Click the Save button at the bottom to save your changes.
  4. To test that this is working, record a new show. When the recording is complete, wait a few minutes and then check for the presence of a new EDL file in the folder containing the show.
    • The EDL file should be created shortly after the recording completes. However, comskip can take a while to completely process every frame of your video. The longer the video, the longer it will take to finish. Thus, the EDL file may not be finalized for 5 to 30 minutes after the recording is complete. 

Step 5(B) — Live-processing: run comskip in real time during a recording. MASSIVE thanks to @reneboulard for his excellent work on live-processing in Linux.

Live processing requires the installation of the Emby Scripter-X plugin. This allows you to execute scripts and commands when specific events occur such as the start of a recording.

It also requires the use of a bash script (attached) called emby_preprocessing.sh. The attached bash script is very powerful. It logs activity, and allows you to use show-specific comskip.ini files on a per-show basis (see Step 8).

  1. Install the Emby Scripter-X plugin:
    1. From your Emby instance, navigate to the Plugins area as follows: Manage server > Plugins. Click the Catalog tab at the top.
    2. In the General section, locate and click Emby Scripter-X. On the plugin detail page, click the Install button. When installation is complete, reboot your Emby server.
  2. Create and test the bash file: 
    1. Download the attached emby_preprocessing.sh file to your downloads folder. Copy the file to your /usr/local/sbin/ folder with the following command:
      $ sudo cp ~/Downloads/emby_preprocessing.sh /usr/local/sbin/
    2. Make the bash file executable with the following command:
      $ sudo chmod +x /usr/local/sbin/emby_preprocessing.sh
    3. Test that the bash script is working by launching the script along with the test file above and looking for the production of EDL files. Here are sample commands:
      $ cd /usr/local/sbin/
      $ ./emby_preprocessing.sh '/mnt/Emby/TV Shows/Seinfeld (1981)/Season 8/Seinfeld S08E05.ts'
  3. Combine the Emby Scripter-X plugin with the bash script:
    1. Ensure that the live_tv parameter in /ect/comskip.ini is enabled: live_tv=1
    2. In the Emby Plugins area, ensure that the Real Time option is enabled.
    3. Navigate to the Emby Scripter-X settings as follows: Manage server > Scripter-X → Actions
    4. Scroll down to locate the onLiveTVRecordingStart section. Click the section title to display the details. 
    5. Click the "+" icon at the top-left of the long list of variables and then the edit/pencil icon on the right. 
    6. Complete the following:
      1. Script file: /usr/local/sbin/emby_preprocessing.sh
      2. Parameters: "%recording.path%" "%recording.name%"
      3. using (dropdown): /bin/bash
    7. Click the save/check icon on the right to save the changes.
    8. Test that the the Emby Scripter-X plugin is properly launching the bash script by recording a show and looking for the production of EDL files. Since the bash script includes a 1-minute delay, wait a few minutes and be patient. Once the EDL file is created, you can observe this file during the recording.  Entries should show up in real time as commercials are detected.

1684966836_Scripter-XActions2022-03-01at4_11_45PM.png.d3f80807fa0a08454defd6f758b208fc.png

Step 6 — Installing the Com Skipper plugin for Emby Server

Now that your system is properly configured and tested, it's time to install the Com Skipper plugin and enjoy the ability to skip commercials while watching a recorded show!

Note: This plugin is not (yet) in the existing plugin catalog and must be installed manually. Once installed, it will appear in the Plugins section of Emby and you can manage it from there.

  1. Download the com_skipper plugin from the first post in this thread. I recommend subscribing to this thread to get notified when updates are released. You might also consider watching this repository on Github for the same reason.
  2. Extract the ZIP file to your downloads folder.
  3. Copy the DLL file to the Emby plugins folder with the following command (update to your emby path from step 1 as needed) .
    $ sudo cp ~/Downloads/ComSkipper.dll /opt/emby-server/system/plugins
  4. Restart Emby from the dashboard.
  5. Review the plugin settings by navigating to Manage server > Plugins > Com Skipper > Settings.
    • Note 1: The setup described above does not support the Real Time Processing option shown in the plugin.
    • Note 2: For AppleTV users, you MUST enable the first 2 options for things to work as you expect. 

350241105_Plugins2022-02-16at11_32_31AM.png.8842e31a81e3e36cc5c15357aca39b3e.png

Step 7 — Test commercial skipping in your Emby client!

If everything is done right, you can now watch either the show you used for testing in Step 4, or the new show you recorded as a test in Step 5. Commercials should be skipped based on the times (in seconds) noted in the EDL files created by comskip.

If many of your commercials are not detected, or if the start/end time for the detected commercials is not accurate enough for you, feel free to experiment with the values in /etc/comskip.ini until the desired accuracy is achieved. Google is your friend in this pursuit. For example, this thread, and this one regarding country-specific INI files.

Step 8 — Using series-specific comskip.ini files

If you followed the live-processing instructions in Step 5(B), you can put a comskip.ini file in the parent folder for any series you are recording. This file can have different parameters than the default file in /etc. This is useful for problematic shows that need specific optimization. 

Using the Seinfeld example above, use this command to copy and then modify your default ini file:

$ cp /etc/comskip.ini '/mnt/Emby/TV Shows/Seinfeld (1981)'
$ nano '/mnt/Emby/TV Shows/Seinfeld (1981)/comskip.ini'

comskip.ini

emby_preprocessing.sh

Edited by vdrover
Added live processing details.
  • Like 2
  • Thanks 2
Link to comment
Share on other sites

BillOatman

@vdrover Wow our ini files are VERY different for both being in the US (I assume)!  I'm going to give yours a try! :) My wife likes wheel of fortune and they do that nonsense where they have a commercial, Vanna talks for 5 seconds, then back to commercial.  With my ini it screws up and shows the commercial after she talks every time.  Otherwise it's damn near perfect, but my ADD won't allow me to let it go so I seek out the holy grail of comskip.ini files :)

Edited by BillOatman
  • Like 1
Link to comment
Share on other sites

Love to hear your results. A few points:

1. I edited my post to include some other links to INI files. I plan to test them also after identifying the differences (I've already alphabetized the the lines to make this a bit easier). For example, I had an old Law & Order episode that needs to add probably 500ms to each detected commercial.

2. @reneboulard has some logic in his script (see above) where you can have program-specific INI files. Very clever. Might be a good way for you to dial in on JUST WoF over time without messing up the shows that work currently.

Link to comment
Share on other sites

Bill, I tested your INI against mine using a 62 minute recording of an old Law & Order rerun. The results are very, very similar. 

1183659985_Scratchsheet-GoogleSheets2022-02-18at7_19_57AM.png.660e224ddaf4fccab6449320076acc73.png

Link to comment
Share on other sites

BillOatman
1 hour ago, vdrover said:

Bill, I tested your INI against mine using a 62 minute recording of an old Law & Order rerun. The results are very, very similar. 

1183659985_Scratchsheet-GoogleSheets2022-02-18at7_19_57AM.png.660e224ddaf4fccab6449320076acc73.png

That's close enough to call them identical!

  • Like 1
Link to comment
Share on other sites

First awesome work!

 

Just though I’d share what I’ve found after discovering the live EDL changes/improves once the recording ends.

 

live_tv=0 ;(0=off / 1=on)

  • This will enable the output of commercial information during the processing allowing the skipping of detecting commercials before the processing is finished

  • Use when you want to process during recording and have the commercial information available before the recording is finished. This uses a simplified detection algorithm so the results may be different from the final results.

I’ve also been using the following in most of my INI files for a few years:

Speed it up

lowres=10 ;(0=off / 10=auto)

  • Enables resolution reduction, setting to 10 makes comskip choose the best value automatically

  • This only works for certain types of video encoding, may not work for h.264 encoded video and may deliver bad results when used with hardware_decode=1

thread_count=3 (int) command line overrides [--threads=<int>] (about 1/2 logical processors is optimal)

  • Number of threads used when decoding the video

  • Increasing the thread count speeds up the processing on CPU's with more cores then the current thread count. Setting the number to higher then the amount of cores makes processing slower. Setting the thread_count to 1 disables multi-threaded decoding, useful in case comskip crashes when decoding the video.

 

Tweek the EDL output

padding=1 ;(int)

  • Amount of seconds each commercial will be reduced both as start and end.

  • When you always want to see the start and end of the commercial break this can be used.

 

edl_offset=-45 ;(int)                                            

  • The amount of frames subtracted from the edl cut time output, use negative numbers to shift to later.

  • Thanks 1
Link to comment
Share on other sites

On 2/20/2022 at 3:27 PM, TMCsw said:

thread_count=3 (int) command line overrides [--threads=<int>] (about 1/2 logical processors is optimal)

I have 10 cores. I'm going to do some performance testing. TY!

Edited by vdrover
Link to comment
Share on other sites

Performance testing 1: Multithreading and low-resolution decoding
I've done quite a bit of performance testing and optimization in the last few days. Herein I present my findings with two comskip parameters mentioned above by @TMCsw: thread_count (specifying the # of processor cores to use at once) and lowres (decoding the video file in "low resolution" mode).

Test environment

  • Hardware: Intel NUC10i7FNH NUC 10 Core i7 with 32GB RAM and a 4TB SSD.
  • # of cpu cores: 12 @ 1.10GHz
  • OS: Ubuntu 20.04.3 LTS (64 bit)
  • Test file
    • 61 minute show recorded with my HD Homerun
    • Codec: 1080i MPEG2VIDEO
    • Resolution: 1920x1080
    • Framerate: 29.97
    • Bitrate: 8,139 kbps
    • Bit Depth: 8 bit
    • Pixel Format: yuv420p

Test procedure and data collection
Here are the steps I took for each test.

  1. Record the server load by running the following command from a terminal window (terminal A): 
    • while true; do uptime >> '/home/username/Downloads/uptime.log'; sleep 1; done 
  2. In a second terminal window (terminal B), modify comskip.ini by adding the parameters below. In my case, I tested variations and combinations of the following (where X and Y are an integer between 0 and 10):
    • thread_count=X
    • lowres=Y
  3. Run comskip in terminal B. When complete, terminate the uptime command in terminal A.
  4. Record the following data in a spreadsheet:
    • decode time (from terminal B).
    • peak 1-minute server load (from uptime.log saved in step 1.
  5. Delete the contents of uptime.log file and return to step 1 to test a new parameter.

Note: Most of the data below is simply the result of one test (n=1). Anecdotally, identical parameters produced very similar results within a few percentage points of each other.

Interpreting the results
The two metrics I followed in this test were the total time required for comskip to complete (the decode time), and the server load.

My server generally operates at a load of 1.2 when not recording a show, detecting commercials, or transcoding video. I have noticed that Emby works well at a load of 4, but I am hesitant to push the load above this for fear of affecting overall server performance. For a full discussion of server loads in Linux, this is a good resource.

For most folks, having fast commercial detection without a big impact on server load is ideal (especially true if your server has a loud fan!). My goal is to make commercial detection as fast as possible while keeping the peak server load around 3.

Results
Note: All the graphs below are color-coded: blue represents decode time and red indicates peak server load.

Multithreading
I first tested the effect of enabling multithreading, or using multiple CPU cores at once, to detect commercials. 

As shown below, a significant reduction in decoding time was achieved when thread_count=3 was added to comskip.ini. Only marginal improvements occurred with additional cores/threads, while server loads trended upwards. 

I concluded from this data that thread_count=3 was best for my system.

multithreading.png.bfa1204a9e92d0b3a5a03ee9d549a201.png

Lowres
I don't really have a clear understanding of how the lowres parameter affects comskip performance, but I imagine it has to do with how carefully it examines each frame of the video. In any case, the data was clear. As shown below, increasing lowres did not increase the server load in a consistent way. However, lowres=2 dropped the decode time by about 30% (remember, thread_count=3 was held constant in these tests).

Note that lowres=10 automatically sets this parameter to the best value. In my case, the data using lowres=10 (not shown) was a very close match to lowres=2

I concluded from this data that lowres=10 (i.e. lowres=2) was best for my system. 

Update 2/25/2022: The lowres=10 is great for performance, but the accuracy was not appropriate. I adjusted to 5 with much improved results, but eventually removed the parameter entirely. 

lowres.png.b094f5ad7855139e3fc2b3dad883fe05.png

Multithreading + lowres
As a final test, I compared the individual effects of lowres and thread_count to the combined effect of both. Consistent with the other data presented, thread_count=3 and lowres=10 had signifiant individual impacts on decode time. Together, the two parameters produced a nearly 60% reduction in decode time.

Server load increased 44% under the same conditions. However, the peak load was still close to my desired threshold (3). Importantly, the higher load was short-lived.

decode-time-thread_count-lowres.jpg.404cc96029f8aa7bca91ac5eb2812ee1.jpg

Conclusion
I concluded from the data that lowres=10 and thread_count=3 was best for my system and I'll be adding those parameters to my comskip.ini file. 

Next steps
In my next analysis, I'll be looking at server performance when running comskip as a post-recording process compared to running it in real time as recordings occur. 

Edited by vdrover
  • Thanks 3
Link to comment
Share on other sites

Wow...awesome analysis and write up! Will be taking notes and making some changes to see if my times drop in general (even on MCEBuddy)!

  • Agree 1
Link to comment
Share on other sites

So first off fantastic work guys! I have a question not quite pertaining to this thread but along similar lines. I have MCEbuddy to convert TS files to mkv to save space and have EDL files generated but the Emby client (as far as I know) don't support EDL files, how are you guy doing the commercial skip on an EDL file? Would there be a way to (maybe a feature request) to just use an EDL file already in the folder with the same name as the show? 

Link to comment
Share on other sites

BillOatman
2 hours ago, Mbppg said:

So first off fantastic work guys! I have a question not quite pertaining to this thread but along similar lines. I have MCEbuddy to convert TS files to mkv to save space and have EDL files generated but the Emby client (as far as I know) don't support EDL files, how are you guy doing the commercial skip on an EDL file? Would there be a way to (maybe a feature request) to just use an EDL file already in the folder with the same name as the show? 

That's exactly what it does now :)  If there is a EDL file with the same name as the show, and the comskipper plugin is installed and enabled, it will skip the commercials defined in the EDL file.  It doesn't matter how the EDL gets generated (in your case mcebuddy is fine), so long as it is a valid file and named correctly.

Edited by BillOatman
Link to comment
Share on other sites

BillOatman
3 minutes ago, Mbppg said:

Perfect Great, link to the latest version, ready and happy to test and report back.

The current release is always linked in the first post.

Link to comment
Share on other sites

Sorry in advanced if this was brought up. I noticed commercial skipping stopped working on the latest beta. All my settings and script remained yet no comskip files were generated. Either it’s related to the recording file or that call up for the script to run?

Anyways, I just rolled back to the stable release and I am testing a recording to see if comskip is called up from the script I have Emby pointed at.

Anyone else seeing this?

Edited by mike3821
Link to comment
Share on other sites

BillOatman
2 minutes ago, mike3821 said:

Sorry I’m advanced if this was brought up. I noticed commercial skipping stopped working on the latest beta. All my settings and script remained yet no comskip files were generating. Either it’s related to the recording file or that call up for the script to run?

Anyways, I just rolled back to the stable release and I am testing a recording to see if comskip is called up from the script I have Emby pointed at.

Anyone else seeing this?

There in no guarantee of it working with betas as they are so helter skelter.  I never run them.  I was told it worked with the beta, but if a new beta got released recently who knows.  Sorry, it's just too much of a moving target for me to support.

  • Agree 1
Link to comment
Share on other sites

20 minutes ago, BillOatman said:

There in no guarantee of it working with betas as they are so helter skelter.  I never run them.  I was told it worked with the beta, but if a new beta got released recently who knows.  Sorry, it's just too much of a moving target for me to support.

Fair enough.

Link to comment
Share on other sites

EmbyOak

@BillOatmanHi Bill, I understand you do not support beta versions.  I would just like to report that your Comskip works great with beta .25 but does not work with .27.  I cannot confirm with .26 as .26 breaks playback for some of my feeds.

 

When I say doesn't work, it partially works.  It is skipping detecting commercials, the audio works, but the video remains of the option page before the show starts (see attached image).

IMG_6393.jpg

  • Thanks 1
Link to comment
Share on other sites

BillOatman
40 minutes ago, EmbyOak said:

@BillOatmanHi Bill, I understand you do not support beta versions.  I would just like to report that your Comskip works great with beta .25 but does not work with .27.  I cannot confirm with .26 as .26 breaks playback for some of my feeds.

 

When I say doesn't work, it partially works.  It is skipping detecting commercials, the audio works, but the video remains of the option page before the show starts (see attached image).

IMG_6393.jpg

Thanks, that probably some sort of incompatibility between the beta server and your client around remote control commands.  Like I said, Emby betas are a very moving target :)

  • Like 1
Link to comment
Share on other sites

vdrover
On 2/16/2022 at 7:12 PM, BillOatman said:

Very nice @vdrover!  I'll make a PDF out of it and put it in github and the release!

Updated to included live-processing for Linux, which as some differences to windows. See steps 5 and 8, and a new attachment. 

  • Thanks 1
Link to comment
Share on other sites

EmbyOak

@BillOatman With some further testing (using Beta .28) the "crashing" (or how ever you describe the plays the menu screen with the audio in the background).  This happens to me with an AppleTV 4k v1.  It works perfectly with the iPhone Emby app as well as in the Safari browser.

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...