How to start a download when file is processing by a software

95 Views Asked by At


I encode a file with a special watermark for be downlaoded by the user
I would like to be able to start the downlaod when ffmpeg encode the file.

I have a php page which one create the water mark , launch ffmpeg and start the download with X-sendifle
The download don't start, adding sleep(15) let's the download start just I receive only what is done

Actually I use Apache2 as the webserver with X-sendfile mod

1

There are 1 best solutions below

0
On

Okay I finally find how to do
Using X-sendfile it's a wrong idea
Using mp4 it's a wrong idea because at the end of the encodage the software rewrite 4 bytes at the beginning of the file

So this is the code I use for be able to send a file like webm or flv files when ffmpeg encode the output

shell_exec('ffmpeg command');
sleep(2); //wait few second for the software start
$foutput ="where/is/your/output.file";
header('Content-Disposition: attachment; file.name.for.the.user"');
header("Keep-Alive:timeout=200, max=500");
//sending by chunk
set_time_limit(3600); 
$file =fopen($foutput , 'r');
$files = fstat($file);
$filesize= $files['size'];
$last = 0;
echo fread($file , $filesize);
sleep(5); //wait a little for the software add new bytes
$last = $filesize;
$files = fstat($file);
$filesize= $files['size'];
$done =0;
while( $done < 2 ){
    if ($filesize != $last){
        fseek($file , $last);
        $read = $filesize - $last ;
        echo fread($file , $read);
        $last =$filesize;
        sleep(5);
        $files = fstat($file);
        $filesize= $files['size'];
    }else{
        fclose($file);
        return 0;
    };
};

I'm a noob for coding and got a problem with my "while" just the code work really good without any issue
Do not use filesize , this command don't like when a file change of size all time and return only 4096 in that case
The download will be slow , this is normal , you receive chunk by chunk what the software done like with ffmpeg the transfer is between 200 to 1200 kb/s