Readme: atrim

Most users will have an easier time editing transport streams using HDTVtoMPEG2:


You seek a way to edit ATSC transport streams like those recorded by the HiPix or WinTV-HD.


This program, "atrim" (ATSC trim), will chop up ATSC streams as you specify.

atrim will divide the input file into two parts, keeping the part that you specify and discarding the other part.  You specify where the cut is to occur, expressed in seconds from the beginning of the file, and whether you want atrim to keep the first part or the second part of the file.  The input file is saved unchanged with the extension '.old' and replaced with the trimmed version.

atrim's notion of time is not that of the ATSC spec, but that of the HiPix app, versions 3.1 and earlier.

Notes about Trimming

atrim does all of it's work by chopping up ATSC streams, maintaining the ATSC packet structure.  It has no concept of video or frames.

Every ATSC rendering device knows how to jump into the middle of an ATSC stream at any point and start rendering, but there will be some delay between the time it jumps in and when the picture actually appears.  This delay varies from device to device.  A consequence of this is that different TVs have different channel-change latency.

So if you want to remove x seconds from the beginning of a video, how do you know what offset to specify?  It will be x plus or minus a few seconds, but you won't know what that plus or minus is before-hand.  You just have to guess and see what happens.  The difference can even vary within the same program.  Fortunately, atrim makes it easy to try again if you pick the wrong offset (see Usage below).

Further, since the delay varies from device to device, an offset that gives you a splice where you want for one device may not give you the same result for another device.  For this reason, atrim automatically saves a few seconds of transport stream from the other side of the splice point.  These files have the extension '.before' or '.after' depending on whether you tell atrim to save the second part or the first part of the file.  You probably won't care now, but these files will come in handy if you try to use the transport stream on another ATSC device later.  These extra files take a lot less space than keeping the entire original files.

If you're using the HiPix, you can determine where you want to make your splice point by looking at the time on the HiPix window (minutes and seconds, HiPix app versions 3.1 and earlier).  The seconds will correspond to the offset within the current minute file only if all the files before it in the program have lengths that are multiples of one minute.  Of course that's the case when you make a recording, but it won't be once you start trimming.  So make your job easy by editing your program backwards - from the end to the beginning.  Or if you screw up, temporarily move the non-one-minute-length files to a different directory.


Usage: atrim <input file> <1|2> <offset seconds>
  where <1|2> is the part of the file you want to keep

You specify the input file, whether you'd like to keep the first part of the file or the second part, and where the splice is to occur.

atrim will save the original file by adding  '.old' to the end of the filename.  If a '.old' file already exists, atrim will use that file as input instead of the file you specify.  This makes for easy trial-and-error.  If your first guess at an offset is incorrect, just re-run the command with a different offset.

Notes and Limitations

Because atrim is reading and writing from the same drive, it uses a large buffer (69 MB) in order
to minimize processing time.  Using an even larger buffer will probably not help performance.  If you don't have 69 MB of free physical RAM, then you should buy more RAM, or use a smaller buffer (at the expense of performance).  The buffer size is defined in the source code.

atrim expects all packets to be 188 bytes long, to begin with the sync byte, and to be aligned to 188-byte boundaries.  So if your ATSC recorder splits streams into multiple files, it should start each new file at the beginning of a packet.  The HiPix and MyHD do this.  By default the WinTV-HD does not.  You can force it to by telling it to write files that are a number of megabytes that is a multiple of 188 bytes.  (e.g. 141 MB)  Set this registry key:


atrim does not handle large files (>2 GB).


On my system, a minute's worth of ATSC stream (137 MB) is processed in about 15 seconds.  The bulk of that time is spent reading and writing to/from the disk.  The only way to improve this would be to read and write to different physical disks.


This code is not guaranteed bug-free.  If you don't want to lose your data, save the original files.




atrim is licensed free for personal use.  You may not distribute the source or executable.