FFmpeg - Apply blur over face
It is possible to apply temporal and spatial blurring to a segment/section – assuming the area you want to blur is a static location.
Original black lab pup image.
Using a mask image
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)
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:
- 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. - Blur the cropped area.
- Overlay the blurred area using the same
x
andy
parameters from the crop filter.
Multiple blurs over specific areas (without a mask)
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]; \
[ovr1][b2]overlay=120:80"
Specific area not blurred (without a mask)
"[0:v]boxblur=10[bg];[0:v]crop=200:200:60:30[fg];[bg][fg]overlay=60:30"
Additional stuff
The audio is being stream copied (re-muxed). No re-encoding, so it is fast and preserves the quality.
The blurred area will have a hard edge.
The blurred area can be moved around if you're good with arithmetic expressions, or see the sendcmd or zmq filters.
If you want to blur for a certain duration use the
enable
option on the boxblur or the overlay.See the FFmpeg Filters Documentation for other blurring filters (sab, smartblur, unsharp).
Some related questions: How to blur a short scene in a video and How to add pixellate effect.