Flash Streaming to FlowPlayer using only PHP

From RMBwiki

Jump to: navigation, search

This guide will show you how to get FlowPlayer working with a PHP-only replacement for lighthttpd+flv streamer module. The only caveat is you need to use flvtool2 to add MetaData, but that beats the Windows-only program by Barak(sp?) since flvtool2 is available for any platform that runs Ruby, which is all the good ones.

Contents

What is meant by streaming?

When I say we are going to be streaming, I mean two things:

  1. Seeking to any point in the video can be done instantly without downloading all the data between the beginning of the video file and the seek point.
  2. Bandwidth is throttled back to only slightly more than is needed to watch the video.

Can't this be done any other way?

There are a few methods for streaming flv files. They are usually standalone servers, and some of them cost a lot of money. If you already have a PHP-enabled website, or are not interested in paying a bunch of money, then the method on this page is going to be the easiest and fastest.

How to do it

Basically, we are going to be plugging in a php script to our web server, then doing the proper configuration of Flowplayer to use this script like a streaming server. The script we are going to be using is a modified version of the script found at 'Streaming' flv video via PHP, take two (flashguru.com), which is linked from the FlowPlayer website, but doesn't work with Flowplayer as-is.

Getting the code

flowplayer_streamer_php

Setup

Modify PHP Script

The first thing you need to do is modify two of the constants in the PHP script so that they match your installation. Near the top of the script you will find something like this:

	//------------------------------------------------------------------------------------------
	//	MEDIA PATH
	//
	//	you can configure these settings to point to video files outside the public html folder.
	//------------------------------------------------------------------------------------------
	
	// points to server root
	define('XMOOV_PATH_ROOT', $_SERVER['DOCUMENT_ROOT']);
	
	// points to the folder containing the video files. Should start and end with '/'
	define('XMOOV_PATH_FILES', '/videos/');

These two constants are mashed together to form the path that this script will use to find the video files. For example:

	define('XMOOV_PATH_ROOT', /securestorage');
	define('XMOOV_PATH_FILES', '/videos/');

Will cause the script to look for myvideo.flv in /securestorage/videos/myvideo.flv


If your videos are stored in part of your web tree, then you can leave XMOOV_PATH_ROOT set to $_SERVER['DOCUMENT_ROOT'] and just set XMOOV_PATH_FILES to whatever URL the videos are under.

So if your videos are accessible like this: http://www.myserver.com/videos/myvideo.flv

then these settings:

	define('XMOOV_PATH_ROOT', $_SERVER['DOCUMENT_ROOT']);
	define('XMOOV_PATH_FILES', '/videos/');

would likely work.


Why is this split into two constants when one will do?

It is handy for transferring code from a development to production server, also handy for people who may not know their docroot, or whose docroot might be changed by the server admin.


What is the point of this?

The streamer needs to know how to find the .flv files on your server, because we are going to be configuring Flowplayer to ask the streamer for the files, and the streamer needs to serve them up.

Configure Flowplayer

At this point, I am going to assume that you have installed the php file on your server, you have modified it as illustrated above, that it is called streamer.php, and it can be accessed on your website like this: http://www.example.com/streamer.php

If you configured everything right, you should be able to download myvideo.flv by going to http://www.example.com/streamer.php/myvideo.flv If you can do that, then you should be able to get Flowplayer working by telling Flowplayer that it is using lighthttpd streamer:

config={ streamingServer: 'lighttpd',
         videoFile: '/streamer.php/myvideo.flv'
       }

Of course that will work with playlists, too.

config={ streamingServer: 'lighttpd',
         playList: [ {url: '/streamer.php/myvideo.flv'},
                     {url: '/streamer.php/myothervideo.flv'},
                     {url: '/streamer.php/mythirdvideo.flv'} ],
       }

* Note: The config boxes above are Flowplayer configurations. If you haven't gotten started using flowplayer yet, you might be a little confused. Go get flowplayer working on your website without streaming first, then come back here and it will make more sense.

Bonus

Please don't try these until you have everything working.

  • Of course you can move streamer.php around, or rename it, so long as you point flowplayer to the proper url, which will always be {url to the streamer}/{name of video file}
  • Depending on your web server's configuration, you may be allowed to drop the .php from the video URL and still have everything work. For example, /streamer/myvideo.flv instead of streamer.php/myvideo.flv. This is a rewrite feature that is usually enabled on PHP-equipped web servers. Try it out!

Troubleshooting

I am getting a black screen and no video

  • Are you putting videos into subfolders?
    • If you doing something like this: /streamer.php/january/itscold.flv because you have videos sorted into subfolders beneath where you set your XMOOV_PATH_FILES constant, you should know that streamer.php will reject any video name with a / in it for security reasons. If you are smart enough to know that you don't need to take this precaution, then you are smart enough to remove that security check from the PHP file yourself.
  • If you are not using subfolders, point your browser to the url that flowplayer is trying to get the video from. Example: If Flowplayer is trying to play /streamer.php/myvideo.flv then point your browser to http://www.yourwebsite.com/streamer.php/myvideo.flv and see what there is to see:
    • If you get a 404 error from your server, but http://www.yourwebsite.com/streamer.php does not give a 404 error, then your web server is not configured properly for this exercise. Most apache installations with php will allow you to add /whatever after the .php It is so common that I don't even know what the feature is called, so I can't help you if it doesn't work for you, except to say "You do have php installed and working right?"
    • If you get an error from the streamer script that says basically "Could not find myvideo.flv, please check your settings", then either you misconfigured the paths in the php script, or you don't have myvideo.flv in the right place
    • If your browser tells you it is trying to download an FLV file, then I have no idea what is going on. It should be working. The only thing I can think of is that the FLV file is corrupted somehow and won't play anyway, so go fix the file.

It plays fine but I can't seek

  • Did you embed the metadata in your flv file using flvtool2?
  • Did you add
    streamingServer: 'lighttpd'
    to the config?
  • Did you spell "lighttpd" correctly?

When I try to seek, the video restarts

Check your streamer.php file. Somewhere in the middle you will see this:

	//------------------------------------------------------------------------------------------
	//	INCOMING GET VARIABLES CONFIGURATION
	//	
	//	use these settings to configure how video files, seek position and bandwidth settings are accessed by your player
	// 
	// NOTE TO Flowplayer users:  XMOOV_GET_POSITION must be set to 'start' to
	//                            work with Flowplayer, and the other two don't matter
	//------------------------------------------------------------------------------------------
	
	define('XMOOV_GET_FILE', 'file');
	define('XMOOV_GET_POSITION', 'start');
	define('XMOOV_GET_AUTHENTICATION', 'key');

Make sure that XMOOV_GET_POSITION is set to start

Personal tools