FFmpeg – Apply blur over face


I'm trying to blur a portion of a video using FFmpeg (specifically to blur a face).

I have been trying to use a combination of timeline editing and the various bluring filters, but I cannot find a way to blur only a section of the video.

I'm hoping for something like:

-vf boxblur=enable='between(t,10,100)':width=20:height=20:x=400:y=200

Where width/height is size of blurred box and x/y are location of blurred box.

Is something like this possible?

Best Answer

It is possible to apply temporal and spatial blurring to a segment/section – assuming the area you want to blur is a static location.

Black lab pup
Original black lab pup image.

Using a mask image

enter image description hereenter image description here
Grayscale PNG mask image and resulting blurred image.

You can make a grayscale mask image to indicate the area to blur. For ease of use it should be the same size as the image or video you want to blur.

Example using alphamerge, boxblur, and overlay:

ffmpeg -i video.mp4 -i mask.png -filter_complex "[0:v][1:v]alphamerge,boxblur=10[alf];[0:v][alf]overlay[v]" -map "[v]" -map 0:a -c:v libx264 -c:a copy -movflags +faststart maskedblur.mp4
  • The white area is where the blur will occur, but this can easily be reversed with the negate filter for instance: [1:v]negate[mask];[0:v][mask]alphamerge,boxblur=10[alf]...

  • You could use the geq filter to generate a mask such as a gradient.

Blur specific area (without a mask)

Black lab pup with blur effect

ffmpeg -i derpdog.mp4 -filter_complex \
 "[0:v]crop=200:200:60:30,boxblur=10[fg]; \
  [0:v][fg]overlay=60:30[v]" \
-map "[v]" -map 0:a -c:v libx264 -c:a copy -movflags +faststart derpdogblur.mp4

Note: The x and y offset numbers in overlay (60 and 30 in this example) must match the crop offsets.

What this example does:

  1. Crop the copy to be the size of the area to be blurred. In this example: a 200x200 pixel box that is 60 pixels to the right (x axis) and 30 pixels down (y axis) from the top left corner.
  2. Blur the cropped area.
  3. Overlay the blurred area using the same x and y parameters from the crop filter.

Multiple blurs over specific areas (without a mask)

enter image description here
Blurred areas in top left, near center, and bottom.

"[0:v]crop=50:50:20:10,boxblur=10[b0]; \
 [0:v]crop=iw:30:(iw-ow)/2:ih-oh,boxblur=10[b1]; \
 [0:v]crop=100:100:120:80,boxblur=10[b2]; \
 [0:v][b0]overlay=20:10[ovr0]; \
 [ovr0][b1]overlay=(W-w)/2:H-h[ovr1]; \

Specific area not blurred (without a mask)

enter image description here


Additional stuff

Related Question