How to Create a Streaming Flash Video Player Using Ming PHP or Ruby

I mentioned in creating Flash videos using FFMpeg that you could use Ming to create your own Flash video player. I've added a patch to the ruby -ming extension for video streaming so now it is possible to create a streaming player with both PHP and Ruby using their Ming extensions. The following examples show you how.

First a little background. You can read about playing back external FLV files dynamically with actionscript at the Macromedia website. It includes references to the NetConnect class and NetStream class that together let you stream a FLV file. The main thing Ming does for you in the following simple examples is give you a SWF with a video player in it and let you attach some action script to it.

Here is how you do it in PHP:

<?php
Ming_setScale(10.0000000);
ming_useswfversion(7);

$movie = new SWFMovie(7);
$movie->setDimension(320,240); 
$movie->setBackground(0x33,0x33,0x33);
$movie->setRate(8);

$strAction = "
stop();
netConn=new NetConnection();
netConn.connect(null);
vStream=new NetStream(netConn);
video1.attachVideo(vStream);
vStream.setBufferTime(10);
vStream.play('http://localhost/test.flv');
";

$stream = new SWFVideoStream();
$item=$movie->add($stream);
$item->setname("video1");
$movie->add(new SWFAction($strAction));
$movie->nextFrame();

$movie->save("videostream.swf");
?>

And here is how you do it with Ruby:

#!/usr/bin/env ruby

require 'ming/ming'
include Ming

Ming::set_scale(10.0000000)

movie = SWFMovie.new(7)
movie.set_background(0xff, 0xff, 0xff)
movie.set_dimension(320, 240)
movie.set_rate(8)

strAction = '
stop();
netConn=new NetConnection();
netConn.connect(null);
vStream=new NetStream(netConn);
video1.attachVideo(vStream);
vStream.setBufferTime(10);
vStream.play(\'http://localhost/test.flv\');
'

vstream = SWFVideoStream.new
item = movie.add(vstream)

item.set_name("video1")
movie.add(SWFAction.new(strAction))
movie.next_frame()

movie.save("videostream.swf")

Here is the result of the above examples:

Tags: , , ,

Leave a Reply

Your email address will not be published.