'FFMPEG hangs (sometimes) when trying to take a photo from USB camera?

I am attempting to write a script that takes a photo every 3 seconds from camera source "1", the -pix_fmt I think is unique to my USB camera.

Here is the script:

#!/bin/bash

set -e

while :
do
  nice_num=$(date +%s)
  
  ffmpeg -ss 00:00:00 -f avfoundation -pix_fmt yuyv422 -r 30.000030 -i "1" -t 1 -frames:v 1 -q:v 2 "image_$nice_num_%03d.jpg"
  
  wait

  sleep 3
done

The issue is half the time it successfully takes the photo, and half the time it just hangs forever echoing this (not useful):

ffmpeg version 5.0.1 Copyright (c) 2000-2022 the FFmpeg developers
  built with Apple clang version 13.1.6 (clang-1316.0.21.2)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/5.0.1 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
  libavutil      57. 17.100 / 57. 17.100
  libavcodec     59. 18.100 / 59. 18.100
  libavformat    59. 16.100 / 59. 16.100
  libavdevice    59.  4.100 / 59.  4.100
  libavfilter     8. 24.100 /  8. 24.100
  libswscale      6.  4.100 /  6.  4.100
  libswresample   4.  3.100 /  4.  3.100
  libpostproc    56.  3.100 / 56.  3.100

Then I CTRL-C 3 times and get:

^C^C^CReceived > 3 system signals, hard exiting

Other methods of "taking a photo every n seconds from bash" are also appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source