Very similar to this topic:
FFmpeg: How to convert vertical video with black sides, to video 16:9, with blurred background sides
I'm trying to make myself a bat file that will automate my Instagram video creations.
The idea is that I make videos of variable resolutions and variable aspect ratios. I don't want to spend time on each video just to make it fit.
So here's my goal with that bat file:
- Must output a square video
- The original video must keep its original aspect ratio no matter what it is
- The blank spaces filling the voids from the original video and the square aspect ratio must be filled with a squared & blurred version of the original video
- Optionally, let me choose the output resolution (for encoding time saving and because AFAIK Instagram displays only 600px² videos(?))
So far I managed to get results that work only for either vertical or horizontal videos but not both. Or end up with the original video cropped, which I don't want: I frame my subjects as I want and I don't want to frame them forethinking the additional cropping a FFmpeg encoding could do.
This is my script so far:
echo off
:again
ffmpeg.exe -i "%~1" ^
-c:v libx264 -crf 23 -filter_complex "[0:v]scale=600*2:600*2,boxblur=luma_radius=min(h\,w)/20:luma_power=1:chroma_radius=min(cw\,ch)/20:chroma_power=1[bg];[0:v]scale=600:-1[ov];[bg][ov]overlay=(W-w)/2:(H-h)/2,crop=w=600:h=600" -profile:v baseline -level 3.0 -pix_fmt yuv420p -preset faster -tune fastdecode ^
-c:a aac -ac 2 -b:a 128k ^
-movflags faststart ^
"%~p1%~n1_Instagramized.mp4" -y
if NOT ["%errorlevel%"]==["0"] pause
shift
if "%~1" == "" goto:eof
goto:again
EDIT:
Thanks to @Gyan I got the solution. I added my input as well:
Since I might reuse that bat file often and might want to change the output resolution, It's not handy to have the resolution hardcoded in six different places.
So I create a variable called SquareSize
which is called using %SquareSize%
(instead of the hardcoded resolution) and set at the begining of the file using set SquareSize=XXX
. So now I can change easily when I need just by opening it and editing the XXX.
echo off
:again
set SquareSize=600
ffmpeg.exe -i "%~1" ^
-c:v libx264 -crf 23 ^
-filter_complex "[0:v]split=2[blur][vid];[blur]scale=%SquareSize%:%SquareSize%:force_original_aspect_ratio=increase,crop=%SquareSize%:%SquareSize%,boxblur=luma_radius=min(h\,w)/20:luma_power=1:chroma_radius=min(cw\,ch)/20:chroma_power=1[bg];[vid]scale=%SquareSize%:%SquareSize%:force_original_aspect_ratio=decrease[ov];[bg][ov]overlay=(W-w)/2:(H-h)/2" ^
-profile:v baseline -level 3.0 -pix_fmt yuv420p -preset faster -tune fastdecode ^
-c:a aac -ac 2 -b:a 128k ^
-movflags faststart ^
"%~p1%~n1_Instagramized.mp4" -y
if NOT ["%errorlevel%"]==["0"] pause
shift
if "%~1" == "" goto:eof
goto:again