home :: examples :: simple_video_parser

Simple Video Parsing Example

In this example, we use the VideoParser object to parse the coding elements of a video file specified on the command line. We create a single event handler for all CodingElement events (essentially all of the coding elements). The handler simply prints out a string representation of each coding element encountered.
using System;
using System.IO;

// Declare the MPEG2Event namespaces.
using MPEG2Event;
using MPEG2Event.Video;

public class SimpleParsingExample {
public static void Main(String[] args) {
// Check for filename passed on command line.
if (args.Length != 1) {
System.Console.WriteLine("Must specify filename");
return;
}
// Open the file for reading and wrap a
// BitStream object around the file stream.
BitStream bs = new BitStream(File.OpenRead(args[0]));
// Create the video parser
VideoParser vp = new VideoParser(bs);
// Register our callback
CodingElement.handlers += new CodingElement.Handler(element_handler);
try {
// Parse pictures until the end of the stream
while (!vp.EOF) {
vp.parsePicture();
}
}
catch (OutOfBitsException e) {
// Handle an ungraceful end to the video stream
System.Console.WriteLine("Video file ended without sequence end code");
}
}
// This is the callback that will process the coding element
// events as they are published.
public static void element_handler(BitStream bs, CodingElement e) {
// All we do with them is print them to the console.
System.Console.WriteLine(e.ToString());
}
}

permalink 2004.11.23-09:24.00