How to record from webcam, with ffmpeg?
#1
Hi Folks,

I need to record some videos, and it would be nice to be able to use the PBP for this. Unfortunately, I'm having some trouble getting ffmpeg to do an acceptable job of keeping recorded audio and video in sync. I wonder if anyone has any tips, or has had any luck recording decent videos?

Here's the command I've been using:

Code:
v4l2-ctl --set-fmt-video=width=640,height=480 -d /dev/video4 #this video format gets a decent framerate

#I'm not sure if its necessary to force key frames. It seemed to help some.
ffmpeg -f v4l2 -i /dev/video4 \
       -f alsa -i default \
       -c:v libx264 -preset ultrafast -force_key_frames 00:00:00.000\
       -c:a aac \
       ~/$TITLE.mp4

I'm running an up-to-date manjaro system.

Thanks!
#2
Do you really have no other alternative than the webcam (smartphone, tablet, other computer)?
In my opinion the webcam doesn't qualify as decent.
When you really have to, try Cheese or Kamoso.

I have tried screen recording with FFMPEG, but gave up and went with SimpleScreenRecorder.
I don't know how to capture the command, but I thought it uses FFMPEG.
#3
(08-28-2020, 10:21 AM)jiyong Wrote: Do you really have no other alternative than the webcam (smartphone, tablet, other computer)?
In my opinion the webcam doesn't qualify as decent.
When you really have to, try Cheese or Kamoso.

I have tried screen recording with FFMPEG, but gave up and went with SimpleScreenRecorder.
I don't know how to capture the command, but I thought it uses FFMPEG.

I do have alternatives, so this isn't life or death, but I'd like to use the PBP (it would be convenient, and I'd just like to know how to do this, for the sake of knowing).

The video and audio quality are actually perfectly OK for my purposes. The only issue is audio synchronization.

It would be nice to be able to do this with ffmpeg or some simple command line too.
#4
For what it's worth, guvcview seems to have a similar issue - I can get a decent framerate, but audio and video are out of sync.
#5
Some tips and tricks (that may or may not help):

* Checkout different video encoding formats (h264 can be "heavy", motion jpeg or something could be lighter (but bigger))
* Compile ffmepg on the device with some CPU specific flags, try to use formats you have hardware acceleration for
* Check what formats come out of the camera, cheap webcams are usually YUYV, but some offer RGB and even jpeg (creating a motion jpeg from a jpeg is easy)
#6
I never bothered with the webcam (too crappy in my opinion), but I can add some experience with SimpleScreenRecorder.
I can record the 1080p screen with h264, so 480p should not be a problem.
So far I was never able to get clean sound from the built-in mic.
Example: https://www.youtube.com/watch?v=DGLrmzwp-YI

I decided to buy the RØDE NT-USB mini microphone and it solved my audio issues.
https://www.youtube.com/watch?v=XN7ZEmgVBzA

If all else fails, record the output of the webcam while displaying it with another app with SimpleScreenRecorder.
You can tell SimpleScreenRecorder to record just a part of the screen.
#7
So, for what it's worth, I've so far gotten decent audio/video synchronization with the following ffmpeg invocation:
Code:
#!/usr/bin/env bash

echo "enter title"
read TITLE

v4l2-ctl --set-fmt-video=width=640,height=480,pixelformat=YUYV -d /dev/video3

ffmpeg -f v4l2 -framerate 20 -thread_queue_size 1024 -use_wallclock_as_timestamps 1 -i /dev/video3 \
       -f alsa -thread_queue_size 1024 -use_wallclock_as_timestamps 1 -i default \
       -c:v libx264 -preset ultrafast \
       -c:a aac -copyts \
       ~/$TITLE.mp4
The key ingredient for achieving decent synchronization seems to be -use_wallclock_as_timestamps on  both alsa and v4l2, and -copyts at the end.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Webcam not working in Manjaro with Cheese, but works in Armbian petaramesh 1 1,031 09-30-2022, 10:09 AM
Last Post: crazyquark
  WORKING Moonlight with FFMpeg on 5.10.11 ShootTheMoon2 2 3,172 11-12-2021, 09:42 PM
Last Post: tblindba
  webcam framerate bottleneck on linux? gleachkr 0 1,797 10-04-2020, 12:55 PM
Last Post: gleachkr

Forum Jump:


Users browsing this thread: 1 Guest(s)