I run the following command through shell_exec

C:\wamp\bin\ffmpeg\bin>start /b ffmpeg -i input2.mp4 -codec:v libx264 -profile:v high -b:v 600k -maxrate 600k -bufsize 1000k -vf scale=854:480 -threads 0 -codec:a libvo_aacenc -b:a 128k output2.mp4 <NUL >NUL 2>NUL 

The script is still waiting ... The shell_exec () constraint seems to have taken into account that you need to output stdou and stderr somewhere so that you can leave the command in the "background" and not wait for the end of its execution. At the same time, launching it directly via the command line, like the conversion goes to the "background", that is, there is no output, and the command is all done exactly. Does anyone have any ideas about this?

UPD

 $cmd = 'START /DC:\wamp\bin\ffmpeg\bin\ /B CMD /C ffmpeg -y -i ' . $content['upload_data']['full_path'] . ' -codec:v libx264 -profile:v high -b:v 600k -maxrate 600k -bufsize 1000k -vf scale=854:480 -threads 0 -codec:a libvo_aacenc -b:a 128k ' . $content['upload_data']['file_path'] . $content['upload_data']['raw_name'] . '.480' . $content['upload_data']['file_ext'] . ' <NUL >NUL 2>NUL'; exec($cmd); 

Here is the command in php. Changing shell_exec to exec did not help either.

  • @Johny thanks. popen helped. The only thing I would like to ask is why popen works, and exec, etc. No, because exec also allows you to execute commands in the background. Also please add an answer, I will accept it. Probably according to the "given by command" for this reason? - Jeremen1
  • one
    Yes, according to this. It’s just that these functions have different purposes: exec() and similar ones are needed to execute the command, get an answer and pass on to the code, while proc_open() and its like represent a kind of terminal, perhaps)) They run the program a separate thread can continue to "interact" with this program. Why exec does not work as expected - I will not answer. I myself once ran into this, did not overcome it, found another solution - and that’s all :-) - Johny

2 answers 2

It would be great to see a piece of php code in the form in which you call this command.

Such a redirection of threads, like you, as far as I remember, works only in system() and exec() .

If this does not bring the proper result, try using popen()

    If you do not need to know the result of the external program, then you can send each exec to a separate thread (thread) http://php.net/manual/ru/function.pcntl-fork.php and at the same time you can send as many commands as you want ( all of them will be executed asynchronously), the ceiling here is only the maximum possible number of processes in the system, if this is a cloud then you can not think about it. If you need the results of the executed commands, you can cache them using the REDIS type. In general for asynchronous operations for php there is a good thing http://daemon.io/ .

    Yes, and 1 moment straight from the site site:

    Note: If you are going to use this function in a program that acts as a daemon, make sure that the standard output of the function is sent to a file or another stream, otherwise PHP will hang until the end of the program.