Close Show/hide page

FLV Encoder with Audio

Thumbnail - Click me
FlvEncoder demo implementation: Records webcam video and audio to a local file

! Update: See updated FLV Encoder post + code here

A prospective client recently approached me about the possibility of adding audio support to SimpleFlvEncoder. They ultimately decided to go a different direction and so didn’t commission me for that work, but not before my curiosity had already gotten the better of me…

This class makes it possible to create FLV’s in AS3 that contain both video and audio, all on the client-side using the regular browser-based Flash Player. Like with the old version from 2007 (!), video is uncompressed (or rather, just zlib-compressed on a per-frame basis). Audio must be supplied in uncompressed PCM format with either 8 or 16 bits per sample, mono or stereo, and at 11, 22, or 44 KHz. It can also create FLV’s that contain only audio or only video.

Here is a quick-and-hand-wavey example of using the class (Please see the comments in the source for more detail.).


var flvEncoder:FlvEncoder = new FlvEncoder(_fps, _duration);
flvEncoder.setVideoProperties(_videoWidth, _videoHeight);
flvEncoder.setAudioProperties(FlvEncoder.SAMPLERATE_44KHZ, _is16Bit, _isStereo);
flvEncoder.begin();

for (var i:int = 0; i < _numFrames; i++)
{
	flvEncoder.addFrame(_myBitmaps[i], _myChunksOfAudio[i]);
}
	
saveOutMyFile( flvEncoder.byteArray );
	

The old "SimpleFlvEncoder" class, which dealt only with video, was stupidly simple to use. Having to deal with audio ends up being a little more involved. As I said, you have to supply the audio data in PCM format, which basically just means raw, uncompressed audio. You give it the audio data in the addFrame() method. You might gather that data using Sound.extract() or SampleDataEvent, or by dynamically generating your own. Use audioFrameSize to determine how much audio data in bytes is expected per frame.

Supplying too little or too much audio per frame is I believe "legal" per the FLV spec, so the class will take whatever size ByteArray is given to it for audio. An incorrect amount may result either in an unexpected framerate on playback or choppiness in the audio depending on the FLV playback implementation of the client program.

It's beyond the scope of this post to provide a tutorial-like explanation of playing with audio data in Flash, but let me describe what I think are the three most likely use-cases for this class when it comes to audio.

One is where the audio is static (e.g., a soundtrack that accompanies the video). In this case, you could load/embed an uncompressed WAV file into a ByteArray and basically just feed the correct number of bytes from that ByteArray to the addFrame() method without much drama (Uncompressed WAV files are just raw PCM audio data with some metadata at the beginning; you would start copying the bytes from after the end of the metadata). Alternatively, you could use Sound.extract() on an MP3, etc.

Another is where the audio comes from the webcam, which is what I'm doing in the sample implementation above. I'm using a modified version of Thibault Imbert's MicRecorder class to collect the audio data into one big audio ByteArray and then chunking out the pieces of it to FlvEncoder in a loop at the very end, which basically amounts to the same thing as the first scenario.

The last is where the audio is dynamic, presumably accompanying video which is equally dynamic, like for a game or other interactive experience. Unfortunately, it's impossible to record the global sound output from Flash using SampleDataEvent or anything else (though I'd be happy to be proven wrong). Just for the hell of it I tried using SoundMixer.computeSpectrum() on a really quick interval for the task, and was able to get something approximating what was needed, but it was predictably... way not good enough. Anyway, this means you would need to create something like a sound mixing engine, or possibly use some extant AS3 sound compositing library.

The last thing to say on this for the time being is that I tried my hand at Alchemy for the first time and made a version of the encoder that runs about 3x faster, but as a C++ and Alchemy noob, couldn't get rid of a memory leak and then gave up on it for the time being. If you're well versed at this sort of thing please let me know and I will happily send you the code. Otherwise, I will hopefully return to it at some point and post an update.

If the encoder is quick enough and the CPU demands of the dynamically generated video content are low enough, the FLV could be created in real time as the content is being produced, rather than storing all the bitmaps and audio data in memory and doing it all at the end; this might be viable under some limited use cases (i.e., low enough CPU use by Flash + low enough frame rate + small enough video size + fast enough computer).

Last last thing: I've always thought one of the coolest open source AS3 projects would be to use Alchemy with the Xvid or x264 C library to do client-side all-Flash Xvid or h264 video encoding (An "easier" solution would be AIR w/ NativeProcess + ffmpeg, but that kind of ruins the fun.). Anyway, just putting it out there...

- Source code for FlvEncoder
- Updated version on Github

Licensed under a Creative Commons Attribution 3.0 License.

