John Davidson

windows - PHP, Apache - how to call php script if a .mp4 file has been requested

0 comments
Message:


I have a service that downloads files in a specfic format from a site, remote.com/file.wav and must translate them on the fly exposing them like local.it/file.ogg.


Doing so when someone goes to local.com/file.mp4 the site connects to remote.com/file.raw downloads the file, convert it calling a windows program myconv.exe -i file.raw file.mp4 and serves file.ogg.


Now I have the script for the download:


$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;

What I'm missing is how to tell the webserver (apache or IIS) "if a ogg file is requested invoke this script" instead of looking on the filesystem for an .mp4 file



My Answer:
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789').'.raw';// download the file from remote.com$file_content = file_get_contents('http://remote.com/'.$file_to_download);file_put_contents($name, $file_content);// convert the file using myconv.exe$command = 'myconv.exe -i '.$name.' '.$file_to_serve;exec($command);// serve the converted fileheader('Content-Type: video/mp4');readfile($file_to_serve);// delete the temporary fileunlink($name);?>

This script will download the requested .raw file from remote.com, convert it to .mp4 using myconv.exe, serve the converted file as .mp4, and then delete the temporary .raw file. You can place this script in a PHP file on your Apache server and configure your server to route requests for .mp4 files to this script.

Rate this post

5 of 5 based on 7904 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us