How to run ffmpeg with php in debian 10 - php

i am trying to run a ffmpeg command with my php in Debian 10, but its not working, but the command is working perfectly in windows. The command is to add a watermark and a text at the bottom of the video.
Here is the php code
<?php
$new_file="new.mp4";
$text="eloke";
$video_name="video.mp4";
echo file_exists($new_file)? "<video autoplay src='$new_file'></video>":"No file";
$cmd="ffmpeg -i $video_name -i watermark.jpg -filter_complex overlay=W-w-5:H-h-15[video];[video]drawtext=\"text=$text:fontcolor=white:fontsize=12:x=(w-text_w)-5: y=(h-text_h)-5\" $new_file";
exec("$cmd 2>&1", $output);
var_dump($output);
?>
I am getting this in the browser
array(1) { [0]=> string(103) "sh: 1: [video]drawtext=text=eloke:fontcolor=white:fontsize=12:x=(w-text_w)-5: y=(h-text_h)-5: not found" }
Please what am i doing wrong

Try using the following snippet and replace into your original code:
$cmd = "ffmpeg -i ".$video_name." -i watermark.jpg -filter_complex overlay=W-w-5:H-h-15[video];[video]drawtext=\"text=".$text.":fontcolor=white:fontsize=12:x=(w-text_w)-5: y=(h-text_h)-5\" ".$new_file;
$cmd = escapeshellcmd($cmd);
exec($cmd." > /path/to/stdout_file 2>&1", $output);
Also, beware of using escapeshellcmd()

Related

Why exec() is not working in foreach loop?

See, for testing purpose if I am trying to run PHP exec() statically with only one video file, then its compressing video perfectly(please see first line line).
Later when I am compressing dynamically in the loop, then exec function is not working. Please tell me why it's not working in the loop?
// echo shell_exec("/usr/local/bin/ffmpeg -i /home4/machine/public_html/riyaz/check_video/video_1494453415.mp4 -strict -2 -crf 20 /home4/machine/public_html/riyaz/zcompress/video_1494453415.mp4");
#include_once("start.php");
$directory_path = FILEPATH.'/';
$video_mp4 = glob("/home4/machine/public_html/riyaz/check_video/*.mp4");
/*video size will show in mb*/
foreach ($video_mp4 as $video_mp4_list){
$video_size = filesize($video_mp4_list);
$video_size_in_mb = round(($video_size/1048576), 2);
$get_file_name = explode('/',$video_mp4_list);
$get_file_name_for_destination = $get_file_name[6];
$getSourceFileNamePath = '/home4/machine/public_html/riyaz/check_video/'.$get_file_name_for_destination;
$getDestFileNamePath = '/home4/machine/public_html/riyaz/zcompress/'.$get_file_name_for_destination;
if ($video_size_in_mb >= 1000 ){
echo exec("/usr/local/bin/ffmpeg -i ". $getSourceFileNamePath ." -strict -2 -crf 20 ".$getDestFileNamePath);
}
}
shell_exec() returns the full output: PHP shell_exec() however
exec() returns only the last line from the outptut that might be empty.. So you have to provide second $output and third parameter $return_var to get more useful data: PHP exec()
if ($video_size_in_mb >= 1000 ){
$command = "/usr/local/bin/ffmpeg -y -i ". $getSourceFileNamePath ." -strict -2 -crf 20 ".$getDestFileNamePath . ' 2>&1';
echo PHP_EOL ."Executing command: ". $command;
if(file_exists($getDestFileNamePath)) echo "File already exists!";
$output = null;
$return_var = null;
exec($command, $output, $return_var);
print_r($command);
print_r($return_var);
print_r($output);
echo PHP_EOL;
} else {
echo "video too small to proceed';
}
php shell_exec() vs exec()
EDIT: The problem is that the destination file exists and ffmpeg exits without any action. One solution is to use attribute -y to overwrite the output file 5.4 Main options -y (global) Overwrite output files without asking.

Loop bash script until cURL response doesn't equal string

I'm trying to loop a bash script (cURL below) until my remote PHP script doesn't equal failure. I've tried the following, and it works when $fileName exists.. but doesn't loop when it returns failure.
My PHP:
<?php
$videoFile = $_GET["name"] . ".mp4";
if (file_exists($videoFile)) {
echo "success";
} else {
echo "failure";
}
?>
Bash command:
until $(curl --output /dev/null --silent --head --fail "my-remote-php-script.php"); do
printf '.'
sleep 1
done
This should work:
while [ "$(curl -s 'http://your-url.php')" == "failure" ]; do
echo -n '.'
sleep 1
done
The $() places the curl in a sub-shell. The resulting string, comming from your php script, is compared with failure.
Try this:
url="http://server.tld/foo.php"
until curl -sf "$url"; do echo -n "."; sleep 1; done

Execute Python with PHP

I try to execute this PHP command :
$result = exec("sudo python /home/pi/test.py",$output, $ret);
var_dump($result);
echo "<br>";
var_dump($output);
echo "<br>";
var_dump($ret);
This command works perfectly on a Linux Terminal but with the PHP it doesn't work.
Here the result on the PHP page :
string(0)
array(0) { }
int(9)
I verified the process with ps -ef, nothing appears.
It might be help someone with the same problem.
I managed later to execute a Python script with this PHP code :
$command = escapeshellcmd('sudo /home/pi/test.py');
$output = shell_exec($command);
echo $output;
For the moment, I never succeed to execute this in background. I try those solutions :
$command = escapeshellcmd("sudo /home/pi/test.py >/dev/null &");
$command = escapeshellcmd("sudo /home/pi/test.py &");
My PHP page wait also the end of the process, but i don't want to.

Using ImageMagick to resize images from command line in PHP

I'm using ImageMagick to generate a small thumbnail of images. convert --version shows ImageMagick 4.2.9 99/09/01.
$output = array();
$cmd = "/opt/RZmagick4/bin/convert data/test.jpg data/small.png"; // works fine
$cmd = "/opt/RZmagick4/bin/convert data/test.jpg -resize 300x200 data/small.jpg"; // does not work
exec($cmd, $output);
echo "<pre>";
print_r($output);
echo "</pre>";
Why does the second $cmd not work? Output for both is Array( ). However, there is just no output image.
You should write this:
$cmd = "/opt/RZmagick4/bin/convert -resize 300x200 data/test.jpg data/small.jpg";

Escaping quotes in a PHP script using ssh2_exec

I need some help with escaping the quotes in this script, I have code above and below, but it does not pertain to this because if I just issue a screen -d -m -S minecraft, it creates one fine. Any help would be amazing because i've been trying for an hour now, Thanks!
if (!($stream = ssh2_exec($con, 'screen -d -m -S minecraft -p 0 -X stuff "\printf "\say This is a test.\r"\"\' ))) {
echo "fail: unable to execute command\n";
} else {
// collect returning data from command
stream_set_blocking($stream, true);
$data = "";
while ($buf = fread($stream,4096)) {
$data .= $buf;
}
echo $data;
fclose($stream);
}
Shouldn't the \ should go before the ". Also, from your description, it sounds like that you only want to run "screen -d -m -S minecraft" and it looks like you have an extra printf in there.
if (!($stream = ssh2_exec($con, "screen -d -m -S minecraft")))
http://php.net/manual/en/language.types.string.php
You only need to change "\ to \".

Categories