Monday, January 18, 2010

How to fix a “clip duration” error when using the Expression Encoder SDK

Tonight I was playing around with the latest Expression Encoder SDK in an attempt to build a small video editing tool.  What I wanted to do was to take a video and then extract only the highlights from it into a smaller “highlights video”.  This is really useful for my side interest as a hockey coach because it would enable me to extract only certain highlights from a hockey match and then send the reduced video around to the players to watch.
So I wrote the following code to get things started:

MediaItem mediaItem = new MediaItem(@"D:\Videos\CANON\20090202\20090202000204.m2ts");

mediaItem.Sources[0].Clips.Add(new Clip(TimeSpan.FromSeconds(49), TimeSpan.FromSeconds(56)));
mediaItem.Sources[0].Clips.Add(new Clip(TimeSpan.FromSeconds(159), TimeSpan.FromSeconds(167)));
mediaItem.Sources[0].Clips.Add(new Clip(TimeSpan.FromSeconds(290), TimeSpan.FromSeconds(297)));

Job job = new Job();
job.MediaItems.Add(mediaItem);

job.OutputDirectory = @"C:\output";
job.Encode();


When I ran it the following exception was thrown:
EncodeErrorException was unhandled
A clip's duration is smaller then the min clip duration 00:00:00.0170000.
Now if you look at the timings that I entered for each of the Clips in the media’s ClipCollection you will clearly see that they are indeed each greater than the duration shown in the error message, so what gives!?

The fix turned out to be quite simple – and obvious really – but I thought that I’d post the solution because there weren’t any clean hits that came up when I did my own search on the error message.  To fix, you simple call Clear() on the ClipCollection before you start adding your own Clip’s to it like so:

MediaItem mediaItem = new MediaItem(@"D:\Videos\CANON\20090202\20090202000204.m2ts");
mediaItem.Sources[0].Clips.Clear();

...

Job job = new Job();
job.MediaItems.Add(mediaItem);

job.OutputDirectory = @"C:\output";
job.Encode();

No comments:

Post a Comment