Cutting an Ogg File
Cutting an Ogg file consisting of video and audio streams into sections by time, while preserving synchronization between streams in the output files, is a bit tricky. Because there are several ways to do so, this section gives you an idea of the possiblities and their advantages and drawbacks.
Tools to edit video files can be divided between lossless and lossy tools. Lossless tools only handle the ogg stream (ogg pages and ogg packets). So the quality of the data is preserved. Lossy tools must first decode the data to allow editing and then re-encode, thus losing quality. This type of editing provides greater flexibility (e.g. it allows overlaying of images) at the expense of quality. Beyond that, the decoding and encoding process requires much more time than just handling Ogg stream packets.
This chapter only deals with tools that losslessy cut video files.
The command line tool oggCut is a tool that can cut Ogg files on a keyframe basis. Use it like:
oggCut -s <StartTime> -e <EndTime> originalFile.ogv createdFile.ogv
where <StarTime> and <EndTime> are given in milliseconds. Alternatively you can cut using a start time and length using option -s:
oggCut -s <StartTime> -l <Length> originalFile.ogv createdFile.ogv
Here, too <Length> is measured in milliseconds.
When oggCut is executed, it searches for the first Ogg Theora packet with a timestamp greater than or equal to the time given by the -s option. If this packet is found, the program seeks to the next keyframe and creates the new file with that keyframe as the first frame.
When the program finds a packet with a timestamp greater than the end time, it closes the newly created file and stops.
As the start time is likely not the position of a keyframe, the created file may be smaller than expected.
Video/Audio synchronization issues with oggCut
Because oggCut only operates only on packet boundaries, and because Vorbis and Theora stream packets are not synchronous, the audio and video packets usually start at different time positions.
Therefore when oggCut creates a new file that starts at time t, the next audio packet will likely start at a position t+x, where x is the time offset between the keyframe video and the next audio packet.
When these two streams are written to the new file, this time offset information is lost. Therefore the player must assume that both files start at time 0. For that reason, the audio stream always plays a bit too early. However, as an audio block is quite short, the difference is usually only a few milliseconds and is hardly noticeable.
The program oggz-chop from oggz-tools cuts Theora videos at any position requested. If neccessary it cuts at non-keyframes or even in the middle of a frame's presentation time.
Technically this kind of fine-granularity cutting is not possible with Theora video technology. It is achieved by a trick: instead of really cutting the video at the requested position, it is cut at the closest point, making the resulting video file longer than requested. Then a so-called Ogg Skeleton header is added to the video that instructs the player to only play the actually requested range of the video.
The superfluous video frames that could not be cut from the video are still present, they will just be hidden on playback.
The disadvantages of this approach are obvious: on the one hand, disk space is wasted by storing unnecessary amounts of data, on the other hand, the newly cut file will only play back correctly in video players that understand the Ogg Skeleton information. Another not so obvious disadvantage is that files that were created this way can not be concatenated without the hidden part reappearing. This is due to the fact that the Ogg Skeleton can only hide frames at the start and end of a video. Concatenating two videos of this kind, will leave hidden frames in the middle -- something Ogg Skeleton does not support.
Now that you understand the implications of using oggz-chop, let's look at how it is used:
oggz-chop -s <StartTime> -e <EndTime> originalFile.ogv createdFile.ogv
Here <StartTime> and <EndTime> are specified as <Hour>:<Minute>:<Second>. <Second> can contain a fractional part so you are free to specify time at any resolution.
We should mention another option: you can run oggz-chop with -k to force it to omit any Ogg Skeleton information. This way granularity of the cutting process is limited to keyframes and you can be sure that the generated file will play back correctly even in older players that do not support the Skeleton.