【问题标题】:PHP, Apache - how to call php script if a .mp4 file has been requestedPHP,Apache - 如果已请求 .mp4 文件,如何调用 php 脚本
【发布时间】:2021-02-24 17:35:51
【问题描述】:

我有一项服务可以从网站remote.com/file.wav 下载特定格式的文件,并且必须像local.it/file.ogg 一样即时翻译它们。

这样做时,当有人访问local.com/file.mp4 时,站点会连接到remote.com/file.raw 下载文件,调用Windows 程序myconv.exe -i file.raw file.mp4 将其转换并提供file.ogg。

现在我有了下载脚本:

$file_to_download= str_replace('/','',$_SERVER['SCRIPT_NAME']);
$file_to_serve = str_replace('.raw','.mp4', $file_to_download); //actually this is the name the 


// make a temporary filename that is unique
$name = 'c:\\tmp\\tmpfile'.str_shuffle("abcdefghilmnopqrstuvzABCDEFGHILMNOPQRSTUVZ1234567890");
//download the data
$data = file_get_contents('http://remote.com/'.$file_to_download,false);
//store the downloaded data on the disk
file_put_contents($name,$data);
//call the CLI utility
exec('start /B c:\\programs\\ffmpeg\\ffmpeg.exe -i'.$name.' '.$file_to_serve);
unlink($name)
//read the output file
$data =  file_get_contents($file_to_serve);
$size = sizeof($data);

header("Content-Transfer-Encoding: binary");
header("Content-Type: ogg/vorbis);
header("Content-Length: $size"); 
unlink($file_to_serve);

echo $data;

我缺少的是如何告诉网络服务器(apache 或 IIS)“如果请求 ogg 文件,则调用此脚本”,而不是在文件系统中查找 .mp4 文件

【问题讨论】:

    标签: php windows apache2.4


    【解决方案1】:

    我自己解决了,我只是用了 Apache mod-rewrite, 我在 httpd.conf 上启用了它

    <IfModule rewrite_module>
        RewriteEngine on
        RewriteRule ^/audio/(.+\.ogg)$ /audio_file.php?&file=$1
    </IfModule>
    

    然后是脚本:

    $file_to_download= $_REQUEST['file'];
    $file_to_serve = str_replace('.raw','.mp4', $file_to_download);
    
    
    // make a temporary filename that is unique
    $name = 'c:\\tmp\\tmpfile'.str_shuffle("abcdefghilmnopqrstuvzABCDEFGHILMNOPQRSTUVZ1234567890");
    //download the data
    $data = file_get_contents('http://remote.com/'.$file_to_download,false);
    //store the downloaded data on the disk
    file_put_contents($name,$data);
    //call the CLI utility
    exec('start /B c:\\programs\\ffmpeg\\ffmpeg.exe -i'.$name.' '.$file_to_serve);
    unlink($name)
    //read the output file
    $data =  file_get_contents($file_to_serve);
    $size = sizeof($data);
    
    header("Content-Transfer-Encoding: binary");
    header("Content-Type: ogg/vorbis);
    header("Content-Length: $size"); 
    unlink($file_to_serve);
    
    echo $data;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 2012-12-01
      • 2015-10-06
      • 2012-02-02
      • 1970-01-01
      • 2012-04-13
      相关资源
      最近更新 更多