I've 3 inputs (1st, 2nd and 3rd block)
1st a mp4 video with 600x400 aspect ratio
2nd a png image with 600x400 aspect ratio
3rd a jpeg image with red background
Output (4th block)
I need a mp4 video of 600x400 as output, it should have resized video of 422x282 and merge all three as shown in image.
Can we implement this via ffmpeg command line?
I'm able to resize video and image separately but having issue in creating desire output.
Use
ffmpeg -i 1.mp4 -i red.jpg -i frame.png
-filter_complex "[0]scale=422:-1[vid];[1][vid]overlay=(W-w)/2:(H-h)/2[bg];
[bg][2]overlay=(W-w)/2:(H-h)/2" out.mp4
First, the video is resized. Then that resized video is overlaid on the red background. Then, on top of that result, the PNG frame is overlaid.
With no red frame and white BG,
ffmpeg -i 1.mp4 -i frame.png
-filter_complex "[0]scale=422:-1,pad=600:400:(ow-iw)/2:(oh-ih)/2:color=white[vid];[vid][1]overlay=(W-w)/2:(H-h)/2" out.mp4
Related
I get a darker background, almost like a placeholder for the asset that is to be blended, whereas the background should be transparent, and should show the same colour as the rest of the background.
I have two webm vp9 files that I am trying to blend using FFMPEG blending functionality.
One of the videos is a zoom animation that starts from 1 pixel in size and then increases in size to 50 x 50 pixels.
The other is a solid red background video 640 px x 360 px
At frame 1 the result looks like this:-
At about 0.5 seconds through, the result looks like this:-
At the end of the sequence, the zoom animation webm fills that darker square you see (50 x 50 pixels).
The code to do the blending of the two webm files looks like this:-
filter_complex.extend([
"[0:v][1:v]overlay=0:0:enable='between(t,0,2)'[out1];",
'[out1]split[out1_1][out1_2];',
'[out1_1]crop=50:50:231:251:exact=1,setsar=1[cropped1];',
'[cropped1][2:v]blend=overlay[blended1];',
"[out1_2][blended1]overlay=231:251:enable='between(t,0,2)'[out2]"
])
This overlays a red background onto a white background, making the red background the new background colour.
It then splits the red background into two, so that there is one output for cropping and another output for overlaying.
It then crops the location and size of the layer to be blended out of the red background. We do this because blending works only on an asset of the same size.
It then performs the blend of the zoom animation onto the cropped background.
It then overlays the blended over the red background
Unfortunately I'm unable to attach videos in stackoverflow, otherwise I would have included them.
The full command looks like this:-
ffmpeg -i v1_background.webm -itsoffset 0 -c:v libvpx-vp9 -i v2_red.webm -itsoffset 0 -c:v libvpx-vp9 -i v3_zoom.webm -filter_complex "[0:v][1:v]overlay=0:0[out1];[out1]split[out1_1][out1_2];[out1_1]crop=50:50:231:251:exact=1,setsar=1[cropped1];[cropped1][2:v]blend=overlay[blended1];[out1_2][blended1]overlay=231:251" output_video_with_blended_overlaid_asset.mp4
I have checked the input vp9 webm zoom video file by extracting the first frame of the video
ffmpeg -vcodec libvpx-vp9 -i zoom.webm first_frame.png
and inspecting the colours in all the channels in GIMP. The colours (apart from the opaque pixel in the middle) are all zero, including the alpha channel.
Note that I tried adding in all_mode, so that the blend command is blend=all_mode=overlay, however this still shows the darker placeholder under the animation asset. In other words, this command
ffmpeg -i v1_background.webm -itsoffset 0 -c:v libvpx-vp9 -i v2_red.webm -itsoffset 0 -c:v libvpx-vp9 -i v3_zoom.webm -filter_complex "[0:v][1:v]overlay=0:0[out1];[out1]split[out1_1][out1_2];[out1_1]crop=50:50:231:251:exact=1,setsar=1[cropped1];[cropped1][2:v]blend=all_mode=overlay[blended1];[out1_2][blended1]overlay=231:251" output_video_with_blended_all_mode_overlay_asset.mp4
also doesn't work
and trying to convert the formats to rgba first doesn't help either, command below is simplified a bit
ffmpeg -c:v libvpx-vp9 -i v2_red.webm -c:v libvpx-vp9 -i v3_zoom.webm -filter_complex "[0:v]format=pix_fmts=rgba[out1];[1:v]format=pix_fmts=rgba[out2];[out1]split[out1_1][out1_2];[out1_1]crop=50:50:0:0:exact=1,setsar=1[cropped1];[cropped1][out2]blend=all_mode=dodge[blended1];[out1_2][blended1]overlay=50:50" output_video_with_blended_all_mode_dodge_rgba_and_alpha_premultiplied_overlay.mp4
adding in an alpha premultiply didn't help either
ffmpeg -c:v libvpx-vp9 -i v2_red.webm -c:v libvpx-vp9 -i v3_zoom.webm -filter_complex "[0:v]format=pix_fmts=rgba[out1];[1:v]setsar=1,format=pix_fmts=rgba,geq=r='r(X,Y)*alpha(X,Y)/255':g='g(X,Y)*alpha(X,Y)/255':b='b(X,Y)*alpha(X,Y)/255'[out2];[out1]split[out1_1][out1_2];[out1_1]crop=50:50:0:0:exact=1,setsar=1[cropped1];[cropped1][out2]blend=all_mode=dodge[blended1];[out1_2][blended1]overlay=50:50" output_video_with_blended_all_mode_dodge_rgba_and_alpha_premultiplied_overlay.mp4
Wondering if there is a workaround I could use so that the background stays transparent?
I was looking for maybe a way of changing the input pixel format in the filter_complex stream to see if that works, but couldn't see anything about this.
I have this example video, recorded by Kazam:
https://user-images.githubusercontent.com/1997316/178513325-98513d4c-49d4-4a45-bcb2-196e8a76fa5f.mp4
It's a 1022x728 video.
I need to add a drop shadow identical to the one generated by the "Drop shadow (legacy)" filter of Gimp with the default settings. So, I generated with Gimp a PNG containing only the drop shadow. It's a 1052x758 image:
Now I want to put the video over the image to get a new video with the drop shadow. The wanted effect for the first frame is:
So, the video must be placed over the image. The top-left corner of the video must be in the position 11x11 of the background image.
How can I achieve this result?
I tried without success the following command. What's wrong?
ffmpeg -i shadow.png -i example.mp4 -filter_complex "[0:v][1:v] overlay=11:11'" -pix_fmt yuv420p output.mp4
About the transparency of the PNG background image, if it can't be maintained, then it's okay for the shadow to be on a white background. Otherwise, if it can be maintained by using an animated GIF as the output format, it is better.
The solution is to remove the transparency from shadow.png. Then:
ffmpeg -i example.mp4 -filter_complex "[0:v] palettegen" palette.png
ffmpeg -loop 1 -i shadow.png -i example.mp4 -i palette.png -filter_complex "[1:v] fps=1,scale=1022:-1[inner];[0:v][inner]overlay=11:11:shortest=1[new];[new][2:v] paletteuse[out]" -map '[out]' -y output.gif
The result is exactly what I wanted:
This solution is inspired by the answer https://stackoverflow.com/a/66318325 and by the article https://www.baeldung.com/linux/convert-videos-gifs-ffmpeg
I've got an MKV that I would like to replace the first 5 seconds with a static png image that fades in/out from black. How can I accomplish this with just ffmpeg?
Easy method is to overlay the image:
ffmpeg -i input.mkv -loop 1 -t 5 -i image.png -filter_complex "[1]fade=type=in:duration=1,fade=type=out:duration=1:start_time=4[fg];[0]drawbox=t=fill:enable='lte(t,5)'[bg];[bg][fg]overlay=eof_action=pass:x=(W-w)/2:y=(H-h)/2" -c:a copy output.mkv
I added the drawbox filter to make a black background because I didn't know the size of your image.
See FFmpeg Filter Documentation.
I have a .mov video with alpha channel and .png watermark. Is it possible to overlay watermark only over the non-transparent pixels of the video using ffmpeg?
Use
ffmpeg -i in.mp4 -i image \
-filter_complex "[0]split[o][a];[a]alphaextract[a];\
[o][1]overlay=format=auto[oc];[oc][a]alphamerge" -c:a copy out.mov
Choose a codec that preserves alpha like -c:v png.
I am trying to overlay some text on video using ffmpeg. I am able to overlay text by the bellow command.
ffmpeg -i input1.mp4 -filter_complex "[0:v]transpose=2[anticlockwiserotated];[anticlockwiserotated]drawtext=fontfile=../../public/fonts/Roboto-Regular-webfont.ttf: text='Test Text':x=100: y=50: fontsize=36: fontcolor=white:[textapplied];[textapplied]transpose=1" output_video.mp4
It is allowing me to overlay horizontally or vertically only.
But I want to append it with some angle like 45 degrees.
For that if I modify the command as
ffmpeg -i input1.mp4 -filter_complex "[0:v]rotate=45*PI/180[anticlockwiserotated];[anticlockwiserotated]drawtext=fontfile=../../public/fonts/Roboto-Regular-webfont.ttf: text='Test Text':x=100: y=50: fontsize=36: fontcolor=white:[textapplied];[textapplied]rotate=315*PI/180" output_video.mp4
By this I am getting overlay video as:
Because in this first I am rotating video to 45 degrees, appending text and bringing it back to original position. So I am loosing borders.
Please suggest me the best way to overlay text with required angle on video.
Thanks in advance.
Basic method is to generate text on a blank canvas, then an alpha layer for the text, rotating the result and overlaying that on the main video.
In the command below, a should be replaced by the angle. The co-ordinates for the drawtext are used in the overlay instead. Depending on the length of your text, some of it may get clipped if you've rotated it anticlockwise. So check and adjust the Y offset accordingly.
ffmpeg -i input1.mp4 -filter_complex
"color=black:100x100[c];
[c][0]scale2ref[ct][mv];
[ct]setsar=1,drawtext=fontfile=../../public/fonts/Roboto-Regular-webfont.ttf:
text='Test Text':fontsize=36:fontcolor=white,split[text][alpha];
[text][alpha]alphamerge,rotate=a:ow=rotw(a):oh=roth(a):c=black#0[txta];
[mv][txta]overlay=x='min(0,-H*sin(a))+100':y='min(0,W*sin(a))+50':shortest=1"
output_video.mp4
One method is by using ASS or SRT subtitles with the FFmpeg subtitles or ass filters.
ffmpeg -i input -filter_complex "subtitles=diagonal.ass" output
SRT subtitles are much simpler than ASS and don't support rotation, but you can manually add it with the filter:
ffmpeg -i input -filter_complex "subtitles=diagonal.srt:force_style='Angle=45'" output
You can create and style the subtitles with Aegisub or manually.