Run script after FFMPEG background task completes (via PHP)









up vote
0
down vote

favorite












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.










share|improve this question





















  • 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.
    – ArtisticPhoenix
    Nov 9 at 19:45











  • Do you mean stop it from being a background process?
    – Utkanos
    Nov 9 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.
    – ArtisticPhoenix
    Nov 9 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.
    – ArtisticPhoenix
    Nov 9 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.
    – Utkanos
    Nov 9 at 19:50














up vote
0
down vote

favorite












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.










share|improve this question





















  • 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.
    – ArtisticPhoenix
    Nov 9 at 19:45











  • Do you mean stop it from being a background process?
    – Utkanos
    Nov 9 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.
    – ArtisticPhoenix
    Nov 9 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.
    – ArtisticPhoenix
    Nov 9 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.
    – Utkanos
    Nov 9 at 19:50












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question













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.







php video ffmpeg background






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 19:04









Utkanos

21.2k43161




21.2k43161











  • 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.
    – ArtisticPhoenix
    Nov 9 at 19:45











  • Do you mean stop it from being a background process?
    – Utkanos
    Nov 9 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.
    – ArtisticPhoenix
    Nov 9 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.
    – ArtisticPhoenix
    Nov 9 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.
    – Utkanos
    Nov 9 at 19:50
















  • 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.
    – ArtisticPhoenix
    Nov 9 at 19:45











  • Do you mean stop it from being a background process?
    – Utkanos
    Nov 9 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.
    – ArtisticPhoenix
    Nov 9 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.
    – ArtisticPhoenix
    Nov 9 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.
    – Utkanos
    Nov 9 at 19:50















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.
– ArtisticPhoenix
Nov 9 at 19:45





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.
– ArtisticPhoenix
Nov 9 at 19:45













Do you mean stop it from being a background process?
– Utkanos
Nov 9 at 19:47




Do you mean stop it from being a background process?
– Utkanos
Nov 9 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.
– ArtisticPhoenix
Nov 9 at 19:48




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.
– ArtisticPhoenix
Nov 9 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.
– ArtisticPhoenix
Nov 9 at 19:50





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.
– ArtisticPhoenix
Nov 9 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.
– Utkanos
Nov 9 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.
– Utkanos
Nov 9 at 19:50












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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.






share|improve this answer




















  • Some interesting ideas, thank you. I will be experimenting with them today.
    – Utkanos
    Nov 12 at 11:14










  • 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
    – ArtisticPhoenix
    Nov 12 at 18:53










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231861%2frun-script-after-ffmpeg-background-task-completes-via-php%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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.






share|improve this answer




















  • Some interesting ideas, thank you. I will be experimenting with them today.
    – Utkanos
    Nov 12 at 11:14










  • 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
    – ArtisticPhoenix
    Nov 12 at 18:53














up vote
1
down vote



accepted










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.






share|improve this answer




















  • Some interesting ideas, thank you. I will be experimenting with them today.
    – Utkanos
    Nov 12 at 11:14










  • 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
    – ArtisticPhoenix
    Nov 12 at 18:53












up vote
1
down vote



accepted







up vote
1
down vote



accepted






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 20:00









ArtisticPhoenix

14.7k11223




14.7k11223











  • Some interesting ideas, thank you. I will be experimenting with them today.
    – Utkanos
    Nov 12 at 11:14










  • 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
    – ArtisticPhoenix
    Nov 12 at 18:53
















  • Some interesting ideas, thank you. I will be experimenting with them today.
    – Utkanos
    Nov 12 at 11:14










  • 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
    – ArtisticPhoenix
    Nov 12 at 18:53















Some interesting ideas, thank you. I will be experimenting with them today.
– Utkanos
Nov 12 at 11:14




Some interesting ideas, thank you. I will be experimenting with them today.
– Utkanos
Nov 12 at 11:14












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
– ArtisticPhoenix
Nov 12 at 18:53




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
– ArtisticPhoenix
Nov 12 at 18:53

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231861%2frun-script-after-ffmpeg-background-task-completes-via-php%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus