How to write a new method of commercial detection

From MythTV Official Wiki
Jump to: navigation, search

Commercial detection is done by a program called mythcommflag which you'll find in (surprise) programs/mythcommflag. mythcommflag is usually initiated by a backend. It is expected to return a list of commercial start & end points (often referred to as the breakmap). This map gets stored in the database. Not only after commflagging has finished, but it gets updated as the commflagger goes along, so we can flag a live recording. It also sends out events to frontends that are currently watching this recording that the breakmap has been updated.

mythcommflag takes care of looking up the file requested, stuffing results into the database & notifying frontends. The actual work is being done by an implementation of the virtual class CommDetectBase. If you want to add a new method of commercial detection, you have to make a new implementation of this class. There are few restrictions on what the implementation can choose to do or not do. The implementation is provided a NuppelVideoPlayer*, which it can use to seek around in the recording, and analyze the video.

It is expected to pause when instructed to, as well as stop when its instructed to. The base class provides several events that you should signal:

- gotNewCommercialBreakList()

signal this event if your detection code has a new idea on what might or might not be commercial (This doesn't per se mean this is going to be another detected commercial. It could also change its mind on something it has considered commercial a while before).

- statusUpdate()

call this with a string to indicate some sort of progress message. mythcommflag will output this either to the console or to the database depending on how it is run. The current implementation says things like "looking for logo" "processing video 34%" or stuff like that.

- breathe()

call this from time to time, to give mythcommflag an opportunity to do some maintainence (like checking if we received a command to stop or pause). I think it is currently called after processing 10 frames or something like that.

You should add your CommDetector class to the commdetector factory, and just assign a number to it. There is a command line option for mythcommflag to specify which commdetector implementation to choose.

Your implementation should be aware that the entire recording might not be finished recording yet, so be careful. Take a look at ClassicCommDetector to see how to properly deal with this situation.

Detection method discussion

We may also be able to detect commercials by a break or change in the data in the non-scanned portions of a frame as well as things like Nielsen Commercial Verification tags.

Another way of detecting commercials is to identify changes in the sound volume. I have noticed that volume usually goes up noticeably during commercials. Not sure whether this is really a pattern across all channels or just on a few. One very simple way I can think of right now to use this idea is to do some weighing of the sound amplitude through time. Effective methods to measure the sound volume through time might exist already.

A more advanced method would be to analyse the image statistically using histograms that are averaged over several frames. The scenes could then be categorised using Bayesian statistics applied either on the background noise in the image or on the color temperature. This would basically determine which scenes belong to a movie based on the type of recording and post processing equipment used to make it. Using this method it would also be possible to build a database of commercials that could be used to reduce the uncertainty of the decision. The statistics engine could be taken from a spam filter software such as bogofilter.

Actually, in most countries the technical amplitude does not increase for commercials. Instead, the dynamic range is decreased and the range is limited to a window just below regulations. (e.g. If the commercials may be no louder than the peak volume of a program according to broadcasting regulations... then they will be just below the peak volume of a program.) R3m0t 22:04, 8 November 2006 (UTC)

See also: Commercial detection with silences