Skip to content
This post
Filed neatly under:
Code, Geekery
Meticulously tagged with:
, , , , , , , , , , , , ,
Share this
  • Digg
  • Facebook
  • Twitter
  • Delicious
  • Newsvine
  • StumbleUpon
Related posts
Taxonomy
Archive
Short
Exchange

Converting to Flash video — (almost) free and not so easy

19Jul20071055

How do you con­vert an arbit­rary video file into a play­able Flash video using freely avail­able pro­grams and meth­ods? After close to an after­noon of search­ing, test­ing and head-scratching, I finally have a whole answer that can be applied ad-hoc to almost any video you can get your hands on.

This “guide” (more anec­dotal than how-to) assumes know­ledge of video encod­ing basics, I’m not going to cover the dif­fer­ence between con­tainer and video formats or how to use Vir­tu­al­Dub, there are plenty of other tutori­als and guides that cover those top­ics.

The Flash Video con­tainer format (FLV) sup­ports two major video formats: the first is code­named “Soren­son Spark” and is a vari­ant on the H.263 stand­ard; the second is “On2 True­Mo­tion VP6”. The former is well sup­por­ted in many encod­ing and decod­ing tools and lib­rar­ies, the lat­ter isn’t. It will come as no sur­prise then that the lat­ter allows for far greater com­pres­sion at sim­ilar visual qual­ity when com­pared apples-to-apples to the H.263 format. To give you a quan­ti­fi­able meas­ure of this: I man­aged to get more than 2 times greater com­pres­sion and bet­ter visual qual­ity when using the VP6 codec. In short, this is the codec you want to use to get the most out of your movies.

Quick and dirty
If you just want to get a com­pat­ible FLV file quickly and pain­lessly and aren’t wor­ried about size or qual­ity over­much, then grab your­self a copy of ffm­peg (a recent com­piled Win­dows bin­ary can be found here) and put it in a place where your command-line of choice can find it. Then punch in:

ffmpeg -i "yourvideofilegoeshere.avi" outfile.flv

Voila, in no time flat you’ll have an all-singing all-dancing .flv file ready for whatever you have in store for it. If you’re feel­ing par­tic­u­larly awe­some, you can even con­trol the out­put size of the video:

ffmpeg -i "totallyawesomekittenvideo.avi" -s 320x240 outfile.flv

ffm­peg, con­verts using the H.263 video fla­vour and MP3 audio format which is prob­ably fine for most people. The prob­lem with this pro­cess is the out­put is less than stel­lar and suf­fers from a tre­mend­ous amount of arte­fact­ing. I wanted to exer­cise a little more con­trol over the visual qual­ity. Some search­ing revealed byz­antine quant­izer set­tings which made the com­mand line look like a cal­cu­lator had exploded:

ffmpeg -i "hahathatguytotallysucks.avi" -qcomp 0.6 -qmax 15 -qdiff 4 -i_qfactor 0.71428572 -b_qfactor 0.76923078 -maxrate 972800 -s 320x240 -b 819200 -refs 1 -subq 1 -y outfile.flv

Tweak­ing these options gives vari­able res­ults, but noth­ing close to the kind of qual­ity / file-size ratio I wanted. The VP6 codec seemed worth try­ing out. Unfor­tu­nately using the VP6 codec is rife with hurdles; the primary one is that it is entirely pro­pri­et­ary, On2 own licenses and pat­ents and prob­ably cro­codiles with bazookas to pro­tect the codec; some com­pan­ies have obtained licenses to use it in their products (On2 of course hav­ing their own imple­ment­a­tion) which means the easi­est and most pain-free route is to buy one of those products and bask in the fully-licensed glory.

What isn’t widely pub­li­cised is that On2 released a ver­sion of the VP6 codec for “Per­sonal use” but no longer provide it for down­load on their web­site. A curs­ory search on Google (lets say “vp6 vfw codec”) returns some good matches. After down­load­ing and installing, I now had the abil­ity to encode to VP6 as long as it’s for “Per­sonal use” accord­ing to the license agree­ment. This little endeav­our was for my own curi­os­ity rather than mon­et­ary gain which I’m sure falls under that stipulation.

Codec in hand, in the­ory it should be as simple as encod­ing to VP6 using some­thing like Vir­tu­al­Dub and then mux­ing everything together into an FLV file. If only things were that simple. As far as I could see, there exists no stan­dalone set of FLV mux­ing tools (like the sem­inal MKVtool­nix suite). How­ever, ffm­peg can out­put to an FLV file and provides the abil­ity to do a straight copy (i.e. no transcod­ing) of the source video, that could work…

No.

For the VP6 codec to be recog­nised within an FLV file, the con­tainer needs to have spe­cial bits set which indic­ate to the player that it’s going to receive VP6 video con­tent rather than H.263/Spark; ffm­peg doesn’t write these bits as it doesn’t “offi­cially” deal with VP6. After much search­ing, I stumbled upon a way to modify ffm­peg to write these bits but the patch hasn’t been merged into the main ffm­peg branch yet. You can down­load the patch and a pre-compiled Win­dows bin­ary from a blog which also offers an altern­ate method of achiev­ing what I’m describing.

The crux
If you haven’t been keep­ing up, here’s the short ver­sion of it. We want to make an FLV using VP6. Vanilla ffm­peg doesn’t do this so we need a mod­i­fied ffm­peg to do it. Use the above blog page or modify your ffm­peg source to get ffm­peg to do what we want.

Open up your source video in Vir­tu­al­Dub and apply any fil­ters you want (con­trast, bright­ness, res­ize etc.) but make sure the “Flip ver­tic­ally” fil­ter is some­where in that mix. Go to com­pres­sion and select the VP6 codec; if you’re using VP 6.2 you can do two-pass encod­ing which, poten­tially gives bet­ter res­ults than a one-pass encode. Make sure “Use source audio” is selec­ted and save your video down. You’ll have an .avi file which has VP6 video and the ori­ginal source audio.

Now use your mod­i­fied ver­sion of ffm­peg and use the fol­low­ing (sub­sti­tut­ing file­names where applicable):

ffmpeg -y -i "ooooohprettyprettyflowers.avi" -vcodec copy outfile.flv

If you want to con­trol the audio com­pres­sion a little bet­ter add the options for that:

ffmpeg -b 128 -ac 2 -ar 44100 -y -i "ohwowexplosions.avi" -vcodec copy outfile.flv

You will now have “outfile.flv” which is your final Flash video file, ready for upload­ing. Of course, the proof of the pud­ding is in the tasting:

Get Flash to see this player.

h263.flv — 1,466KB

Get Flash to see this player.

vp62.flv — 676KB

Con­clu­sion
This pro­cess is obvi­ously not suited for the auto­matic encod­ing pro­cess that a lot of sites seem to crave nowadays, this pro­cess is far bet­ter suited for the cash-strapped auteur who wants the most out of their videos and band­width and doesn’t have a large amount of videos to encode. VP6 sup­port in ffmpeg/libavcodec is com­ing along, and the most recent builds of ffm­peg come with decod­ing sup­port for VP6, but whether patents/license pre­vent encod­ing sup­port is still to be seen. 

Responses and trackbacks have been turned off for this post.
<
&rt;