how to add watermark in multiple areas using ffmpeg? - php

i am using laravel framework and also using ffmpeg php library. Actually i have done almost 70% of work. But the problem i faced is to show watermark at multiple areas on video. I have done the watermark on top-left corner that is running very fine on that video. But i want to add watermark in top-left, bottom-left, bottom-right. I have used this code for top-left watermark (for video):-
$inputVideo = public_path('input/airplane_flight_airport_panorama_1080.mp4');
$outputVideo = public_path('uploads/output.mp4');
$watermark = public_path('input/watermark.jpg');
$wmarkvideo = "ffmpeg -i ".$inputVideo." -i ".$watermark." -filter_complex ". '"overlay=x=(main_w-overlay_w):y=(main_h-overlay_h)/(main_h-overlay_h)"'." ".$outputVideo;
exec($wmarkvideo );
Please help me how can i add watermark on top-left, bottom-left, bottom-right in these areas. Thanks in advance :)

This is the ffmpeg command you would use for multiple watermarks
ffmpeg -i inputVideo -i watermark-tr -i watermark-tl -i watermark-br -i watermark-bl
-filter_complex "[0][1]overlay=x=W-w:y=0[tr];
[tr][2]overlay=x=0:y=0[tl];
[tl][3]overlay=x=W-w:y=H-h[br];
[br][4]overlay=x=0:y=H-h" outputfile
tr = top-right; tl = top-left; br = bottom-right; bl = bottomleft
With center as well,
ffmpeg -i inputVideo -i watermark-tr -i watermark-tl -i watermark-br -i watermark-bl -i watermark-c
-filter_complex "[0][1]overlay=x=W-w:y=0[tr];
[tr][2]overlay=x=0:y=0[tl];
[tl][3]overlay=x=W-w:y=H-h[br];
[br][4]overlay=x=0:y=H-h[bl];
[bl][5]overlay=x=(W-w)/2:y=(H-h)/2" outputfile

Related

How can i add a watermark on videos? [duplicate]

I am currently using these commands:
Top left corner
ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:10 [out]" outputvideo.flv
Top right corner
ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out]" outputvideo.flv
Bottom left corner
ffmpeg –i inputvideo.avi -vf "movie=watermarklogo.png [watermark]; [in][watermark] overlay=10:main_h-overlay_h-10 [out]" outputvideo.flv
Bottom right corner
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.flv
How to place watermark center of the video ?
Examples to add a watermark / logo image on video using the overlay filter.
Centered
ffmpeg -i input.mp4 -i logo.png -filter_complex "overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" -codec:a copy output.mp4
or with the shortened overlay options:
overlay=(W-w)/2:(H-h)/2
Top left
This is the easy one because the default, if you provide no options to overlay, is to place the image in the top left.
This example adds 5 pixels of padding so the image is not touching the edges:
overlay=5:5
Top right
With 5 pixels of padding:
overlay=main_w-overlay_w-5:5
or with the shortened options:
overlay=W-w-5:5
Bottom right
With 5 pixels of padding:
overlay=main_w-overlay_w-5:main_h-overlay_h-5
or with the shortened options:
overlay=W-w-5:H-h-5
Bottom left
With 5 pixels of padding:
overlay=5:main_h-overlay_h
or with the shortened options:
overlay=5:H-h-5
Transparency / opacity / alpha
Example to make watermark 50% transparent using the format and colorchannelmixer filters:
ffmpeg -i input.mp4 -i watermark.jpg -filter_complex "[1]format=rgba,colorchannelmixer=aa=0.5[logo];[0][logo]overlay=(W-w)/2:(H-h)/2:format=auto,format=yuv420p" -c:a copy output.mp4
Improved quality
Using the format=auto option in the overlay filter can make PNG watermarks look better:
ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=5:H-h-5:format=auto,format=yuv420p" -c:a copy output.mp4
Note the addition of the format filter (yes, same name as the option, but a standalone filter) to reset it to YUV 4:2:0 which is needed for MP4 output. Remove ,format=yuv420p if you are not outputting MP4.
Scale watermark in relation to main video
Use the scale2ref filter:
Example to make logo 10% (1/10) the size of the main video:
ffmpeg -i input.mp4 -i watermark.png -filter_complex "[1][0]scale2ref=w=oh*mdar:h=ih*0.1[logo][video];[video][logo]overlay=5:H-h-5" -c:a copy output.mp4

FFMPEG - Palettegen for animations is not working

