I'm trying to convert a video from .mp4
to .flv
in php using:
shell_exec("ffmpeg -i input.mp4 output.flv 1>output.txt 2>&1");
Also I am taking output of entire process in output.txt
file to use it in creation of progress bar in javascript
(that's not the problem here).
The problem is:
if video(input.mp4) size is>50Mb then it takes too long to complete shell_exec
(>30secs). which hangs the UI unnecessarily.
So what i want to know is:
Is there any way to make shell_exec
work together with rest of the script?
I need only to initiate ffmpeg.exe
via php
script and then php script can terminate.
Once the process is initiated javascript
will start its magic(at client side).
So I need to execute shell_exec
async. with rest of the script. and once the process is initiated the script should inform client by simply echo
ing. It should not wait for ffmpeg.exe
to complete its working. Is it possible?
I've seen several similar questions on SO, many of them describing the same problem
ex:
Run a ffmpeg process in the background
Run PHP Task Asynchronously
ffmpeg Progress Bar - Encoding Percentage in PHP
Can ffmpeg show a progress bar?
etc.
BUT
They all were using linux
OS's or Using curl
. I'm using windows 8
(wamp without curl and any other add-on except ffmpeg.exe
).
So Is it possible? can experts here please help me one more time?
Thanks in advance.
An important note should be mentioned here. The ffmpeg command-line tool uses stderr for output of error log messages and stdout is reserved for possible use of pipes (to redirect the output media stream generated from ffmpeg to some other command line tool). That being said, if you run your ffmpeg in the background, you'll most probably want to redirect the stderr to a log file, to be able to check it later.
One more thing to take care about is the standard INPUT (stdin). Command-line ffmpeg tool is designed as an interactive utility that accepts user's input (usually from keyboard) and reports the error log on the user's current screen/terminal. When we run ffmpeg in the background, we want to tell ffmpeg that no input should be accepted (nor waited for) from the stdin. We can tell this to ffmpeg, using I/O redirection again "
<?php
echo "Starting ffmpeg...\n\n";
echo shell_exec("ffmpeg -y -i input.avi output.avi </dev/null >/dev/null 2>/var
/log/ffmpeg.log &");
echo "Done.\n";
?>
The "-y" option is used to auto-overwrite the output file (output.avi) without asking for yes/no confirmation. If you need the opposite scenario, to auto-cancel the entire process if the output file already exists, then use "-n" option instead.
Some PHP libraries allow wrapping ffmpeg calls into PHP objects, and give you a nice syntax to work with if you don't like to use the command line. One of these is the actively maintained PHP-FFMpeg. It only requires you to download a recent ffmpeg and ffprobe build apart from installing the PHP components. Then you can run PHP code like this:
$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');
$video->filters()->resize(new FFMpeg\Coordinate\Dimension(320, 240))->synchronize();
$video->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4')
Note: ffmpeg-php is an extension that is not developed since 2007 (and requires "ffmpeg-0.4.9_pre1 or higher"), which means that you are restricted to use a very old version of ffmpeg, without possibility to update it to the latest version. Since a lot of changes/improvements are being made, inside ffmpeg's code, every day, it makes ffmpeg-php incompatible with the latest ffmpeg.
Yes, you can use the following command in windows to run FFmpeg in the background without reloading the whole page.
pclose(popen("start /B C:/xampp/htdocs/video/bin/ffmpeg -i input.mp4 output.flv , r"));
this is location of ffmpeg in my pc ,you can use your ffmpeg location.