89 Responses to “FLV Encoder with Audio”

  1. Designed to gauge curtains

  2. Marin Kraatz says:

    I’m really enjoying the design and layout of your website. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a designer to create your theme? Great work!

  3. I prefer this fabulous website it’s a learn piece! Thankful I discovered that in yahoo.

  4. Full Disclosure began in April 2012 when San Francisco comedian Eric Barry bought a mixer and some microphones, and launched the Full Disclosure sex podcast out of his bedroom, a series of intensely revealing interviews with adult performers, sexologists, celebrities, and others in the entertainment industry.

  5. Issac Maez says:

    An fascinating dialogue is worth comment. I think that it is best to write more on this matter, it may not be a taboo subject but usually individuals are not enough to speak on such topics. To the next. Cheers

  6. Hi there! I could have sworn I’ve been to this site before but after browsing through many of the articles I realized it’s new to me. Regardless, I’m certainly delighted I found it and I’ll be bookmarking it and checking back often!

  7. jenny says:

    Hello there! This blog post could not be written much better! Going through this article reminds me of my previous roommate! He always kept preaching about this. I will send this article to him. Fairly certain he’ll have a good read. Thank you for sharing!

  8. The next time I read a website, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  9. After looking at a few of the blog articles on your web site, I honestly like your way of writing a blog. I saved as a favorite it to my bookmark webpage list and will be checking back in the near future. Take a look at my web site too and tell me what you think.

  10. I couldn’t resist commenting. Well written!

  11. Nice post. I learn something new and challenging on websites I stumbleupon on a daily basis. It’s always exciting to read content from other authors and practice a little something from their web sites.

  12. Hi there! I could have sworn I’ve been to this website before but after looking at some of the articles I realized it’s new to me. Anyways, I’m certainly delighted I stumbled upon it and I’ll be bookmarking it and checking back often!

  13. Having read this I believed it was very informative. I appreciate you taking the time and energy to put this content together. I once again find myself personally spending way too much time both reading and leaving comments. But so what, it was still worthwhile!

  14. racetam says:

    Greetings! Very helpful advice in this particular article! It is the little changes that make the largest changes. Thanks for sharing!

  15. Aw, this was an exceptionally nice post. Finding the time and actual effort to produce a very good article… but what can I say… I hesitate a whole lot and don’t manage to get nearly anything done.

  16. After I initially commented I seem to have clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I recieve 4 emails with the exact same comment. Perhaps there is a way you can remove me from that service? Thank you!

  17. Fantastic goods from you, man. I have take note your stuff previous to and you are simply extremely excellent. I actually like what you have got right here, certainly like what you’re saying and the way in which by which you say it. You are making it enjoyable and you still take care of to stay it wise. I cant wait to learn much more from you. This is actually a terrific web site.

  18. I fondness this site Preteen Kds LOL I CAN’T Deem SHE IS Popular PORN HOLY SHIT!!!! I Get THE GIRL Near THE RIGHT…I Second-hand En route for WORK In support of HER MOM…HOLY DAMN…WELL Instantly SHE IS MARRIED SO YEAH LOL FUNNY SHIT HOLY DAMN.

  19. 850323 643113hello I was very impressed with the setup you used with this blog. I use blogs my self so congrats. definatly adding to favorites. 733340

  20. There’s certainly a great deal to find out about this subject. I love all the points you have made.

  21. Ruthe Tates says:

    Great work! That’s wonderful to hear. One trend that I’ve found particularly troublesome in regards to pollution has been those single use coffee things.

  22. eyelash says:

    I was able to find good advice from your articles.

  23. This site really has all of the info I needed concerning this subject and didn’t know who to ask. |

  24. This is a topic which is close to my heart… Thank you! Where are your contact details though?

  25. Lauren Koppy says:

    Hello! I could have sworn I’ve been to this website before but after browsing through some of the post I realized it’s new to me. Anyhow, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  26. What’s Taking place i am new to this, I stumbled upon this I’ve found It absolutely useful and it has aided me out loads. I am hoping to give a contribution & aid different customers like its helped me. Good job.|

  27. Dave says:

    Would you consider writing a tutorial on how to get this working?

  28. Great blog you have here.. It’s difficult to find excellent writing like yours nowadays. I truly appreciate people like you! Take care!!

  29. Good day! I could have sworn I’ve visited this web site before but after looking at many of the articles I realized it’s new to me. Anyways, I’m definitely delighted I discovered it and I’ll be bookmarking it and checking back often!

  30. Good post. I learn something totally new and challenging on blogs I stumbleupon on a daily basis. It’s always helpful to read through content from other authors and use something from other sites. |

  31. It is the best time to make some plans for the future and it is time to be happy. I have read this post and if I could I desire to suggest you few interesting things or suggestions. Maybe you can write next articles referring to this article. I want to read even more things about it!|

  32. fdpod.com says:

    We provide for both commercial and business use that utilize lightweight and sturdy materials, in addition to vibrant, long-lasting colors. Our innovative banner designs enable small business owners that ability to showcase their presence with a Fortune 500 professionalism at affordable pricings. Currently recognized as one of the top custom banner printing companies, New York Banner stands provides dedicated attention to our clients. Our design and marketing specialists are available to help you create laser-targeted banners for trade shows, sidewalk displays, POP displays and a variety of promotional events. Our roll up banners offer clients the convenience of continued use without worrying about storage or typical wear and tear. – See more at:

  33. Professional liability insurance is that the same as errors and omissions insurance (E&O) or Professional indemnity insurance. Errors and Omissions insurance (E & O) is for protection of professionals. Some professionals think that they have to buy only errors and omissions (E & O) insurance if it’s required by their client or by jurisdictions of law. If you recognize your clients might sue you for damages resulting from your negligence or error while performing your work, you must most definitely take into account this errors and omissions insurance even your clients aren’t asking for insurance certificate or not required by jurisdictions of law.

  34. Hi! This is my first comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading through your posts. Can you suggest any other blogs/websites/forums that go over the same subjects? Appreciate it!|

  35. This is a great tip particularly to those new to the blogosphere. Short but very precise info… Thanks for sharing this one. A must read post!|

  36. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! By the way, how could we communicate?|

  37. maduras says:

    An interesting discussion is definitely worth comment. I do believe that you need to write more on this topic, it might not be a taboo subject but typically people don’t discuss these issues. To the next! Kind regards!!

  38. I truly love your site.. Very nice colors & theme. Did you develop this website yourself? Please reply back as I’m hoping to create my very own site and want to learn where you got this from or what the theme is called. Many thanks!

  39. Wow, amazing blog structure! How long have you been blogging for? you make running a blog glance easy. The whole glance of your web site is magnificent, let alone the content material!

Leave a Reply