I have a script taken from the correct answer at https://superuser.com/questions/1002562/convert-multiple-images-to-a-gif-with-cross-dissolve, but I am getting no results.
Even copying the code word for word does not work for me.
However, if I run $ffmpeg = shell_exec("ffmpeg -i images/image001.jpg -vf palettegen palette_test.png"); on a single image, it works fine. But because I need it for an animation, I need to create a pallete for all images.
Unless of course, on each image in the GIF sequence I can load a new palette?
$q = "ffmpeg -f image2 -framerate 0.5 -i ../view/client/files/".$dir."/%*.jpg -i palettePro.png -lavfi paletteuse -y .." . $save_path . '/' . $filename . " -report";
Palettegen can analyze and generate a matching frame sequence with per-frame palette.
ffmpeg -i images/image%03d.jpg -vf palettegen=stats_mode=single palette_%03d.png
And then
ffmpeg -framerate 0.5 -i images/image%03d.jpg -framerate 0.5 -i palette_%03d.png -lavfi paletteuse=new=1 -y $filename

FFMPEG gif resizing not working while I am adding watermark

I am trying to resize video to gif image. But everytime i am getting failure.
Please check my cdde:
$input = "files/video.mp4";
$thumbnail = 'img/logo.png'
$output = 'gif/'.time().'.gif';
$command = "ffmpeg -t 3 -ss 00:00:02 -i $input -i $thumbnail -filter_complex overlay=W-w-5:H-h-5 -codec:a copy $output"
#exec($command, $ret);
print_r($ret);
Above command I am not getting array result. But working fine. My video successfully converted to GIF with watermark. But if I am trying to resize below code. This command not working. Please tell me where is error
$command = "ffmpeg -t 3 -ss 00:00:02 -i $input -vf scale=400:-1 -i $thumbnail -filter_complex overlay=W-w-5:H-h-5 -codec:a copy $output"
Please help me. Let me know if any other way to make video to gif with resize and watermark image.
EDIT: If I am removing watermark command. Then my resize GIF working fine.
The scaling should occur within the complex as well.
ffmpeg -t 3 -ss 00:00:02 -i $input -i $thumbnail -filter_complex [0]scale=400:-1[b];[b][1]overlay=W-w-5:H-h-5 -codec:a copy $output
Else the standalone vf output is mapped for output by ffmpeg.
FFmpeg offers palettegen and paletteuse filters for optimized GIF generation.
ffmpeg -t 3 -ss 00:00:02 -i $input -i $thumbnail -filter_complex [0]scale=400:-1[b];[b][1]overlay=W-w-5:H-h-5,split[v][p];[p]palettegen,fifo[pal];[v][pal]paletteuse -codec:a copy $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);

Create thumbnail image from video in server in php

In my website I have a option to upload video file by the user. In that I want to create a thumbnail image of that video. I have tried in local system with some coding it is working fine. I tried same coding in to service it is not working. I checked for ffmpeg enabled in server, it was disabled. Is their any other option to creating thumbnail in server with out enabling the ffmpeg? Please help me to find the solution.
you can use ffmpeg ( Form you tag i guess you knew )
What you needed to pass to ffmpeg is
-i = input file
-deinterlace = deinterlace pictures
-an = disable audio recording
-ss = start time in the video (seconds)
-t = duration of the recording (seconds)
-r = set frame rate
-y = overwrite existing file
-s = resolution size
-f = force format
Example
// where ffmpeg is located
$ffmpeg = '/usr/bin/ffmpeg';
//video dir
$video = 'path/to/video';
//where to save the image
$image = 'path/to/image.jpg';
//time to take screenshot at
$interval = 5;
//screenshot size
$size = '640x480';
//ffmpeg command
$cmd = "$ffmpeg -i $video -deinterlace -an -ss $interval -f mjpeg -t 1 -r 1 -y -s $size $image 2>&1";
exec($cmd);
Or try
$second = 15;
$cmd = "$ffmpeg -itsoffset -$second -i $video -vcodec mjpeg -vframes 1 -an -f rawvideo -s $size $image";
exec($cmd);
Think you should also look at detail dissuasion on possible issues
ffmpeg-php to create thumbnail of video
If you don't want to use ffmpeg you could use mencoder as an alternative too. But in the end you will need to install ffmpeg/mencoder and then use that to render the thumbnail as PHP has no built in functionality to handle video.
If you're on a shared webhost you might want to look into services like zencoder that can generate you converted streams of video including thumbnails:
https://app.zencoder.com/docs/api/encoding/thumbnails

Categories