Microblog: A very long article Wikipedia article on the orientation of toilet paper [Jun 7th, 22:52] [R]

Thursday, August 28th, 2008

Editing MPEG TS Streams

Categories: [ IT ]

When I record a TV show on Finnish DigiTV, I get an MPEG TS file containing interleaved audio and video streams (as well as a subtitle stream if the TV channel uses them). Removing the commercials or even simply the five minutes padding at the beginning and the end is not completely straightforward, but can be done quickly with some simple tools.

editmpegts is a command line tool I wrote that takes an MPEG TS stream and an edit list (in MPlayer's EDL format), specifying the time positions of commercial breaks and cuts the MPEG TS into slices to keep only the interesting part of the recording. I then transcode the slices individually, and contatenate them with avimerge.

There are however a couple of pitfalls if you want to use mencoder to transcode the slices into e.g., XviD+MP3, since the MPEG 2 video decoder resets every time the aspect ratio of the video changes. When doing 2-pass encoding, this means that the stats gathered before the change of aspect ratio will be lost, resulting in miscalculated bitrate and far too big output files (at least in the old, overpatched version of MPlayer I'm using).

mencoder reacts on two things: the change of the value in the aspect ratio field in the MPEG sequence header, and the presence of an “End of Sequence Marker”. editmpegts forces all the aspect ratio flags to the same value, and simply suppresses the marker by overwriting it with the data that precedes in within one TS packet. It is all done in one pass, no need to demultiplex and remultiplex the stream (but it does copy the data into a new output file, it doesn't therefore modify the input file).

The MPEG TS streams broadcast by SubTV contain loads of sequence headers but very few end of sequence markers. My understanding is that every sequence (starting with a sequence header) should end with an end of sequence marker, so SubTV's streams are illegal. But I may be wrong. Anyway, it works fine with MPlayer (and now with mencoder too), so who cares?

[ Posted on August 28th, 2008 at 00:05 | no comment | ]

Trackback Address



No comment

Add comments

You can use the following HTML tags: <p>, <br>, <em> <strong>, <pre>. URLs starting with http:// will automatically be turned into hyperlinks.


Save my Name and URL/Email for next time

11 - 8 =