Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run ffmpeg from PHP web script

Tags:

php

ffmpeg

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, ·_-

like image 903
it6 Avatar asked Sep 10 '12 16:09

it6


1 Answers

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.

like image 89
Green Black Avatar answered Sep 28 '22 17:09

Green Black