php+ffmpeg add logo to video having error - php

I create code for add logo to video using ffmpeg.
ffmpeg -i 1.mp4 -i a.jpg -filter_complex "overlay=main_w/2-overlay_w/2:main_h/2-overlay_h/2" outputvideo.mp4
When I use this code in windows dos there is no problem and logo is added, but it give me an error when I use this with PHP.
PHP:
<?php
echo "Starting ffmpeg...\n\n";
echo shell_exec("ffmpeg -i 1.mp4 -i a.jpg -filter_complex "overlay=main_w/2-overlay_w/2:main_h/2-overlay_h/2" outputvideo.mp4");
echo "Done.\n";
?>
Error:
Parse error: syntax error, unexpected 'overlay' (T_STRING) in C:\xampp\htdocs\tts\2.php on line 3
How to solve this?
Please help me.

Escape " like \".
echo shell_exec("ffmpeg -i 1.mp4 -i a.jpg -filter_complex \"overlay=main_w/2-overlay_w/2:main_h/2-overlay_h/2\" outputvideo.mp4");

As Glavic has pointed out, you need to escape the double quotes in the string, so your command should look like this:
echo shell_exec("ffmpeg -i 1.mp4 -i a.jpg -filter_complex \"overlay=main_w/2-overlay_w/2:main_h/2-overlay_h/2\" outputvideo.mp4");

Related

what is ffmpeg exit code 2? and also output error array is empty and not working in php

