Recently I wanted to do some simple video slicing in AS3 with FLV's. I did not found any library for that so here is FLVSlicer !

FLVSlicer allows you to extract a "slice" of an FLV at runtime, but also merge multiple slices if you need to. The library may be really useful if you want to get a specific part from an FLV with no server involved. Ley's say for instance that you are looking for a specific text content in a video and you would like to save it on your hard disk or post it on youtube.

The API is really simple :

//create the FLVSlice object
var slicer:FLVSlicer = new FLVSlicer(new flvBytes());

// listen for the SlicedEvent.COMPLETE event
slicer.addEventListener( SlicedEvent.COMPLETE, onSliced );

// extract the slice with the specific timing (in ms) and save it
var firstSlice:FLVSlice = slicer.slice(1000, 3000);

// extract another slice with the specific timing (in ms) and save it
var secondSlice:FLVSlice = slicer.slice(7200, 8900);

// create a FLVSlice vector
var slices:Vector.<FLVSlice> = new Vector.<FLVSlice>();

// store the slices
slices.push ( firstSlice );
slices.push ( secondSlice );

// merge them as a single FLV stream
var merged:FLVSlice = slicer.merge( slices );

The tricky thing was to update each frame timestamp (NetStream.time) without creating any backward timestamp. Then, the onMetaData duration tag is modified so that the sliced FLV works smoothly in the Flash Player. When calling the slice() method from the FLVSlicer API, it will automatically find the nearest keyframe and cut from there. For the ending point, it will slice it with no nearest keyframe behavior.

To illustrate its usage, here is a little demo of the slice() method :

A Flash animation should have appeared here, but it seems that your browser has an older version of the Flash Player or it is not installed at all. Please, install the last release of the Flash Player now, then reload this page.

Merging multiple slices will be available in the next release :)
Merging multiple slices is available since version 0.3.

The current limitation right now with the Flash Player is that you cannot inject the new generated FLV in the NetStream class you have to send it to a server and load it again, damn, do you remember a similar story ? This is now possible with Flash Player 10.1 (NetStream.appendBytes).

Other limitation, when loading a video through the NetStream class, you cannot access the video bytes, so you have to load the video twice : Embed tag to access the video stream and use the traditional method to play the video.

So please, vote for adding byte access to the NetStream class.

This request has been opened by Benjamin Dobler who created RichFLV a very cool AIR app which does some FLV editing.

Sources from the demo above are available here.
Thanks for the cool components Keith ;)

Update : Version 0.3 adds a merge(slices:Vector.<FLVSlice) method to merge multiple FLVSlice objects into a single FLV stream.