3

I'd like to convert videos to black and white but also specify the threshold of what to convert to black/white.

I'm currently using cv2 to do this successfully using the 2 lines below (first converts to grayscale then B&W). But it would help my workflow to do everything with FFMPEG if possible.

    grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    (thresh, BW) = cv2.threshold(grayFrame, 10, 255, cv2.THRESH_BINARY)

3 Answers 3

4

Use the threshold filter:

enter image description here

ffmpeg -i main_1280x720.mp4 -f lavfi -i color=gray:s=1280x720 -f lavfi -i color=black:s=1280x720 -f lavfi -i color=white:s=1280x720 -filter_complex threshold output.mkv

This filter requires four video streams to perform thresholding:

  1. main input
  2. threshold
  3. min
  4. max

You can optionally add the format filter for a grayscale pixel format: threshold,format=gray (assuming your output format supports that otherwise additional pixel format conversion will automatically take place).

1

The main answer didn't work for me, this did:

ffmpeg -i input.mkv -f lavfi -i color=gray:s=1280x720 -f lavfi -i color=black:s=1280x720 -f lavfi -i color=white:s=1280x720 -filter_complex "[0:v]scale=1280x720,threshold" output.mkv
1

maskfun filter has a slightly easier syntax and I assume gets the same result.

ffmpeg -i video.mp4 -vf format=gray,maskfun=low=128:high=128:fill=0:sum=128 video-bw.mp4

https://ffmpeg.org/ffmpeg-filters.html#maskfun

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.