I use ffmpeg to convert bitrate to 128 but not working in php
exec("ffmpeg -i input.mp3 -codec:a libmp3lame -b:a 128k output().mp3 2>&1",
$output, $exit_code);
if ($exit_code!= 0) {
$data['message'][] = "Error";
}
print_r($output);
print_r($exit_code);
exit;
After running this code show error code 2.
The output is an empty array and also exit_code is 2 and not create output.mp3 file.
I already study How can I find out what this ffmpeg error code means? but this isn't my problem and don't explain error code 2 or error code 2 is not defined. My problem is dont show any error and error message is empty just exit_code show 2 that means is some error happened.
syntax error near unexpected token `('
You need to escape the parentheses because they are special characters:
exec("ffmpeg -i input.mp3 -codec:a libmp3lame -b:a 128k 'output().mp3' 2>&1",
or
exec("ffmpeg -i input.mp3 -codec:a libmp3lame -b:a 128k output\(\).mp3 2>&1",
Also see FFmpeg: Quoting & Escaping.
This isn't exactly a PHP issue, so please always make sure your ffmpeg command works before you attempt to script it. (That would really reduce the number of questions here.)

How do I add whitespaces when running ffmpeg from php shell_exec

I am trying to insert text overlay, and can do this, but I cannot add spaces to the text.
ffmpeg -i meme.mp4 -y -vf drawtext='/Users/me/Library/Fonts/Champagne & Limousines.ttf:text='testtext': fontcolor=white: fontsize=24' -codec:a copy outputtexttest.mp4 2>&1
The error when I make testtext test text is:
Unable to find a suitable output format for 'text: fontcolor=white: fontsize=24' text: fontcolor=white: fontsize=24: Invalid argument
Add double quotes around the text, not single quotes like you have:
echo shell_exec("$ffmpeg -i meme.mp4 -y -vf drawtext='/Users/me/Library/Fonts/Champagne & Limousines.ttf:text=''test + infinity spaces :) text'': fontcolor=white: fontsize=24' -codec:a copy outputtexttest.mp4 2>&1");

How merge two mp4 videos side by side with the help of FFMPEG and PHP code?

Could anybody help me with the php code for FFMPEG to concatenate two mp4 videos and store the concatenated files as an mp4 at any folder in the server?
i find command for ffmpeg.but i don't know how to make it work in php..
ps: in the orther hand, is there any program like ffmpeg i can use?
You can use the hstack filter for side-by-side:
ffmpeg -i left.mp4 -i right.mp4 -filter_complex hstack /path/to/output.mp4
Using in PHP may look like:
<?php
echo "Starting ffmpeg...\n\n";
echo shell_exec("ffmpeg -i left.mp4 -i right.mp4 -filter_complex hstack /path/to/output.mp4 >/dev/null 2> ffmpeg.log &");
echo "Done.\n";
?>
Adapted from FFmpeg Wiki: PHP.

Add image overlay for each millisecond the video using ffmpeg and php

I need to add an image and different position for each millisecond the video frame, using php and ffmpeg.
For example:
In the first second I add an image in the X position, the next second another image in the position X.
If you use the command directly on the terminal, the conversion is successful. But in PHP I have difficulties.
In php, use the following command:
<?php
shell_exec('C:\\ffmpeg\\bin\\ffmpeg.exe -i C:\\ffmpeg\\bin\\vid.mp4 -i C:\\ffmpeg\\bin\\1.png -filter_complex "[0:v][1:v] overlay=100:25:enable=\'between(t,1,1.5)\'" C:\\ffmpeg\\bin\\output1.mp4');
sleep(20);
echo "1";
shell_exec('C:\\ffmpeg\\bin\\ffmpeg.exe -i C:\\ffmpeg\\bin\\output1.mp4 -i C:\\ffmpeg\\bin\\2.png -filter_complex "[0:v][1:v] overlay=10:25:enable=\'between(t,1.5,2)\'" C:\\ffmpeg\\bin\\output2.mp4');
sleep(50);
echo "2";
shell_exec('C:\\ffmpeg\\bin\\ffmpeg.exe -i C:\\ffmpeg\\bin\\output2.mp4 -i C:\\ffmpeg\\bin\\3.png -filter_complex "[0:v][1:v] overlay=250:25:enable=\'between(t,2,2.5)\'" C:\\ffmpeg\\bin\\output1.mp4');
sleep(20);
echo "3";
shell_exec('C:\\ffmpeg\\bin\\ffmpeg.exe -i C:\\ffmpeg\\bin\\output1.mp4 -i C:\\ffmpeg\\bin\\4.png -filter_complex "[0:v][1:v] overlay=300:25:enable=\'between(t,3,3.5)\'" C:\\ffmpeg\\bin\\output2.mp4');
sleep(20);
echo "4";
shell_exec('C:\\ffmpeg\\bin\\ffmpeg.exe -i C:\\ffmpeg\\bin\\output2.mp4 -i C:\\ffmpeg\\bin\\5.png -filter_complex "[0:v][1:v] overlay=350:25:enable=\'between(t,4,4.5)\'" C:\\ffmpeg\\bin\\output1.mp4');
sleep(70000);
shell_exec('C:\\ffmpeg\\bin\\ffmpeg.exe -i C:\\ffmpeg\\bin\\output1.mp4 -i C:\\ffmpeg\\bin\\1.png -filter_complex "[0:v][1:v] overlay=400:25:enable=\'between(t,5,5.5)\'" C:\\ffmpeg\\bin\\output2.mp4');
I used the sleep function (PHP), but it did not work.
Please, as I have not much experience with ffmpeg and php, you can help me.
Thank you very much.
Troubleshoot by examining the command's output. That should give you the answer.
exec('C:\\ffmpeg\\bin\\ffmpeg.exe -i C:\\ffmpeg\\bin\\vid.mp4 -i C:\\ffmpeg\\bin\\1.png -filter_complex "[0:v][1:v] overlay=100:25:enable=\'between(t,1,1.5)\'" C:\\ffmpeg\\bin\\output1.mp4', $output);
var_dump($output);

To water mark videos at bottom right corner using ffmpeg

I found some answer here in stack which is indeed using ffmpeg but it is giving me some error.
I ran it in command window and the error is quite like
"Unable to find a suitable output format for 'ΓÇôi'
ΓÇôi: Invalid argument".
my command is as follows
ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=(main_w-overlay_w-10)/2:(main_h-overlay_h-10)/2 [out]" outputvideo.mp4
please suggest some ideas.
Basically overlay property defines where your watermark image will be posted -
main_w: video width
main_h: video height
overlay_w: overlay width
overlay_h: overlay height.
I guess this should work fine
$mark = "ffmpeg -i ".$inputvideo." -i logo.png -filter_complex ". '"overlay=x=(main_w-overlay_w):y=(main_h-overlay_h)/(main_h-overlay_h)"'." ".uniqid()."topright.mp4";
You can try these out. Should work out for you.
/*
* At top left watermark
*/
$mark = "ffmpeg -i ".$inputvideo." -i logo.png -filter_complex ". '"overlay=x=(main_w-overlay_w)/(main_w-overlay_w):y=(main_h-overlay_h)/(main_h-overlay_h)"'." ".uniqid()."topleft.mp4";
/*
* At top right watermark
*/
$mark = "ffmpeg -i ".$inputvideo." -i logo.png -filter_complex ". '"overlay=x=(main_w-overlay_w):y=(main_h-overlay_h)/(main_h-overlay_h)"'." ".uniqid()."topright.mp4";
I tried with this command, and it worked for me. hope it will work for you as well.
$mark = "ffmpeg -i inputvideo.mp4 -i watermark.png -filter_complex 'overlay=x=(main_w-overlay_w):y=(main_h-overlay_h)' outputvideo.mp4";
exec($mark);

Categories