1

I have a PHP script which triggers an FFMPEG file conversion via shell_exec().

shell_exec('ffmpeg -i file.webm -c:v libx264 -c:a aac -strict -2 file.mp4 >/dev/null 2>/dev/null &');

This happens in the background (hence &), i.e. the script completes before conversion has finished.

Is there a way to call and execute a PHP script (to update a DB flag) once the conversion is complete?

I've done plenty of Googling but my knowledge of server commands just isn't up to understanding what I'm reading (e.g. this answer). The best I could manage was to redirect stdout to a file via

shell_exec('ffmpeg -i file.webm -c:v libx264 -c:a aac -strict -2 file.mp4 > MYFILE.txt 2>/dev/null &');

...but obviously that just creates and writes to a file, it doesn't call and execute it via PHP.

6
  • Make the background job a blocking process, otherwise there really is no way around the race condition. I suppose you could poll the file (with a cron job or something) you are outputting for it's modified time, once that stops changing you could trigger something to happen. Commented Nov 9, 2018 at 19:45
  • Do you mean stop it from being a background process? Commented Nov 9, 2018 at 19:47
  • Depends on what you classify a background process, if you remove the & it still runs as a shell command but the calling PHP script will halt until that is done. Commented Nov 9, 2018 at 19:48
  • I am sure you know that already, but another idea is to create a background PHP process, that you run with &, in which calls that shell command, blocks, and then does the other thing after. That way if you have a user waiting and need to not block the PHP script that calls the background PHP script can return, and the background PHP script runs the shell command and then the other "thing" when it's done. If that makes sense. Commented Nov 9, 2018 at 19:50
  • And there's no way to do this without having the PHP script wait for it to complete? That surprises me. I'd like to show a "We're working on it message" immediately then silently call the completion script when it's done. Commented Nov 9, 2018 at 19:50

1 Answer 1

1

I am not that great at server commands either, so I can't really help you there. But I do have this knack for figuring things out.

So I see a few ways you could do this, essentially you need PHP to do something when the command line call finishes. The obvious answer is to remove the & off the end of the command and make it blocking so PHP sticks around tell the job is done. But in doing so you can't return to the end user until that is done.

Option 1 So one way around this is to make a sort of Bootstrap PHP script that you call non-blocking. In this script do your now blocking conversion command and after that returns have PHP do something else.

 //bootstrap.php
 shell_exec('ffmpeg -i file.webm -c:v libx264 -c:a aac -strict -2 file.mp4 > MYFILE.txt 2>/dev/null'); //blocking
 //Update the DB

Then from your Controller or what have you call the bootstrap non-blocking

shell_exec('php {pathto}/bootstrap.php 2>/dev/null &');

This way the call to the bootstrap returns immediately but the conversion call is blocking, which gives you the chance to update the DB afterwords.

Option 2

Because the conversion is outputting a file, you could start a separate background job, that monitors the modified time of the output file. Then if the modified time is like a minute in the past you could assume it's done converting and update the DB. The modified time should continue to update as long as data is being added to the file.

Hope that helps.

PS. I have some code you may fine useful on GitHub

Runs Background processes in both windows & linux

https://github.com/ArtisticPhoenix/MISC/blob/master/BgProcess.php

PHP process locking ( Mutex simulation using files)

https://github.com/ArtisticPhoenix/MISC/blob/master/ProcLock.php

Command line argument mapping for PHP programs:

https://github.com/ArtisticPhoenix/Cli

Your welcome to use them if it helps you out.

Sign up to request clarification or add additional context in comments.

2 Comments

Some interesting ideas, thank you. I will be experimenting with them today.
Yea I developed these (I slimmed them down a bit) for a RabbitMq queuing system, which requires background workers. I run the real versions of these on a server that does about 180 million searches a day... enjoy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.