1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
| package {
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Camera;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.NetStreamAppendBytesAction;
import flash.net.Responder;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
/**
* @author BECKER Christophe
*/
public class Main extends Sprite {
//
public var vid : Video;
public var cam : Camera;
public var bt : Sprite;
public var txt : TextField ;
//
public var boucle : int;
public var flv_writer : FlvWriter;
//
public var nc : NetConnection;
public var responder : Responder;
//
public function Main() {
cam = Camera.getCamera();
cam.setMode(640, 480, 15);
bt = new Sprite();
var sh : Shape = new Shape();
sh.graphics.beginFill(0x000000);
sh.graphics.drawRect(0, 0, 640, 20);
sh.graphics.endFill();
bt.addChild(sh);
var txtFormat : TextFormat = new TextFormat();
txtFormat.color = 0xffffff;
txt = new TextField();
txt.defaultTextFormat = txtFormat;
txt.text = 'click me for start video recorer';
txt.autoSize = TextFieldAutoSize.LEFT;
txt.selectable = false;
txt.x = (sh.width / 2) - (txt.width / 2);
bt.addChild(txt);
bt.y = 0;
bt.addEventListener(MouseEvent.CLICK, run);
addChild(bt);
vid = new Video(cam.width, cam.height);
vid.attachCamera(cam);
vid.y = 30;
addChild(vid);
}
private function run(e : MouseEvent) : void {
flv_writer = FlvWriter.getInstance();
flv_writer.createFile(cam.width, cam.height, 5);
bt.removeEventListener(MouseEvent.CLICK, run);
txt.text = 'recording';
txt.x = (bt.width / 2) - (txt.width / 2);
this.addEventListener(Event.ENTER_FRAME, add_image);
}
private function add_image(e : Event) : void {
if (!cam.muted) {
trace("add_image");
boucle++;
if (boucle == 25) {
this.removeEventListener(Event.ENTER_FRAME, add_image);
nc = new NetConnection();
nc.connect(null);
var ns : NetStream = new NetStream(nc);
ns = new NetStream(nc);
ns.client = {};
ns.client.onMetaData = metaDataHandler;
vid.attachNetStream(ns);
ns.play(null);
ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
ns.appendBytes(flv_writer.byte);
} else {
add_picture();
}
}
}
private function metaDataHandler(meta : Object) : void {
if (vid) {
vid.width = meta.width;
vid.height = meta.height;
}
}
public function add_picture() : void {
var img : BitmapData = new BitmapData(cam.width, cam.height);
img.draw(vid);
flv_writer.image = img;
}
}
}
import flash.display.BitmapData;
import flash.utils.ByteArray;
/*
SimpleFlvWriter.as
Lee Felarca
http://www.zeropointnine.com/blog
5-2007
v0.8
Singleton class to create uncompressed FLV files.
Does not handle audio. Feel free to extend.
Source code licensed under a Creative Commons Attribution 3.0 License.
http://creativecommo...icenses/by/3.0/
Some Rights Reserved.
EXAMPLE USAGE:
var myWriter:SimpleFlvWriter = SimpleFlvWriter.getInstance();
myWriter.createFile(myFile, 320,240, 30, 120);
myWriter.saveFrame( myBitmapData1 );
myWriter.saveFrame( myBitmapData2 );
myWriter.saveFrame( myBitmapData3 ); // etc.
myWriter.closeFile();
*/
class FlvWriter {
static private var _instance : FlvWriter;
private var frameWidth : int = 320;
private var frameHeight : int = 240;
private var frameRate : Number = 15;
private var duration : Number = 10;
private const blockWidth : int = 32;
private const blockHeight : int = 32;
private var previousTagSize : uint = 0;
private var iteration : int = 0;
private var bmp : BitmapData;
//
private var out : ByteArray;
public static function getInstance() : FlvWriter {
if (FlvWriter._instance == null)
FlvWriter._instance = new FlvWriter(new SingletonEnforcer());
return FlvWriter._instance;
}
public function FlvWriter(singletonEnforcer : SingletonEnforcer) {
out = new ByteArray();
}
public function createFile(pWidth : int, pHeight : int, pFramesPerSecond : Number, pDurationInSeconds : Number = 0) : void {
frameWidth = pWidth;
frameHeight = pHeight;
frameRate = pFramesPerSecond;
duration = pDurationInSeconds;
out.writeBytes(header());
// create metadata tag
out.writeUnsignedInt(previousTagSize);
out.writeBytes(flvTagOnMetaData());
}
public function set image(pBitmapData : BitmapData) : void {
// bitmap dimensions should of course match parameters passed to createFile()
bmp = pBitmapData;
out.writeUnsignedInt(previousTagSize);
out.writeBytes(flvTagVideo());
}
public function get byte() : ByteArray {
return out;
}
private function header() : ByteArray {
var ba : ByteArray = new ByteArray();
ba.writeByte(0x46);
// 'F'
ba.writeByte(0x4C);
// 'L'
ba.writeByte(0x56);
// 'V'
ba.writeByte(0x01);
// Version 1
ba.writeByte(0x01);
// misc flags - video stream only
ba.writeUnsignedInt(0x09);
// header length
return ba;
}
private function flvTagVideo() : ByteArray {
var tag : ByteArray = new ByteArray();
var dat : ByteArray = videoData();
var timeStamp : uint = uint(1000 / frameRate * iteration++);
// tag 'header'
tag.writeByte(0x09);
// tagType = video
writeUI24(tag, dat.length);
// data size
writeUI24(tag, timeStamp);
// timestamp in ms
tag.writeByte(0);
// timestamp extended, not using ***
writeUI24(tag, 0);
// streamID always 0
// videodata
tag.writeBytes(dat);
previousTagSize = tag.length;
return tag;
}
private function videoData() : ByteArray {
var v : ByteArray = new ByteArray;
// VIDEODATA 'header'
v.writeByte(0x13);
// frametype (1) + codecid (3)
// SCREENVIDEOPACKET 'header'
// blockwidth/16-1 (4bits) + imagewidth (12bits)
writeUI4_12(v, int(blockWidth / 16) - 1, frameWidth);
// blockheight/16-1 (4bits) + imageheight (12bits)
writeUI4_12(v, int(blockHeight / 16) - 1, frameHeight);
// VIDEODATA > SCREENVIDEOPACKET > IMAGEBLOCKS:
var yMax : int = int(frameHeight / blockHeight);
var yRemainder : int = frameHeight % blockHeight;
if (yRemainder > 0) yMax += 1;
var xMax : int = int(frameWidth / blockWidth);
var xRemainder : int = frameWidth % blockWidth;
if (xRemainder > 0) xMax += 1;
for (var y1 : int = 0;y1 < yMax; y1++) {
for (var x1 : int = 0;x1 < xMax; x1++) {
// create block
var block : ByteArray = new ByteArray();
var yLimit : int = blockHeight;
if (yRemainder > 0 && y1 + 1 == yMax) yLimit = yRemainder;
for (var y2 : int = 0;y2 < yLimit; y2++) {
var xLimit : int = blockWidth;
if (xRemainder > 0 && x1 + 1 == xMax) xLimit = xRemainder;
for (var x2 : int = 0;x2 < xLimit; x2++) {
var px : int = (x1 * blockWidth) + x2;
var py : int = frameHeight - ((y1 * blockHeight) + y2);
// (flv's save from bottom to top)
var p : uint = bmp.getPixel(px, py);
block.writeByte(p & 0xff);
// blue
block.writeByte(p >> 8 & 0xff);
// green
block.writeByte(p >> 16);
// red
}
}
block.compress();
writeUI16(v, block.length);
// write block length (UI16)
v.writeBytes(block);
// write block
}
}
return v;
}
private function flvTagOnMetaData() : ByteArray {
var tag : ByteArray = new ByteArray();
var dat : ByteArray = metaData();
// tag 'header'
tag.writeByte(18);
// tagType = script data
writeUI24(tag, dat.length);
// data size
writeUI24(tag, 0);
// timestamp should be 0 for onMetaData tag
tag.writeByte(0);
// timestamp extended
writeUI24(tag, 0);
// streamID always 0
// data tag
tag.writeBytes(dat);
previousTagSize = tag.length;
return tag;
}
private function metaData() : ByteArray {
// onMetaData info goes in a ScriptDataObject of data type 'ECMA Array'
var b : ByteArray = new ByteArray();
// ObjectNameType (always 2)
b.writeByte(2);
// ObjectName (type SCRIPTDATASTRING):
writeUI16(b, "onMetaData".length);
// StringLength
b.writeUTFBytes("onMetaData");
// StringData
// ObjectData (type SCRIPTDATAVALUE):
b.writeByte(8);
// Type (ECMA array = 8)
b.writeUnsignedInt(7);
// // Elements in array
// SCRIPTDATAVARIABLES...
if (duration > 0) {
writeUI16(b, "duration".length);
b.writeUTFBytes("duration");
b.writeByte(0);
b.writeDouble(duration);
}
writeUI16(b, "width".length);
b.writeUTFBytes("width");
b.writeByte(0);
b.writeDouble(frameWidth);
writeUI16(b, "height".length);
b.writeUTFBytes("height");
b.writeByte(0);
b.writeDouble(frameHeight);
writeUI16(b, "framerate".length);
b.writeUTFBytes("framerate");
b.writeByte(0);
b.writeDouble(frameRate);
writeUI16(b, "videocodecid".length);
b.writeUTFBytes("videocodecid");
b.writeByte(0);
b.writeDouble(3);
// 'Screen Video' = 3
writeUI16(b, "canSeekToEnd".length);
b.writeUTFBytes("canSeekToEnd");
b.writeByte(1);
b.writeByte(int(true));
var mdc : String = "SimpleFLVWriter.as v0.8 zeropointnine.com";
writeUI16(b, "metadatacreator".length);
b.writeUTFBytes("metadatacreator");
b.writeByte(2);
writeUI16(b, mdc.length);
b.writeUTFBytes(mdc);
// VariableEndMarker1 (type UI24 - always 9)
writeUI24(b, 9);
return b;
}
private function writeUI24(stream : *, p : uint) : void {
var byte1 : int = p >> 16;
var byte2 : int = p >> 8 & 0xff;
var byte3 : int = p & 0xff;
stream.writeByte(byte1);
stream.writeByte(byte2);
stream.writeByte(byte3);
}
private function writeUI16(stream : *, p : uint) : void {
stream.writeByte(p >> 8);
stream.writeByte(p & 0xff);
}
private function writeUI4_12(stream : *, p1 : uint, p2 : uint) : void {
// writes a 4-bit value followed by a 12-bit value in two sequential bytes
var byte1a : int = p1 << 4;
var byte1b : int = p2 >> 8;
var byte1 : int = byte1a + byte1b;
var byte2 : int = p2 & 0xff;
stream.writeByte(byte1);
stream.writeByte(byte2);
}
}
class SingletonEnforcer {
}
/*
FLV structure summary:
header
previoustagsize
flvtag
[info]
videodata
[info]
screenvideopacket
[info]
imageblocks
imageblocks
...
previoustagsize
flvtag
...
FLV file format:
header
last tag size
FLVTAG:
tagtype
datasize
timestamp
timestampextended
streamid
data [VIDEODATA]:
frametype
codecid
videodata [SCREENVIDEOPACKET]:
blockwidth ub[4]
imagewidth ub[12]
blockheight ub[4]
imageheight ub[12]
imageblocks [IMAGEBLOCKS[]]:
datasize ub[16] <same as 'ub16', i think>
data..
last tag size
FLVTAG
etc.
*/ |
Partager