php - 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 文件
解决方案
我自己解决了,我只是使用了 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;
推荐阅读
- mongodb - 为什么 MongoDB 不使用散列连接或排序合并?
- excel - 是否可以让 excel 突出显示不符合我所需格式的单元格(例如日期为 6.2.2021 与所需的 6/2/2021)?
- android - LibGDX:在屏幕之间切换时保留纹理(在内存中)
- scala - 如何在 Spark Executors 中检查广播变量的状态
- pine-script - 可以在使用路径工具创建的线条的端点上叠加点
- swift - SWIFTUI 滑块更改拇指图像或拇指大小?
- azure - 带有应用服务连接错误的 Azure Key Vault
- javascript - 内容安全策略阻止内联执行
- keyboard - 如何修复此 gnuplot 错误以处理按键?
- php - PHP 解析错误:语法错误、意外的“CallClient”(T_STRING)、期望函数(T_FUNCTION)或 const(T_CONST)