How to pipe a new command after ffmpeg background process completes? - php

This is my ffmpeg process:
exec("/usr/local/bin/ffmpeg -y -i source.avi dest.mp4 >/dev/null 2>/dev/null &
Now, I wish to execute a PHP file after the conversion is complete. Logically, this is what I have:
exec("/usr/local/bin/ffmpeg -y -i source.avi dest.mp4 >/dev/null 2>/dev/null ; php proceed.php &
This doesn't work though, since then PHP will hold up the process to wait till the ffmpeg conversion is complete. What I want is basically to call proceed.php after the conversion completes, both of which are done in the background.
If anyone can provide the Windows server solution, that will be awesome too.

Write an external (bash/php) script that executes both the ffmpeg and php process, and tack & after that.
For windows, please open a new question on SO.

To add on to what Evert had posted, here is an example of what I use for my FFMPEG bash script... it's far from done (it doesn't alert if the program crashes, for instance) but it's somewhere to start:
#!/bin/sh
## Set our paths
FFMPEG_PATH=/usr/local/bin
SITE_PATH=path_to_file
VIDEO_PATH=$SITE_PATH/public_html/videos
## Make sure we have permissions to do this stuff
chown -R wwwrun:www $VIDEO_PATH/$2
chmod -R 765 $VIDEO_PATH/$2
## Set the options for mp4 compression
options="-vcodec libx264 -b 512k -ar 22050 -flags +loop+mv4 -cmp 256 \
-partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \
-me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \
-flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \
-g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10\
-qmax 51 -qdiff 4"
## Start the conversion.
$FFMPEG_PATH/ffmpeg -y -i $VIDEO_PATH/$2/original/$1 -an -pass 1 -threads 2 $options $VIDEO_PATH/$2/$2.mp4 2> $VIDEO_PATH/$2/pass_one.log
$FFMPEG_PATH/ffmpeg -y -i $VIDEO_PATH/$2/original/$1 -acodec libfaac -ab 96k -pass 2 -threads 2 $options $VIDEO_PATH/$2/$2.mp4 2> $VIDEO_PATH/$2/pass_two.log
## Create the thumbnail for the video
. $SITE_PATH/bin/create_thumbnail $2 00:00:15 2> $VIDEO_PATH/$2/generate_thumbnails.log
## Clean up the log files that were created
## find /log_path/ -name *log* -exec rm {} \;
## Update datbase and send email that we're done here.
php $SITE_PATH/public_html/admin/includes/video_status.php converting_finished $2
And this all gets called from a PHP file that does (along with some other code):
proc_close(proc_open(server_path.'/bin/convert_video_mp4 '.mysql_result($next_video, 0, "uid").'.'.mysql_result($next_video, 0, "original_ext").' '.mysql_result($next_video, 0, "uid").' &', array(), $foo));
PS - I know mysql extension are on their way out, I haven't been using or updating this code in a while, so please update to your specifications

Related

PHP ffmpeg from command line

When i run the ffmpeg command directly in the command line it is working. But when i used the php command is breaking and getting error (At least one output file must be specified)
Code directly in command line
ffmpeg -i "F:/Sites/raaga_deploy36/mp4/arul/ghajini_rahath-96.mp4" -y -c copy -bsf h264_mp4toannexb -flags global_header -map 0 -f segment -segment_time 10 -segment_list "F:/Sites/raaga_deploy36/mp4/m3u8/ghajini_rahath-96.m3u8" -segment_format mpegts "F:/Sites/raaga_deploy36/mp4/m3u8/ghajini_rahath-96-segment_%05d.ts"
Code from PHP
$mfile=escapeshellarg($dir.$file);
$cfile=escapeshellarg($dir1.$fname.".m3u8");
$cfile_sg=escapeshellarg($dir1.$fname."-segment_%05d.ts");
$cmd="ffmpeg -i ".$mfile."-y -c copy -bsf h264_mp4toannexb -flags global_header -map 0 -f segment -segment_time 10 -segment_list ".$cfile.".m3u8 -segment_format mpegts ".$cfile_sg;
$res=shell_exec( $cmd );
Output in the command line
ffmpeg -i "C:/sites/hls/mp4/arul/ghajini_orum-128.mp4
"-y -c copy -bsf h264_mp4toannexb -flags global_header -map 0 -f segment -segmen
t_time 10 -segment_list "C:/sites/hls/mp4/m3u8/ghajini_orum-128.m3u8".m3u8 -segm
ent_format mpegts "C:/sites/hls/mp4/m3u8/ghajini_orum-128-segment_ 05d.ts"
since the commands (line) are breaking here it is not working.
please see the link below
http://dv.raaga.com/new_ig/m3u_convert_err.jpg
Thanks
Thanigaivelan

FFmpeg and mencoder

Currently i am making 2 videos with this command:
ffmpeg -loop 1 -r 25 -t 5 -i /home/psafari/public_html/youtube_images/movie_" . $id . ".jpg -q:v 1 -an /home/psafari/public_html/youtube_videos/".$movie1."
ffmpeg -loop 1 -r 25 -t ".$t." -i /home/psafari/public_html/yy.jpg -q:v 1 -an /home/psafari/public_html/youtube_videos/".$movie2."
And it also works, but when i will make 2 videos into 1, it only shows 5 seconds of the first video (like the other doesn't comvert also).
mencoder -ovc copy -o /home/psafari/public_html/final_".time().".avi /home/psafari/public_html/youtube_videos/".$movie1." /home/psafari/public_html/youtube_videos/".$movie2
Am i doing something wrong?

ffmpeg-php and shell_exec

I'm using ffmpeg-php for uploading videos and converting videos from
"ogv", "mp4", "avi", "flv", "ogg",
"mpg", "wmv", "asf" and "mov"
formats to
"flv" , "ogg" and "flv" formats.
I'm doing this by:
$command1='ffmpeg -i '.$Source." -ar 44100 -ab 128k -s wvga -aspect 4:3 -f FLV -y ".$dest;
$command2="ffmpeg -i ".$Source." -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre slow -threads 10 -s wvga -aspect 4:3 -f mp4 -y ".$dest;
$command3="ffmpeg -i ".$Source." -acodec libvorbis -ab 128k -ac 2 -vcodec libtheora -threads 10 -s wvga -aspect 4:3 -f ogg -y ".$dest;
#shell_exec($command1.";".$command2.";".$command3);
When applying those commands on .ogv videos it works fine but when applying on .MOV it does not work and no errors displays although when I paste the command into the terminal it works fine.
Hint: Script is running by cron job.
is there any suggestions for this problem?
Most likely the script is running out of time before the conversion takes place.
Increase the execution time of your scripts.
Although its not a recommend solution for production. You might want to run background processes to do the conversion and use the PHP script just to upload the video.
What happens when you run the problem part outside of a crontab?
are you saving the output of your crontab entry to files? i.e.
59 12 1 1 * { myPhpScript ; } > /tmp/myPhpScript.log 2>&1` ...
( The semicolon preceding the closing '}' is critical)
This captures any stdout or stderr messages to a logfile.
I hope this helps.
P.S. as you appear to be a newish user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.

FFMpeg working in command line but not in PHP using exec();

I am using FFMpeg to covert videos and it is working fine from the command line. I am using the following command:
ffmpeg -i input.mpg -vcodec libx264 -b 819200 -s 100x100 -g 15 -bf 3 -b_strategy 1 -coder 1 -qmin 10 -qmax 51 -sc_threshold 40 -flags +loop -cmp +chroma -me_range 16 -me_method hex -subq 5 -i_qfactor 0.71 -qcomp 0.6 -qdiff 4 -directpred 1 -flags2 +fastpskip -dts_delta_threshold 1 -acodec libfaac -ab 48000 output.m4v
However, when I run the command using PHP exec(), the output video is not encoded correctly and is distorted and cropped. I am using the following in PHP:
$output = exec($cmd . ' 2>&1', $output, $return);
The $output returns a '0' successful code.
Does any one have any suggestions?
Thank you.
This is unusual. It is possible that you have more than one ffmpeg binary installed, and the one that is being called by the PHP/Apache user is not the same as the one you call as your user from the command line.
Try specifying the full path to your ffmpeg binary (/usr/bin/ffmpeg or whatever) inside your exec().
It sounds like some command line options are getting lost/altered. I would try to split this into a 2 part process:
write a shell script on-the-fly (from PHP) that has all the proper command arguments (make it executable)
execute the shell script (from PHP)
I would probably try:
1) change ' 2>&1' to ' 2>&1 &'
Also, transcoding can take a while. Are you certain you are waiting long enough for the transcode to complete?

ffmpeg run from command line executes, from mod_fcgi truncates after completion

The ffmpeg from command line generates preview files and two separate two-pass conversion that when run from a shell script, execute successfully.
Running the commands via php's exec(/usr/bin/ffmpeg) or through exec(name_of_shell_script) generates preview files successfully. THe strange behavior is that the movies will generate, then truncate. ffmpeg log files are generated successfully, the out put file I can watch grow in size as the conversion continues, and then, when complete it appears, the files are truncated....
The only things that have changed on the system are changing from mod_php to mod_fcgi and php_cgi, but the error logs show nothing unusual except for
mod_fcgid: stderr: wmv, files3/1qwj, 1qwj.wmv
supressing output of the shell
scriptname.sh > /dev/null 2>&1
does not change the outcome.
should shell_exec be used? Is it a unix permissioning?
This is in ubuntu 10.04.1
This solution doesn't apply
FFMPEG running in Command Line but not PHP
EDIT:
looks like it might have something to do with the two pass encoding. The two pass encoding works fine from command line, but from the PHP env the shell overwrites something on the second pass.
nice -n 11 /usr/bin/ffmpeg -y -i $1 -r 30000/1001 -b 1M -bt 2M -vcodec libx264 -threads 0 -pass 1 -vpre /usr/share/ffmpeg/libx264-fastfirstpass.ffpreset -an movie.flv
nice -n 11 /usr/bin/ffmpeg -y -i $1 -r 30000/1001 -b 1M -bt 2M -vcodec libx264 -threads 0 -pass 2 -vpre /usr/share/ffmpeg/libx264-hq.ffpreset -acodec libfaac -ac 2 -ar 48000 -ab 192k movie.flv
$1 is the input filename
found
https://roundup.ffmpeg.org/issue1829
Edit:
when done here are the log file artifacts
-rw-r--r-- 1 www-data www-data 0 2010-09-19 19:02 ffmpeg2pass-0.log
-rw-r--r-- 1 www-data www-data 0 2010-09-19 19:02 movie.flv
-rw-r--r-- 1 www-data www-data 153466 2010-09-19 19:02 movie.jpg
-rw-r--r-- 1 www-data www-data 358803 2010-09-19 19:02 movie_preview.jpg
-rw-r--r-- 1 www-data www-data 410283 2010-09-19 19:02 x264_2pass.log
-rw-r--r-- 1 www-data www-data 5759257 2010-09-19 19:02 x264_2pass.log.mbtree
opened new ticket at request of maintainer
https://roundup.ffmpeg.org/issue2238
Edit:
looks like the issue is the audio for wmv files
http://ubuntuforums.org/showthread.php?t=1074152
Issue went away by updating ffmpeg and compiling.
wmv pro audio files are now supported in ffmpeg, and the installation I was using did not support.

Categories