注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

phperwuhan的博客

记载一个phper的历程!phperwuhan.blog.163.com

 
 
 

日志

 
 

使用 php文件读取视频stream并用flowplayer播放  

2010-03-31 12:29:26|  分类: 流媒体 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
来源:http://faceye.com/wiki/topic/402881b12625f1060126263b9a5311a5

php文件读取视频留可以原创文章:

http://richbellamy.com/wiki/Flash_Streaming_to_FlowPlayer_using_only_PHP

下 面我想说说我是怎么配置成功的:

1. 下载读取视频stream的streamer.php文件:

http://richbellamy.com/wiki/Flowplayer_streamer_php

php 代码如下,仔细红色的配置部分:

<?
/*

xmoov-php 0.9
Development version 0.9.3 beta

by: Eric Lorenzo Benjamin jr. webmaster (AT) xmoov (DOT) com
originally inspired by Stefan Richter at flashcomguru.com
bandwidth limiting by Terry streamingflvcom (AT) dedicatedmanagers (DOT) com
Flowplayer compatibility by Rich Bellamy rich (@+) rmbwebs d0t com

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.
For more information, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
For the full license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.


*/


// SCRIPT CONFIGURATION

//------------------------------------------------------------------------------------------
// MEDIA PATH
//
// you can configure these settings to point to video files outside the public html folder.
//------------------------------------------------------------------------------------------

// points to server root

//定义你的根目录,一般为你的网站根目录下面的第一个文件,下面这个配置 则代表的是http://www.malacity.net/example. 注意前面都没有斜杠
define('XMOOV_PATH_ROOT','example');

// points to the folder containing the video files. Should start and end with '/'

//下面配置的 是视频的所在目录 则以下配置后为http://. 注意前后都有斜杠www.malacity.net/example/videos/
define('XMOOV_PATH_FILES', '/videos/');



//------------------------------------------------------------------------------------------
// SCRIPT BEHAVIOR
//------------------------------------------------------------------------------------------

//set to TRUE to use bandwidth limiting.
define('XMOOV_CONF_LIMIT_BANDWIDTH', TRUE);

//set to FALSE to prohibit caching of video files.
define('XMOOV_CONF_ALLOW_FILE_CACHE', FALSE);



//------------------------------------------------------------------------------------------
// BANDWIDTH SETTINGS
//
// these settings are only needed when using bandwidth limiting.
//
// bandwidth is limited my sending a limited amount of video data(XMOOV_BW_PACKET_SIZE),
// in specified time intervals(XMOOV_BW_PACKET_INTERVAL).
// avoid time intervals over 1.5 seconds for best results.
//
// you can also control bandwidth limiting via http command using your video player.
// the function getBandwidthLimit($part) holds three preconfigured presets(low, mid, high),
// which can be changed to meet your needs
//------------------------------------------------------------------------------------------

//set how many kilobytes will be sent per time interval
define('XMOOV_BW_PACKET_SIZE', 90);

//set the time interval in which data packets will be sent in seconds.
define('XMOOV_BW_PACKET_INTERVAL', 0.3);

//set to TRUE to control bandwidth externally via http.
define('XMOOV_CONF_ALLOW_DYNAMIC_BANDWIDTH', TRUE);

define('XMOOV_GET_BANDWIDTH', 'bw');

//------------------------------------------------------------------------------------------
// DYNAMIC BANDWIDTH CONTROL
//------------------------------------------------------------------------------------------

function getBandwidthLimit($part)
{
switch($part)
{
case 'interval' :
switch($_GET[XMOOV_GET_BANDWIDTH])
{
case 'low' :
return 1;
break;
case 'mid' :
return 0.5;
break;
case 'high' :
return 0.3;
break;
default :
return XMOOV_BW_PACKET_INTERVAL;
break;
}
break;
case 'size' :
switch($_GET[XMOOV_GET_BANDWIDTH])
{
case 'low' :
return 10;
break;
case 'mid' :
return 40;
break;
case 'high' :
return 90;
break;
default :
return XMOOV_BW_PACKET_SIZE;
break;
}
break;
}
}



//------------------------------------------------------------------------------------------
// 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');



// END SCRIPT CONFIGURATION - do not change anything beyond this point if you do not know what you are doing



//------------------------------------------------------------------------------------------
// PROCESS FILE REQUEST
//------------------------------------------------------------------------------------------

