YouTube selfmade – Part 2

As promised in the first part, we want to do the stunt ourselves – but as I do not own a copy of Flash nor someone was willing to donate a license, I have to do it in PHP using the MING Library. But let’s stop ranting and get down to a text editor:

setDimension(480, 360);
$movie->Background(0,0,0);

What we did here is the start of a small php script that generates an empty flash object we’ll be using as our workspace. Its size is 480 by 360 pixel. As I’m damn creative, I just call it ‘video’ and start adding some more code for embedding the video ‘screen’ into it:

$stream = new SWFVideoStream();
$stream->setDimension(480, 350);
$item = $movie->add($stream);
$item->moveTo(5, 5);
$item->setname("video");

Now our scene is set and ready for the flv file we know from the first post. In Flash we’d be using ActionScript. I guess it would look that way:


connection = new NetConnection();
connection.connect(null);
stream = new NetStream(connection);
video.attachVideo(stream);
stream.setBufferTime(10);
stream.play('http://ourhost/mystream.flv');

For using that code in PHP, I just smack it into a variable and add it as SWFAction to our project. Regarding the location of our flv file: Feel free to be creative – I’m just using a static location here as it’s just a demo.

$action = new SWFAction($action_string);
$movie->add($action);

Ok people – Now the important part: as I am a man, I need something to play with while watching the video. How do buttons sound? Controls?

Let’s fire up gimp and paint them. If you’re nice, save them as DBL graphics or just use the png2dbl tool that came shipped with ming. As I’m done with the paint job now, let’s hack it into our player:

$button = new SWFButton();
$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);

And that was the easy part because I want a progress bar. As we have already quit our paint program, we’ll be doing it using PHP:

$mc = new SWFSprite();
$shape = new SWFShape();
$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);

And now let’s move it:

$a = new SWFAction("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);

The progress bar is now drag able, but it still doesn’t work until we add some more code:

// width in px
width = xMax - xMin;

paused = false;
if(drag) {
// pause stream while seeking
_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;
}

// restart paused stream
if(paused) {
_global.stream.pause(false);
}

And that’s it. Let’s finish the movie by skipping to the next frame. Regarding my demo script here, I’ll just write a swf file as I have hardcoded the FLV file location. Alternatively you might want to make php send the swf file to your clients.

$movie->nextFrame();
$movie->save("FLVPlayer.swf");

The moral of the story: There’s no magic behind those video portals. It’s just some lines of code, darn simple and stupid.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *