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(0×33,0×33,0×33);
$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: , , ,

This entry was posted in php, ruby, utilities

3 Comments

  1. Arifur Rahman

    Hi,

    Do I need to add any library to execute above code? If so, where can I find that?

    Thanks
    Arif

  2. Yes you need the PHP Ming library. See http://www.php.net/ming for more details on how to get it installed.

  3. Ali Hassaan

    Hi,
    I am searching for the right code to create a custom video streaming player for flash files. I am not sure if this is the right one. But can you guide me with this ?

    thank you,

One Trackback

  1. [...] Now that you can create a streaming Flash video player with PHP or Ruby and you know add metadata for cuepoints to Flash videos you are ready for something else. The following code will show you how to create a video player with PHP that will watch for metadata events and display annotations contained inside the metadata either over the video itself or in a div on the same page as the movie. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>