command line – Why does ffmpeg sometimes hang when trying to grab a frame from a webcam?

I need to torture test a webcam to replicate a hardware fault, so I’m running a bash script to run ffmpeg and capture a single frame thousands of times. Sometimes this runs quickly, I can save a 50 or so frames in two minutes, other times it will hang on a single capture for nearly a full minute.

While it’s frozen, I can see the CPU usage on a single core is at 100%.

Is there anything about this bash script that would cause it to momentarily freeze like this?

#!/bin/bash

for i in {1..10000}
do
  printf "*******\n Iteration $i of 10000 \n******* \n"
  
  # Start ffmpeg
  ffmpeg -f video4linux2 -i /dev/video1 -vframes 1 -video_size 160x120 ~/Desktop/test-images/test${i}.png -y
  
  # Wait 1 second
  sleep 1

done

Running the command on it’s own gives the same behavior.

Read more here: Source link