首页 > 解决方案 > PHP,Apache - 如果已请求 .mp4 文件,如何调用 php 脚本

问题描述

我有一项服务可以从网站下载特定格式的文件,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 文件

标签: phpwindowsapache2.4

解决方案


我自己解决了,我只是使用了 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;

推荐阅读