FFMPEG script to merge multiple videos and a background image - ffmpeg

I have 30 clips which are different in aspect ratio(like some videos are 10801920(they are vertical) and some are 1280720(horizontal aspect ratio videos). I want to merge all of them but also have a static background image that is of 1920x1080 aspect ratio. The video would be such that all the clips are concatenated but they have a background image(just like those tiktok compilation videos on youtube). Can someone please help me with this program?

Example using 3 videos. It can easily be expanded to 30 videos. I broke the command into multiple lines so you can see the syntax better. Make it one line before executing.
ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -i image.png -filter_complex
"[0:v]scale=1280:720:force_original_aspect_ratio=increase,crop=1280:720,setsar=1,fps=25,format=yuv420p[v0];
[1:v]scale=1280:720:force_original_aspect_ratio=increase,crop=1280:720,setsar=1,fps=25,format=yuv420p[v1];
[2:v]scale=1280:720:force_original_aspect_ratio=increase,crop=1280:720,setsar=1,fps=25,format=yuv420p[v2];
[0:a]aformat=sample_rates=44100:channel_layouts=stereo[a0];
[1:a]aformat=sample_rates=44100:channel_layouts=stereo[a1];
[2:a]aformat=sample_rates=44100:channel_layouts=stereo[a2];
[v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[vid][a];
[3][vid]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2[v]"
-map "[v]" -map "[a]" -c:v libx264 -c:a aac -movflags +faststart output.mp4
References:
How to concatenate videos in ffmpeg with different attributes?
How to center overlay in ffmpeg?
Resizing videos with ffmpeg to fit specific size

Related

How can one use ffmpeg to position a video exactly at a specific pixel position in jpeg?

For example,
there is a jpeg with dimensions 1900x1000 and it has a frame (a rectangle) with pixel coordinates 490x100 and 1400x500
a video with resolution 720p needs to be placed within this rectangle
Could you pls share the ffmpeg command to concatenate the jpeg and video? I tried -hstack and -xstack filters. But, couldn't get the video inside the rectangle.
TIA
Your rectangle has a size of 910x400, so you have to downscale the 1280x720 video to fit. But the aspect ratios do not match. So you need to also pad or crop the 1280x720 video to properly fit inside 910x400 if you want to preserve the aspect ratio.
Combine these answers:
Resizing videos with ffmpeg to fit into static size
How to overlay with ffmpeg?
Crop to fit
ffmpeg -i image.jpg -i video.mp4 -filter_complex "[1]scale=910:400:force_original_aspect_ratio=increase,crop=910:400[fg];[0][fg]overlay=x=490:y=100,format=yuv420p" -c:a copy -movflags +faststart output.mp4
Pad to fit
ffmpeg -i image.jpg -i video.mp4 -filter_complex "[1]scale=910:400:force_original_aspect_ratio=decrease,pad=910:400:-1:-1[fg];[0][fg]overlay=x=490:y=100,format=yuv420p" -c:a copy -movflags +faststart output.mp4
Squish/stretch to fit
This will ignore the aspect ratio and will result in a distorted image.
ffmpeg -i image.jpg -i video.mp4 -filter_complex "[1]scale=910:400,setsar=1[fg];[0][fg]overlay=x=490:y=100,format=yuv420p" -c:a copy -movflags +faststart output.mp4

ffmpeg: Is there a way to create video from images and overlay on image at same time?

I am trying to create a video from still images using ffmpeg. The command I use to do this is
ffmpeg -y -r 3 -i input_images%03d.png -c:v libx264 -vf fps=24 -pix_fmt yuv420p output.mp4
However, I would like to overlay this video on still image, without creating a video of the still image first. So, for example, if I have the following images
[still, frame1, frame2, frame3]
I'd like a command to create a video of frame1, frame2, and frame3 overlayed on still.
all with one command. Is there a way to do this?
I've looked at several answers to related problems (e.g., Add image overlay on video FFmpeg) but they don't answer my question, exactly.
Use
ffmpeg -framerate 24 -i still.png -framerate 3 -i input_images%03d.png -c:v libx264 -filter_complex "overlay=x='(W-w)/2':y='(H-h)/2'" -pix_fmt yuv420p -y output.mp4

Create Seamless Video and Crop Video in one command

I have a ffmpeg command that convert a video for seamless loop. But now i want to crop Video as well in same command.
is there any solution to do crop video 720x720 in same command
--Seamless Commmand--
ffmpeg -i video.mp4 -filter_complex [0:v]split[body][pre];[pre]trim=duration=1,format=yuva420p,fade=d=1:alpha=1,setpts=PTS+(28/TB)[jt];[body]trim=1,setpts=PTS-STARTPTS[main];[main][jt]overlay -c:v libx264 -strict experimental out.mp4
Change [0:v]split[body][pre] to [0:v]crop=720:720,split[body][pre]

How to combine a video and an image using "reverse" vstack?

I have an image and a video (same width). I now want to use ffmpeg to add the image above the video. Google and other SO threads the use of the vstack filter_complex tag, which works great - except that it puts the image under the video.
I've tried putting the image first and then the video, but this doesnt work. I've also tried giving the vstack command reverse inputs, but also didnt work!
The video may also contain audio which I would need to keep.
See code below:
// Works, but puts image below video (instead of above)
ffmpeg -i test.mp4 -i text.png -filter_complex vstack result.mp4
// Doesn't work at all
ffmpeg -i test.mp4 -i text.png -filter_complex '[1:v][0:v]vstack' result.mp4
// Doesn't work at all
ffmpeg -i test.mp4 -i text.png -filter_complex '[1:v][0:v]vstack=inputs=2[v]' -map '[v]' -map 0:a result.mp4
Google / SO did not yield any tips on how to achieve this so far. Do you know a solution?
Use
ffmpeg -i test.mp4 -i text.png -filter_complex '[1:v][0:v]vstack' -c:a copy -pix_fmt yuv420p result.mp4
Videos and images can have different pixel formats. When the various inputs to a stack filter don't have the same format, the filter picks the format of the first input and converts all other inputs to that format. However, some video players don't support a wide variety of formats. yuv420p is the widely supported format and so the command above forces the output to that one. Audio, if present in the MP4, will get carried over.
ffmpeg -i test.mp4 -i text.png -filter_complex '[1:v]format=yuv444p[img];[img][0:v]vstack' -c:a copy -pix_fmt yuv420p result.mp4

Make video from images with ffmpeg/imagemagic

I want to convert jpg images to mp4 video without resizing image(keep original images size and well formated video)
I have tried lot's of solutions of ffmpeg and imagemagic (links given below)but both crop images after converting in video format and i want a video from images with original image size.
Solution will be appreciated with ffmpeg or imagemagick. :)
slow ffmpeg's images per second when creating video from images
image to video ffmpegf
FFMPEG An Intermediate Guide/image sequence
How can I create a video file from a set of jpg images? [duplicate]
How to create a video from images with FFmpeg?
FFmpeg
Make video from still image sequence
Combining images with ImageMagick
Imagemagick.org
ffmpeg -framerate 1/5 -i na%03d.jpg -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p output.mp4
on large image(1600X1200) its execute successfully but not generate a smooth video.
on small image(300x168) its show error. i also try this command on small image
ffmpeg -framerate 1/5 -i abc%03d.jpg -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p output.mp4 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2"
this work for me i use this in loop
ffmpeg -loop 1 -i na002.jpg -c:a copy -c:v libx264 -strict 1 -shortest -vf "scale='min(1280,iw)':min'(720,ih)':force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2" test.mp4
ffmpeg -y -i video.mp4 -vf "scale='min(1280,iw)':min'(720,ih)':force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2" out1.media
ffmpeg -i "C:\xampp\htdocs\social_media\public\uploads\stories\temp_media\81/temp0.mp4" -i "C:\xampp\htdocs\social_media\public\uploads\stories\temp_media\81/temp1.mp4" -i "C:\xampp\htdocs\social_media\public\uploads\stories\temp_media\81/temp2.mp4" -i "C:\xampp\htdocs\social_media\public\uploads\stories\temp_media\81/temp3.mp4" -filter_complex "[0]setdar=16/9[a];[1]setdar=16/9[b];[2]setdar=16/9[c];[3]setdar=16/9[d];[a][0:a][b][1:a][c][2:a][d][3:a] concat=n=4:v=1:a=1[v][a]" -map "[outv]" -map "[outa]"

Resources