1

Scenario

I upload a file, after that is done I use PHP-FFMPEG to convert the file to an mp4, this is the code I use for that:

$video = $ffmpeg->open($targetPath . '1.' . $extension);
$format = new \FFMpeg\Format\Video\X264();
$format->setVideoCodec('libx264');
$format->setAudioCodec('aac');
$format->setAdditionalParameters(['-preset', 'fast', '-crf', '24']);
$format->on('progress', function ($video, $format, $percentage){
    $_SESSION['progress'] = $percentage;
});
$video->save($format, $targetPath . '.mp4');

It works fine, however when I open another tab and go to my website it just loads until the video has been converted even though that is being done on another tab.

When I open a different browser the website loads just fine.

Note: This also happens without the following code:

$format->on('progress', function ($video, $format, $percentage){
    $_SESSION['progress'] = $percentage;
});

But when I transfer the session to the other browser the website also stops functioning.

Question

What is causing this and how can I fix this?

Tom
  • 606
  • 7
  • 28
  • [Read this](https://github.com/PHP-FFMpeg/PHP-FFMpeg/issues/606) - It seems relevant to your issue. They recommend that you implement an async job that convert the video in the background (CRON job). Also might be related: [How to continue script execution in background in PHP?](https://maslosoft.com/kb/how-to-continue-script-execution-in-background-in-php/) – Alon Eitan Jan 31 '21 at 15:50
  • But then how can I set a session to monitor the progress? Maybe a database entry, but that seems overkill. – Tom Jan 31 '21 at 15:55
  • It might be an overkill but you don't want the user to wait for a long time, that's a bad user experience – Alon Eitan Jan 31 '21 at 15:57
  • So you suggest make a file that has a 5 second cronjob which updates the progress to the database? – Tom Jan 31 '21 at 16:10
  • You can schedule the task to run every X minutes, another options is that when a user is uploading a file you can also execute the video conversion script as described [here](https://stackoverflow.com/questions/6273791/calling-exec-on-a-php-file-and-passing-parameters/6273843) (Just to make sure that the task starts immediately) – Alon Eitan Jan 31 '21 at 16:26
  • Is there any solution for this to dynamically work on both Windows and Linux systems? – Tom Jan 31 '21 at 16:49

0 Answers0