if (isset($_SERVER['PATH_INFO']) || isset($_GET[XMOOV_GET_FILE]))
{
// PROCESS VARIABLES

# get seek position
if (isset($_GET[XMOOV_GET_POSITION])) {
$seekPos = intval($_GET[XMOOV_GET_POSITION]);
} else {
$seekPos = 0;
}

# get file name
if (isset($_SERVER['PATH_INFO'])) {
$fileName = htmlspecialchars(ltrim($_SERVER['PATH_INFO'], "/"));
} else {
$fileName = htmlspecialchars($_GET[XMOOV_GET_FILE]);
}

# assemble file path
$file = XMOOV_PATH_ROOT . XMOOV_PATH_FILES . $fileName;

# assemble packet interval
$packet_interval = (XMOOV_CONF_ALLOW_DYNAMIC_BANDWIDTH && isset($_GET[XMOOV_GET_BANDWIDTH])) ? getBandwidthLimit('interval') : XMOOV_BW_PACKET_INTERVAL;
# assemble packet size
$packet_size = ((XMOOV_CONF_ALLOW_DYNAMIC_BANDWIDTH && isset($_GET[XMOOV_GET_BANDWIDTH])) ? getBandwidthLimit('size') : XMOOV_BW_PACKET_SIZE) * 1042;

# security improved by by TRUI www.trui.net
if (!file_exists($file))
{
print('<b>ERROR:</b> xmoov-php could not find ('.$file.') please check your settings.');
exit();
}
if(file_exists($file) && strrchr($fileName, '.') == '.flv' && strlen($fileName) > 2 && !eregi(basename($_SERVER['SCRIPT_NAME']), $fileName) && ereg('^[^./][^/]*$', $fileName))
{
$fh = fopen($file, 'rb') or die ('<b>ERROR:</b> xmoov-php could not open (' . $fileName . ')');

$fileSize = filesize($file) - (($seekPos > 0) ? $seekPos + 1 : 0);

// SEND HEADERS
if(!XMOOV_CONF_ALLOW_FILE_CACHE)
{
# prohibit caching (different methods for different clients)
session_cache_limiter("nocache");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
}

# content headers
header("Content-Type: video/x-flv");
header("Content-Disposition: attachment; filename=\"" . $fileName . "\"");

# FLV file format header
if($seekPos != 0)
{
header("Content-Length: " . ($fileSize + 13));
print('FLV');
print(pack('C', 1));
print(pack('C', 1));
print(pack('N', 9));
print(pack('N', 9));
}
else
{
header("Content-Length: " . $fileSize);
}

# seek to requested file position
fseek($fh, $seekPos);

# output file
while(!feof($fh))
{
# use bandwidth limiting - by Terry
if(XMOOV_CONF_LIMIT_BANDWIDTH)
{
# get start time
list($usec, $sec) = explode(' ', microtime());
$time_start = ((float)$usec + (float)$sec);
# output packet
print(fread($fh, $packet_size));
# get end time
list($usec, $sec) = explode(' ', microtime());
$time_stop = ((float)$usec + (float)$sec);
# wait if output is slower than $packet_interval
$time_difference = $time_stop - $time_start;
if($time_difference < (float)$packet_interval)
{
usleep((float)$packet_interval * 1000000 - (float)$time_difference * 1000000);
}
}
else
{
# output file without bandwidth limiting
while (!feof($fh)) {
print fread($fh, 8192);
}

}
}

}

}
?>

2 . 上传一个myvideo.flv文件到videos文件夹中

3. 配置完后你可以通过测试:

http://www.malacity.net/streamer.php/myvideo.flv

查 看是否可以下载你所上传的文件?如果能下载则表面你创建的tream成功了.

4. 配置flowplayer:注意红色说明.

<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- A minimal Flowplayer setup to get you started -->

<!--
include flowplayer JavaScript file that does
Flash embedding and provides the Flowplayer API.
-->
<script type="text/javascript" src="flowplayer-3.1.4.min.js"></script>

<!-- some minimal styling, can be removed -->
<link rel="stylesheet" type="text/css" href="style.css">

<!-- page title -->
<title>Minimal Flowplayer setup</title>

</head><body>

<div id="page">

<h1>Minimal Flowplayer setup</h1>

<p>View commented source code to get familiar with Flowplayer installation.</p>

<!-- this A tag is where your Flowplayer will be placed. it can be anywhere -->
<a
style="display:block;width:520px;height:330px"
id="player">
</a>

<!-- this will install flowplayer inside previous A- tag. -->
<script>
flowplayer("player", "plugin/flowplayer-3.1.5.swf",{
clip: {

//此处就是播放器读出的stream文件
url: 'http://www.malacity.net/streamer.php/iPhone.flv',
provider: 'lighttpd'
},

// streaming plugins are configured normally under the plugins node
plugins: {
lighttpd: {
url: 'flowplayer.pseudostreaming-3.1.3.swf'
}
}
});
</script>



<!--
after this line is purely informational stuff.
does not affect on Flowplayer functionality
-->

<p>
If you are running these examples <strong>locally</strong> and not on some webserver you must edit your
<a href="http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html">
Flash security settings</a>.
</p>

<p class="less">
Select "Edit locations" &gt; "Add location" &gt; "Browse for files" and select
flowplayer-x.x.x.swf you just downloaded.
</p>


<h2>Documentation</h2>

<p>
<a href="http://flowplayer.org/documentation/installation/index.html">Flowplayer installation</a>
</p>

<p>
<a href="http://flowplayer.org/documentation/configuration/index.html">Flowplayer configuration</a>
</p>

<p>
See this identical page on <a href="http://flowplayer.org/demos/example/index.htm">Flowplayer website</a>
</p>

</div>


</body></html>

具 体的实例请参考我的链接

http://www.malacity.net/example/index.html




来源:http://richbellamy.com/wiki/Flash_Streaming_to_FlowPlayer_using_only_PHP#When_I_try_to_seek.2C_the_video_restarts

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

[hide]

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


  评论这张
 
阅读(654)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017