I’m trying to use my Raspberry Pi as a home monitoring system, together with a webcam.
Over the past few weeks or so, I encountered multiple problems, each of which wasn’t complicated, but due to the lack of documentation and/or proper error message, they took a long time to resolve.
- Video not playable
- Part of the setup was just to read from the webcam frame by frame, then write the frames to a video file, pretty much like https://gist.github.com/boyou/580bbdc0fedb7af5446ef5b8ccf4ce8f.
- No error/warning was raised, a video file was generated, but it couldn’t be played by VLC or the browser.
- I wasn’t so familiar with
ffmpeg
/ffprob
/ etc, so I ended up eyeballing and trying random things like different codecs, and finally found out that the size parameter toVideoWriter
is(width, height)
, while thendarray
is in row first order. - I could have used things like
ffmpeg -i tmp.mp4
orffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 tmp.mp4
, and noticed the video file is indeed malformed. - But anyway, opencv should have raised an error or warning.
- Low frame rate of video
- I was trying to let
ffmpeg
read the webcam and periodically write to video files, with command like this. - However, the frame rate was very low (10), instead of the specified 30.
- Luckily this time there was some useful log, “The driver changed the time per frame from 1/30 to 1/10”.
- Some search lead me to this answer, so we need to configure the codec twice, both to
mjpeg
.
- I was trying to let
- The almight
ffmpeg
- As mentioned earlier, continuous video from webcam.
- Split video into chunks.
- Crop or cut the video.
- There are some subtleties that can cause the time to be inaccurate. We can either don’t use
copy
or putss
/t
options AFTER the input.
- There are some subtleties that can cause the time to be inaccurate. We can either don’t use
And some reading I did:
– What exactly is “codec”: https://www.freemake.com/blog/video-codec/
—
c:v
,codec:v
, andvcodec
mean the same: https://lists.ffmpeg.org/pipermail/ffmpeg-user/2017-February/035335.html– What is moov atom: https://sanjeev-pandey.medium.com/understanding-the-mpeg-4-moov-atom-pseudo-streaming-in-mp4-93935e1b9e9a
— And
faststart
: https://superuser.com/questions/856025/any-downsides-to-always-using-the-movflags-faststart-parameter