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

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.

Related

TWO linux commands executed in background called by PHP exec() function

I try to execute two linux commands in background by single php exec() call.
My first command makes some files backup:
cp -r ../source ../destination
Second command creates a file "DONE.txt" when backup is complete:
touch ../destination/DONE.txt
Moreover I base on php manual and use following background exec() call:
exec('bash -c "exec nohup setsid '.$twoCommands.' > /dev/null 2>&1 &"');
The whole code is bellow:
exec('bash -c "exec nohup setsid { cp -r ../source ../destination && touch ../destination/DONE.txt; } > /dev/null 2>&1 &"');
And... it doesn't work:) But why?
If I use only one command:
exec('bash -c "exec nohup setsid cp -r ../source ../destination > /dev/null 2>&1 &"');
It works well:)
I have no idea why you're trying to make this so complicated. nohup and setsid are two completely separate functions, not arguments you pass to exec. Nor do you even need to call exec in the command itself; that's the point of using exec in PHP. Output redirection is only necessary if you need the PHP script to continue in the background after the exec call rather than waiting for it to finish.
[root#test~]# ll
total 8
drwxr-xr-x. 2 root root 24 Jun 27 13:40 source
[root#test~]# ll source
total 0
-rw-r--r--. 1 root root 0 Jun 27 13:40 1
-rw-r--r--. 1 root root 0 Jun 27 13:40 2
[root#test~]# php -a
Interactive shell
php > exec("cp -r ./source ./destination && touch ./destination/DONE.txt");
php > exit
[root#test~]# ll destination/
total 0
-rw-r--r--. 1 root root 0 Jun 27 13:44 1
-rw-r--r--. 1 root root 0 Jun 27 13:44 2
-rw-r--r--. 1 root root 0 Jun 27 13:44 DONE.txt
[root#test~]#

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

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

Recording audio with FFMPEG works in terminal but not through exec() of PHP

I use this command to RECORD audio and video from my webcam in terminal and it works!
ffmpeg -f video4linux2 -r 25 -sameq -s 640x480 -i /dev/video0 -f alsa -i plughw:0,0 -ar 22050 -ab 128k -y webcam.flv
But when I do it through PHP, like this:
echo shell_exec('ffmpeg -f video4linux2 -r 25 -sameq -s 640x480 -i /dev/video0 -f alsa -i plughw:0,0 -ar 22050 -ab 128k -y webcam.flv 2>&1 &');
I receive that log:
ffmpeg version 0.8.6-4:0.8.6-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav developers built on Apr 2 2013 17:02:36 with gcc 4.6.3 * THIS PROGRAM IS DEPRECATED * This program is only provided for compatibility and will be removed in a future release. Please use avconv instead. [video4linux2 # 0x23579a0] Estimating duration from bitrate, this may be inaccurate Input #0, video4linux2, from '/dev/video0': Duration: N/A, start: 4868.729067, bitrate: 122880 kb/s Stream #0.0: Video: rawvideo, yuyv422, 640x480, 122880 kb/s, 25 tbr, 1000k tbn, 25 tbc Home directory /var/www not ours.
ALSA lib pcm_hw.c:1401:(_snd_pcm_hw_open) Invalid value for card [alsa # 0x23589e0] cannot open audio device plughw:0,0 (No such file or directory) plughw:0,0: Input/output error
Before I got that Mic device problem I was having problem with the Webcam device's permissions, then i did:
sudo chmod -R 777 /dev/video0
And the video capture was ready to be used! But now I got this Mic device problem!
I think it can be about permissions too, but I dont know linux very well and have no idea how to fix that!
Thanks!
I just found the solution! =x
sudo chmod -R 666 /dev/snd/*

run ffmpeg from PHP web script

I need to manage the recording/capture of a website mindwhile it is running a slide-show to get videos form these slides.
My approach is:
<?php
define('FFMPEG_LIBRARY', '/usr/bin/ffmpeg ');
$ffmpegcmd = "ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output.mpg";
shell_exec($ffmpegcmd);
?>
But i get this error from php error log:
[x11grab # 0x81e8aa0] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 800 height: 600
No protocol specified
No protocol specified
[x11grab # 0x81e8aa0] Could not open X display.
:0.0: Input/output error
Similar command from console run good.
Please, any help to get display and be able to control ffmpeg from browser php script?
Thanks in advance.
thanks for your time.
I got rid the X display error, but not I still haven't got the capture.
Using xvfb I get an unknown file at /tmp written by www-data user:
-rw-r--r-- 1 www-data www-data 11252 Sep 12 09:49 server-B20D7FC79C7F597315E3E501AEF10E0D866E8E92.xkm
Running startx I got also an unknown file at /tmp
-rw------- 1 www-data www-data 59 Sep 12 09:53 serverauth.oLcFlG7tXC
any of both grow in size so it is not capturing anything. The content is some binary thing.
What are those files about?
What I am trying is to write a script in which I can control the time ffmpeg is capturing the desktop to create a video from a jquery slide displayed on a website.
My try from console is closer, but if I can do it by browser I will be able to know when to stop sending an AJAX request once the slide is finished.
This is my try from console:
#!/bin/bash
# start the slide website: I will need to change it to control by querystring the language and course level
firefox http://www.languagecourse.net/vocabulary-trainer.php &
# start recording: I will need to adjust the frame to capture
ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output2.mpg &
# since I can't control the time a course takes I pause an arbitrary time
sleep 5
# look for the capture PID and close it
for i in $(ps aux | grep ffmpeg | sed "s/ */#/g" | cut -f2 -d#)
do
echo "proceso $i killed"
kill -9 $i
done
I wonder once the website is opened I can continue doing the control from AJAX, but not know if I will be able to get the ffmpeg PID to stop the command.
I will appreciate any kind of comments.
Regards,
ยท_-
You can use Xvfb to emulate a x-environment
<?php
$ffmpegcmd = "xvfb-run -a ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output.mpg";
shell_exec($ffmpegcmd);
or something like this
<?php
$ffmpegcmd = "startx -- `which Xvfb` :1 -screen 0 800x600x24 && DISPLAY=:1 && ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/output.mpg");
shell_exec($ffmpegcmd);
That should be good to get rid of the "Could not open X display." error, and will probably solve your problem.

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?

Categories