Skip to content

Instantly share code, notes, and snippets.

@ggendre
Created April 5, 2011 12:18
Show Gist options
  • Save ggendre/903492 to your computer and use it in GitHub Desktop.
Save ggendre/903492 to your computer and use it in GitHub Desktop.
FLV modification before play
//this code is able to load a FLV file, change stuff in his byteArray, and play the resulting FLV
//this could be helpfull to secure a web player for example, by obfuscating the flv sent by the server
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.URLRequest;
import flash.utils.ByteArray;
public var ns:NetStream;
public var nc:NetConnection;
public var originalBytes:ByteArray;
public var modifiedBytes:ByteArray;
private function flvLoadBytes():void{ //chargement du FLV en binaire et modification
flash.media.SoundMixer.stopAll();
//chargement...
var ur:URLRequest = new URLRequest('ex1.dub.flv');
var ul:URLLoader = new URLLoader();
ul.dataFormat = URLLoaderDataFormat.BINARY;
ul.addEventListener(Event.COMPLETE, function(ev:Event):void
{
//fichier chargé.
originalBytes = ul.data;
modifiedBytes = new ByteArray();
//copy header (header=13octets, scripttag=159octets)
originalBytes.readBytes(modifiedBytes,modifiedBytes.length,13+159);
var i:int=0;
var n:Number;
var tmpBytes:ByteArray;
//modification du flv.
while( originalBytes.bytesAvailable >= 531){
//on lit les 531 octets
tmpBytes=new ByteArray;
originalBytes.readBytes(tmpBytes,0,531);
if (i%2==0){
//on ne garde qu'un paquet de 531 sur deux
//s'ils sont ceux qu'il nous faut
//on les ecrits dans modifiedBytes (à la fin)
tmpBytes.readBytes(modifiedBytes,modifiedBytes.length,531);
}
i++;
}
//trace('taille = from '+originalBytes.length+' to '+modifiedBytes.length);
originalBytes.position=0;
flvPlayBytes(modifiedBytes);
});
ul.load(ur);
}
private function flvPlayBytes(bytes:ByteArray):void{ //joue le tableau de bytes d'un flv valide
nc = new NetConnection();
//chargement du flv modifié en mémoire.
nc.addEventListener(NetStatusEvent.NET_STATUS, function(event:NetStatusEvent):void{
switch(event.info.code)
{
case 'NetConnection.Connect.Success':
ns = new NetStream(nc);
ns.client = {};
ns.play(null);
ns.appendBytes(bytes);//attention, appendBytes après play
break;
}
});
nc.connect(null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment