douxuanjie2692 2013-03-02 22:03
浏览 133
已采纳

使用PHP作为'file ='参数将视频文件从远程URL传递到jwplayer

I'm trying to directly play a video that is located on remote server (www.nowvideo.eu in this example), get video direct URL using curl() and preg_match(), and then using the download script found here directly present that .php file as the video file itself.

Downloading video works, but when I try to add a param 'file=video_file.php' to jwplayer I get this error: "Task Queue failed at step 5: Playlist could not be loaded due to crossdomain policy restrictions." I have a crossdomain.xml file, it is set to allow access from all domains, but I don't know why is it even important since video_file.php is on the same domain as play.php (which contains jw player code).

Here is the code for video_file.php:

<?php
$url = 'http://www.nowvideo.eu/video/qdu0vx7m3m3xd';
$content = curl($url);
preg_match('%video/(.[^/]*+)%', $url, $videoID);
preg_match('/filekey="(.[^\"]*?)"/',$content,$key);
$key = str_replace('"','',$key[0]);
$video_data = 'http://www.nowvideo.eu/api/player.api.php?user=undefined&pass=undefined&file='.$videoID[1].'&key='.$key;
$content_key = curl($video_data);
preg_match('/url=(.[^&]*+)/',$content_key,$file);
$file = str_replace('url=','',$file[0]);
download($file,2000);

/*
Set Headers
Get total size of file
Then loop through the total size incrementing a chunck size
*/
function download($file,$chunks){
    set_time_limit(0);
    header('Content-Description: File Transfer');
    header('Content-Type: video/x-flv');
    header('Content-disposition: inline; filename='.basename($file));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    $size = get_size($file);
    header('Content-Length: '.$size);

    $i = 0;
    while($i<=$size){
        //Output the chunk
        get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks));
        $i = ($i+$chunks);
    }

}

function curl($url){  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    $result = curl_exec($ch);  
    curl_close($ch);  
return $result;
}

//Callback function for CURLOPT_WRITEFUNCTION, This is what prints the chunk
function chunk($ch, $str) {
    print($str);
    return strlen($str);
}

//Function to get a range of bytes from the remote file
function get_chunk($file,$start,$end){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $file);
    curl_setopt($ch, CURLOPT_RANGE, $start.'-'.$end);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'chunk');
    $result = curl_exec($ch);
    curl_close($ch);
}

//Get total size of file
function get_size($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    return intval($size);
}
?>

Here is the code for play.php:

<html>
<head>
</head>
<body>
<object id="flashplayer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="830" height="490">
<param name="movie" value="http://example.com/player.swf">
<param name="allowFullScreen" value="true">
<param name="allowScriptAccess" value="always">
<param name="FlashVars" value="file=http://example.com/video_file.php&autostart=true&smoothing=true&stretching=exactfit">
<embed name="flashplayer" src="http://example.com/player.swf" flashvars="file=http://example.com/video_file.php&autostart=true&smoothing=true&stretching=exactfit" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="830" height="490">
</object>
</body>
</html>

Code for crossdomain.xml (located on root):

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" secure="false"/>
<allow-http-request-headers-from domain="*" headers="*" secure="false"/>
</cross-domain-policy>

Any ideas?

  • 写回答

1条回答 默认 最新

  • douzhao9608 2013-03-02 22:55
    关注

    Change:

    &stretching=exactfit

    To:

    &stretching=exactfit&provider=video

    Now, this will work, in the JW Player (I am assuming you are using JW5 here).

    -Ethan

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口