Video and Audio Streaming with Flash and Open Source Tools.docx
《Video and Audio Streaming with Flash and Open Source Tools.docx》由会员分享,可在线阅读,更多相关《Video and Audio Streaming with Flash and Open Source Tools.docx(50页珍藏版)》请在冰豆网上搜索。
VideoandAudioStreamingwithFlashandOpenSourceTools
1.VideoandAudioStreamingwithFlashandOpenSourceTools
©2005KlausRechert
http:
//blogs.ugidotnet.org/kfra/archive/2006/10/04/50003.aspx
Flashhasalwaysbeendevelopedandusedformultimediapurposes,butuntilversion6thepossibilitiesforaudiostreamingwerelimitedandalsotherewasnovideosupport.WithVersion6and7Macromediaintroducedvideosupportandanewfileformattosupportvariouswaysofstreaming.Thisarticlecoversonlyastreamingvariantcalled"progressivedownload"whichdoesnotneedserversupport.TruestreamingsupportisavailablewithMacromedia'snon-freeFlashCommunicationServer(FCS).
FLVStreams
StreamingisbuilduponanewfileformatcalledFLV,whichseparatesthestreamablecontentandtheflashmovie.Theresultisaverycompactflashmovieactingasamultimediaplayerandastorageforstreamablecontentfromwhichtheflashmovieloadsastreamondemand.
AsingleFLVstreamcontainsatmostoneaudiostreamandatmostonevideostream.FlashsupportsuncompressedsoundandvariouscompressedformatslikeMP3andADPCMaswellastheproprietaryNellymoseraudiocodec.Withflashversion6Macromediaalsointroducedvideosupportforflash.Inversion6onlytheSorensonH.263videocodecwassupportedwhichisaslightlymodifiedversionoftheopenH.263standard.Thelatestflashversion7introducedasecondvideoformat"ScreenVideo",whichisasimple,losslessvideoformat,especiallydevelopedforscreencapturing.
ConvertingandCreatingContent
OnemethodforcreatingFLVstreamsisconvertingexistingaudioandvideocontentwithFFmpeg[].FFmpegisamatureandveryexcellentsoftwareprojectforconvertingaudioandvideofromandtovariousformats.Convertingavideocanbesimplydonewith
ffmpeg-iinfile.[avi|mpeg]stream.flv
FFmpegusestheSorensonH.263videoformatforencodingvideodata.ThereisnosupportfortheScreenVideoformatatthistime.WhiletheScreenVideoformatismainlyusefulforscreencapturingapplications,SorensonsH.263ismultipurposevideocodecwithgoodcompressionrates,suitableespeciallyforencodingmotionpictures.
AnotherprojectdealingwithFLVstreamsiscalledlibflv[].WhileFFmpegisageneralaudioandvideoconvertingsuite,libflvisfocusedonworkingwithFLVstreams.Theprojectisstillinaveryearlystage,butisableencodingvideosintheScreenVideoformatandallowssimpleFLVstreammanipulationslike(de-)multiplexingofaudioandvideostreams.AsimpleGTK-basedscreencapturingapplicationcanbefoundintheexampledirectory.
BuildingaSimpleMultimediaPlayer
Afterhavingcreatedsomestreamablecontent,aflashmultimediaplayerisneeded.Onehugeadvantageofflashbasedplayersoverotherplug-inbasedmultimedia-playersisthattherearenoconstraintsaboutitslookandhowitisintegratedinyoursitesdesign.
MINGisanopensourcelibrarywhichisabletocreateflashfileswithalmostallrecentflashfeatures,includingActionScript,soundandvideosupport.Thelibraryalsohaslanguagebindingsforabunchofscriptandprogramminglanguages.TheexamplespresentedinthisarticlearewritteninPHP4.PortingtheexamplestoothersupportedlanguageslikeC/C++,Java,PythonorPerlshouldbetrivial.
TorunthefollowingexampleacurrentCVSsnapshotofMINGisneeded.ItisavailableeitherviaSourceforgesanonymousCVSserviceorpre-packagedat
Firstwecreateanewmoveinstanceandsetdimensionandbackgroundcolor:
ming_useswfversion(7);$movie=newSWFMovie(7);
$movie->setDimension($width,$height);$movie->Background($r,$g,$b);
Thenewflashmovecannowbefilledwithflashobjectscalledcharacters.Forthemultimediaplayerexamplewecreateavideocanvasobjectandaddittothemovie.Theadd()methodtakesacharacterandinsertsittothecurrentframeandreturnsahandletotheobject.Thiscanbeusedtomove,rotate,resizeorremoveanobject.IftheobjectisgoingtobeusedwithActionScript,anamecanbeassignedtoit.
$stream=newSWFVideoStream();$stream->setDimension($width,
$height);$item=$movie->add($stream);$item->moveTo($x,$y);
$item->setname("video");
TheSWFVideoStream()constructorcanalsotakeaFLVfileasargument.Inthiscasethevideostreamwillbeembeddedtotheflashfile.Howeverthisapproachhassomedrawbacks.Firstofalltheresultingflashmoviewillgetasleastasbigasthestream.Butalsothestream'sframeratemustnotexceedtheflashmoviesframerateandeachflashfileislimitedto16000frames,whichmeansthattheembeddedstreamcancontainatmost16000frames.
Amultimediaplayerapplicationshouldbeabletoloadandplaystreamsdynamically.ThereforetheSWFVideoStream()constructoriscalledwithnoarguments.Thusonlyanemptyvideocanvaswillbecreated,whichwillbecontrolledbythefollowingActionScriptcode:
connection=newNetConnection();
connection.connect(null);
stream=newNetStream(connection);
video.attachVideo(stream);
stream.setBufferTime(10);
stream.play('http:
//localhost/mystream.flv');
TheActionScriptfirstcreatesapseudoconnectionbypassingnulltotheconnect()methodoftheNetConnectionobject.Incontrast,arealconnectiontoaMacromediastreamingservercanbemadebypassingavalidurltothemethod.HavingaNetConnectioninstanceanewNetStreamobjectcanbecreatedandattachedtotheemptyvideocanvas.Thisobjecthandlesstreamingandprovidesmethodsforcontrollingthestream.TheaboveexampleloadsaFLVstreamfromthelocalwebserverwithadownloadbufferof10seconds.TheActionScriptcodecanbecompiledandaddedtothemoviewith:
$action=newSWFAction($action_string);$movie->add($action);
UntilnowtheflashmoviejustloadsandplaysacertainFLVstream.Tocontrolitsbehavior,asimpleuserinterfaceconsistingofsomebuttonsandaseek-sliderismissing.FlashhasitsowncompressedlosslessbitmapformatcalledDBL.MINGprovidesasmallutilitypng2dbltoconvertPNGimagestoDBL.Suchimagesareusedfortheplayer'scontrolbuttons:
$button=newSWFButton();$flags=(SWFBUTTON_UP|SWFBUTTON_HIT|
SWFBUTTON_OVER|SWFBUTTON_DOWN);
$button->addShape(ImageShape("images/pause.dbl"),$flags);$action=new
SWFAction("stream.pause();");$button->addAction($action,
SWFBUTTON_MOUSEDOWN);$button_ref=$movie->add($button);
$button_ref->moveTo($x,$y);
Theaboveexamplecreatesapausebuttonforthemultimediaplayer.Aninteractivebuttoniscreatedintwosteps.Firstitslookhastobedefinedbyaddingshapesforcertainmouseevents.Inflashashapeisthebasicrepresentationforgraphicobjects.Foreachmouseeventadifferentshapeobjectcanbeassignedtothebutton.Intheaboveexamplethebuttonlooksalwaysthesame.
InthesecondstepthebuttonsactioncanbedefinedbyassigningActionScripttoaspecialevent.
Onedrawbackusingprogressivedownloadstreamingwithoutserversupportisthatthereisnopossibilitytogetthestream'stotallength.Thereforetheseek-slidersfunctionalityislimitedtoseekingwithinthealreadyloadedpartsofthestream.
Thedragablepartoftheseek-sliderisrealizedasamovie-clipobject.Amovie-clipisrunningasanindependentmovieintheflashmovie.Ithasanindependenttimeline,canhandlesscriptsandhandlesexternaleventsitself.
$mc=newSWFSprite();$shape=newSWFShape();
$shape->setLine(4,25,0,0,128);$shape->movePenTo(0,5);
$shape->drawLineTo(0,10);$mc->add($shape);$mc->nextFrame();$slider
=$movie->add($mc);$slider->moveTo($xMin,$y);
Amovieclip(SWFSprite)hassimilarmethodslikeamovieobject.Theadd()methodinsertsaflashobjecttothecurrentframe,nextFrame()finishesthecurrentframeandcreatesanewone.Themovieclipisalsoanormalflashobjectwhichcanbeaddedtoamovieandplacedonthestage.Thefunctionalityoftheseek-sliderisdefinedbythreesmallscripts.Thefirsttwoactionsmakethemovie-clipdragable:
$a=newSWFAction("startDrag(this,$xMin,$y,$xMax,$y,1);drag=true;");$slider->addAction($a,SWFACTION_MOUSEDOWN);
$a=new
SWFAction("stopDrag();drag=flase;");$slider->addAction($a,SWFACTION_MOUSEUP);
Thethirdmorelengthyscriptsetsthestreampositiondependingonslidersx-positionifthesliderisactuallymovedbytheuserorsetstheslidersx-positiondependingonthestreamscurrenttime:
//widthinpxwidth=xMax-xMin;paused=false;if(drag){//pause
streamwhileseeking_global.stream.pause(true);paused=true;x=
_root._xmouse-xMin;seekTo=(_global.streamLen/width)*x;
_global.stream.seek(seekTo);}else{pos=(_global.stream.time*(width/
_global.streamLen))+xMin;this._x=pos;this._y=y;}//restartpaused
streamif(paused){_global.stream.pause(false);}
Thisscriptisassignedtothe$slider-handlewiththeSWFACTION_ENTERFRAMEevent.
AfterhavingaddedallelementstotheflashmoviethefirstframehastobeclosedwiththenextFrame()call.Sincewedonnotneedanotherframethemoviecanalsobefinished:
$movie->nextFrame();$movie->save("FLVPlayer.swf");
Theresultingmultimediaplayer
ExampleVideo:
CopyrightbyThiloWeigel,UniversityofFreiburg
Conclusion
Withflashitiseasytocreatealightweight,fullycustomized,embeddedvideoandaudioplayer.Therearepowerfulopensourcetoolsavailableforcreatingcontentandalsocreatingflashmovies.ThisarticleintroducedthebasicconceptsofflashstreamingandworkingwithMING.Theherepresentedmediaplayerprovidesonlythemostbasicfeaturesandwasonlyintendedasasimpleexamp