PHP, Apache - как вызвать php-скрипт, если был запрошен файл .mp4

У меня есть служба, которая загружает файлы в определенном формате с сайта 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


person DDS    schedule 12.11.2020    source источник


Ответы (1)


Я решил сам, я просто использовал мод-перезапись Apache, я включил его в 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;
person DDS    schedule 13.11.2020