How To: Convert mythcommflag markers to chapter marks within

For discussion related to MythTV which doesn't belong in another forum.

Moderator: Forum Moderators

Post Reply
Posts: 4
Joined: Thu Jun 09, 2016 5:04 pm
United States of America

How To: Convert mythcommflag markers to chapter marks within

Post by zakaron » Mon Aug 15, 2016 7:37 pm

I liked the fact that Kodi can handle an EDL for commercial markers provided by MythTV or even an external text source file for its markers. What I didn't like is that it ignores the EDL once I've transcoded my recordings to an mp4 file with HandBrake. Apparently the framerate does not get calculated correctly while playing one of my mp4 files (shows 0 framerate in the info screen during playback) and because it is 0, the EDL code section is skipped. Seems to match up with this thread:
I am not running .28 nor am I ready to do so since I am running MythTV on FreeBSD and don't feel like compiling my own (.27.5 is available in freshports). Oddly I do transcode with constant framerate using --cfr option in my HandBrake command line.

Since I need to transcode my recordings to conserve space, I came up with an alternate way to handle commercials. Basically in my user job to transcode my recording, right after I do the transcoding process, I call mythcommflag on the new .mp4 to work its magic and mark the commercials. I then select out the ending commercial flags (type 5 in the recordedmarkup table) and build a temporary chapter file (I called chapfile) that is compatible with mp4box utility ( ) that can merge in the chapter flags with the video & audio streams to create a new mp4 container file. I then move this new file back into my recordings directory and update the database with the new file size, filename and transcoded flag set. I also delete out the recordedmarkup table entries since the chapter markers are now permanent within the mp4 container. Here's the section showing the chapter creation with an explanation of the variables used (this is bourne shell, btw):
$DBUSER = your mysql user name
$DBPASSWD = your mysql user password
$CHAN = channel ID as a passed in argument
$START = start time of recording as a passed in argument
$OUTFILE = basename of new mp4 file (basically basename from recorded table with updated mp4 extension instead of mpg)
$VIDEODIR = directory to your recordings
$CHAPCNT = just an incremental chapter count to be used in the chapter name

Code: Select all

# Mark commercials and set chapter markers in actual MP4 container file
# mythcommflag returns number of commercials as its exit code. Only numbers 128+ are an error code.
mythcommflag --file $OUTFILE --quiet
if [ $ERROR -gt 127 ]; then
   echo "Error creating commercial markers for $OUTFILE  with error $ERROR" >> errors.log
   exit $ERROR
MARKLIST=`mysql -B --skip-column-names -u$DBUSER -p$DBPASSWD mythconverg -e "select mark from recordedmarkup where chanid='$CHAN' and starttime='$START' and type='5' order by mark;"`
  echo "AddChapter($FRAME,Commercial $CHAPCNT)" >> chapfile
  CHAPCNT=`expr $CHAPCNT + 1`
mp4box -noprog -quiet -add $OUTFILE -chap chapfile $VIDEODIR/$OUTFILE
if [ $ERROR -ne 0 ]; then
  echo "Error in mp4box adding chapters. Check chapfile for accuracy. Error code: $ERROR" >> errors.log
  exit $ERROR

# Clean up recordedmarkup table since we do not need the markers after chapter creation
mysql -u$DBUSER -p$DBPASSWD mythconverg -e "delete from recordedmarkup where chanid='$CHAN' and starttime='$START';"
If you would like to see the entire script for what I do to transcode, I can paste that too if it would help to see. I figured I'd share this to start with for those wanting an alternate way to skip commercials without cutting them completely in case of detection errors. That was my main hang up; I didn't want to lose part of the show and wanted the option to rewind if necessary to go back through a commercial to get the part of the show if incorrectly marked. And Kodi already has most remote controls set up to skip ahead a chapter, so it is easy now to just skip to the end of the commercial. For those who export your recordings to be used on other devices, you'll still be able to skip commercials :)

Posts: 4
Joined: Fri Apr 24, 2015 2:36 am
United States of America

Re: How To: Convert mythcommflag markers to chapter marks within

Post by cs_dvr » Tue Mar 12, 2019 4:58 pm

Very interested to see your script... working on putting together a user job script or two and adding it to github..

Posts: 77
Joined: Tue Nov 11, 2014 11:03 am
Great Britain

Re: How To: Convert mythcommflag markers to chapter marks within

Post by halucigenia » Wed Mar 20, 2019 4:16 pm

Cutting out commercials is the way that I do it with my script.
I do have to manually adjust the markers before cutting, but that is not much hassle.

I can't imagine that watching your transcodes with bookmarks would be much fun because you can't watch the programme uninterrupted without wondering if you have missed anything and fast forwarding or rewinding to find out (I would have to anyway...). At least once the commercials are stripped out, you just get to to watch the programme straight through. Finding any serious transcoding errors, which would be spotted on viewing, could then be corrected as long as the original recording has not been deleted.

Commercial free viewing is the best thing about MythTv IMHO. I even hate timeshifting - starting viewing the recording late and having to skip commercials manually - I think that even this disrupts my viewing too much.

Disk space is cheap, but even so I transcode most of the stuff I record, stripping out the commercials, and keep the originals for quite some time before deleting them, just in case.

Post Reply