0

Example video:

enter image description here

Desired results:

enter image description here

Zilton
  • 47
  • 7

1 Answers1

1

The full command may be as followed:

ffmpeg -y -i in.jpg -filter_complex "scale=1920:1080,setsar=1:1,crop=1584:896:172:92,split[crp0][crp1];[crp0]scale=1920:1080,setsar=1:1,gblur=sigma=30[blur];[blur][crp1]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" out.jpg

The command applies the image you have posted (named in.jpg).


Sample output:
enter image description here


Chain of filters:

  • scale=1920:1080,setsar=1:1 - Resize the input to 1920x1080 (setsar is used for fixing the aspect ratio).
  • crop=1584:896:172:92 - Crops the part inside the black frame.
  • split[crp0][crp1] - Split the cropped output to two identical streams (two identical images).
  • [crp0]scale=1920:1080,setsar=1:1,gblur=sigma=30[blur] - Resize the cropped image to 1920x1080 and blur the resized image.
    Store the blurred image in the temporary variable [blur].
  • [blur][crp1]overlay= ... - Overlay [crp1] over the blurred image.

It works the same way for a video file.
Example:

ffmpeg -y -i in.mp4 -filter_complex "scale=1920:1080,setsar=1:1,crop=1584:896:172:92,split[crp0][crp1];[crp0]scale=1920:1080,setsar=1:1,gblur=sigma=30[blur];[blur][crp1]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" -vcodec libx264 -pix_fmt yuv420p -acodec copy out.mp4

Finding the crop parameters using cropdetect filter:

Description of cropdetect filter:

Auto-detect the crop size.
It calculates the necessary cropping parameters and prints the recommended parameters via the logging system.
The detected dimensions correspond to the non-black area of the input video.

Using cropdetect result with Linux is described in this post.

I wanted to use it in Windows 10, and found this example,
but it's not working...


The following code is working (with ffmpeg version 4.4.1-full_build-www.gyan.dev):

ffmpeg -hide_banner -i in.jpg -vf scale=1920:1080,setsar=1:1,cropdetect=skip=0 -t 1 -f null - 2>&1 | findstr /R /C:"crop=" > log.txt
for /F "tokens=14* delims= " %%i in (log.txt) do set crop=%%i
echo %crop%

Using %crop% with FFmpeg command:

ffmpeg -y -i in.jpg -filter_complex "scale=1920:1080,setsar=1:1,%crop%,split[crp0][crp1];[crp0]scale=1920:1080,setsar=1:1,gblur=sigma=30[blur];[blur][crp1]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" out.jpg

Batch file without writing to log.txt:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set count=1
for /F "tokens=2* delims==" %%F in ('"ffmpeg -hide_banner -i in.jpg -vf scale=1920:1080,setsar=1:1,cropdetect=skip=0 -t 1 -f null - 2>&1"') do (
  set var!count!=%%F
  set /a count=!count!+1
)
echo %var1%

ffmpeg -y -i in.jpg -filter_complex "scale=1920:1080,setsar=1:1,crop=%var1%,split[crp0][crp1];[crp0]scale=1920:1080,setsar=1:1,gblur=sigma=30[blur];[blur][crp1]overlay=(main_w-overlay_w)/2:(main_h-overlay_h)/2" out.jpg
endlocal
Rotem
  • 30,366
  • 4
  • 32
  • 65