Close Show/hide page

Updated FLV Encoder, 3.5x faster with Alchemy

Thumbnail - Click me
Demo: Record and encode FLV’s from webcam in realtime, using Alchemy

Or, download AIR desktop version

This is an update to FLV Encoder which adds an optional Alchemy routine that’s about 3.5x faster, as well as FileStream support for writing directly to a local file using AIR. The library has been architected in such a way that you can use the package while targeting either the browser (no FileStream support) or AIR, and either Flash 9 (no Alchemy support) or Flash 10 – without getting class dependency compiler errors.

I’ve put the package on Github, along with a couple examples (including the one from the last post). The API has changed a little so be sure to also see the example code below. A method updateDurationMetadata() has been added so the video duration does not have to be declared at the start. Also, a bug where the top-most line of pixels was not being written has been fixed.

Realtime encoding demo:

Because of the increased speed of the Alchemy version, it is now viable to encode FLV’s in realtime as the audio and video is being captured, at least within certain limits. Click on the thumbnail above for an online demo that encodes webcam video and audio to a file at 320×240 in real time. If your system is fast enough, you can keep the framerate set to 15FPS with minimal hiccups. The browser-based version must store the entire FLV in memory before saving to disk, but the equivalent AIR version can save its contents directly to a file so that the only limiting factor is disk space. I’m using a dynamic timing and queuing system to keep video and audio in sync which could be the topic of another post.

Updated usage examples:

[A] To encode an FLV to a ByteArray, start by instantiating ByteArrayFlvEncoder. The rest is similar to the last version, but you can now call updateDurationMetadata() when you’re done to update the duration property in the metadata. Finally, call kill() to prepare the object for garbage collection:

var baFlvEncoder:ByteArrayFlvEncoder = new ByteArrayFlvEncoder(myFrameRate);
baFlvEncoder.setVideoProperties(myWidth, myHeight, VideoPayloadMakerAlchemy);
// (Omit the 3rd argument to NOT use Alchemy if you're targeting Flash 9)
baFlvEncoder.setAudioProperties(BaseFlvEncoder.SAMPLERATE_44KHZ, true, false, true);
baFlvEncoder.addFrame(myBitmapData, myAudioByteArray);
baFlvEncoder.addFrame(myBitmapData, myAudioByteArray); // etc.
saveOutMyFileUsingFileReference( baFlvEncoder.byteArray );
baFlvEncoder.kill(); // for garbage collection

[B] To encode an FLV directly to a local file (in AIR), instantiate FileStreamFlvEncoder with a File reference, and open up the exposed FileStream, and then close it when you’re all done:

var myFile:File = File.documentsDirectory.resolvePath("video.flv");
var fsFlvEncoder:FileStreamFlvEncoder = new FileStreamFlvEncoder(myFile, myFrameRate);
fsFlvEncoder.fileStream.openAsync(myFile, FileMode.UPDATE);
fsFlvEncoder.setVideoProperties(myWidth, myHeight, VideoPayloadMakerAlchemy);
fsFlvEncoder.setAudioProperties(BaseFlvEncoder.SAMPLERATE_44KHZ, true, false, true);
fsFlvEncoder.addFrame(myBitmapData, myAudioByteArray);
fsFlvEncoder.addFrame(myBitmapData, myAudioByteArray); // etc.


A new instance of FlvEncoder should be created for every new FLV to be generated.

If you’re using the Alchemy version, the SWC lives in leelib/util/flvEncoder/alchemy, so be sure add that folder to your project library path. The C++ code is there as well.

Note that the current Alchemy video encoding routine is being done synchronously, which means that although it’s much faster than the regular AS3 version, the main Flash thread must still wait for the Alchemy encoding to be done before continuing. An asynchronous version would allow for a real-time encoding scenario that is more flexible and probably with less ‘dropouts’, and would probably prompt the need an internal queuing system. Anyway, this warrants further development.

- Source code (Github)

Licensed under a Creative Commons Attribution 3.0 License.

107 Responses to “Updated FLV Encoder, 3.5x faster with Alchemy”

  1. Jon says:

    In case anyone runs into the same issue that I did, I’m getting better results after adding the -r option to my ffmpeg command explicitly defining the frame rate to use.
    ffmpeg -r 15 -i temp.flv -qscale 0 -pix_fmt yuv420p -ar 22050 -vcodec libx264 temp.mp4

  2. Actually good style and style and exceptional content material , absolutely nothing at all else we need to have : D.

  3. this site says:

    Gastronomia reggiana of gioco cellulare 3. modem roper, paestum it

  4. 438628 749593This write-up is dedicated to all those that know what is billiard table; to all those that do not know what is pool table; to all those that want to know what is billiards; 316796

  5. Fairy says:

    Hi there, I’ve tried your code but the video gets saved as a flv file in my documents folder but the problem i’m facing is that the video plays too fast, can you please help me out?

    cam = Camera.getCamera();
    cam.setMode(640, 480, 15);
    vid = new Video(cam.width, cam.height);

    private function startRecording(e : MouseEvent) : void
    this.addEventListener(, enterFrame);

    _baFlvEncoder = new ByteArrayFlvEncoder(stage.frameRate);
    _baFlvEncoder.setVideoProperties(OUTPUT_WIDTH, OUTPUT_HEIGHT);

    bt.removeEventListener(MouseEvent.CLICK, startRecording);

    public function enterFrame(
    var bmpData:BitmapData = new BitmapData(OUTPUT_WIDTH, OUTPUT_HEIGHT, false, 0xFFFFFFFF);


    private function stopRecording(e : Event) : void

    this.removeEventListener(, enterFrame);

    var fileRef:FileReference = new FileReference();, “test.flv”);


  6. John says:

    Thanks for this, it’s really useful! Not sure if anyone will read this, but is it possible to save to a device’s cameraRoll, the same way you can save bitmaps directly to the cameraRoll?

  7. Hello just wanted to give you a quick heads up. The words in your post seem to be running off the screen in Safari. I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I’d post to let you know. The layout look great though! Hope you get the problem resolved soon. Cheers.

Leave a Reply