Command Line to Capture Audio and Video from a Webcam to file in 16.04LTS
OSS was the only audio API in Linux up through the 2.4 series of official (kernel.org) Linux kernels. ALSA was added starting with 2.5, and in those versions, Linux kernel authors marked OSS as deprecated. 4Front continued to develop OSS outside the Linux kernel.
Source: Open Sound System - Wikipedia
Locate the sound device using arecord -L
. Then use it in the commandline. The output is very extensive.
This will record 30 seconds of video from my webcam:
$ ffmpeg -f alsa -i plughw:CARD=C525,DEV=0 -f video4linux2 -s 320x240 -i /dev/video0 -t 30 out.mpg
The arecord output on my system is entered below with the audio device I chose to use highlighted in bold. In this case it's plughw:CARD=C525,DEV=0.
default Playback/recording through the PulseAudio sound server null Discard all samples (playback) or generate zero samples (capture) pulse PulseAudio Sound Server sysdefault:CARD=MID HDA Intel MID, ALC889A Analog Default Audio Device front:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog Front speakers surround21:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog 2.1 Surround output to Front and Subwoofer speakers surround40:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog 4.0 Surround output to Front and Rear speakers surround41:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog 4.1 Surround output to Front, Rear and Subwoofer speakers surround50:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog 5.0 Surround output to Front, Center and Rear speakers surround51:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog 5.1 Surround output to Front, Center, Rear and Subwoofer speakers surround71:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog 7.1 Surround output to Front, Center, Side, Rear and Woofer speakers dmix:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog Direct sample mixing device dmix:CARD=MID,DEV=2 HDA Intel MID, ALC889A Alt Analog Direct sample mixing device dsnoop:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog Direct sample snooping device dsnoop:CARD=MID,DEV=2 HDA Intel MID, ALC889A Alt Analog Direct sample snooping device hw:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog Direct hardware device without any conversions hw:CARD=MID,DEV=2 HDA Intel MID, ALC889A Alt Analog Direct hardware device without any conversions plughw:CARD=MID,DEV=0 HDA Intel MID, ALC889A Analog Hardware device with all software conversions plughw:CARD=MID,DEV=2 HDA Intel MID, ALC889A Alt Analog Hardware device with all software conversions sysdefault:CARD=HDMI USB Capture HDMI, USB Audio Default Audio Device front:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio Front speakers surround21:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio 2.1 Surround output to Front and Subwoofer speakers surround40:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio 4.0 Surround output to Front and Rear speakers surround41:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio 4.1 Surround output to Front, Rear and Subwoofer speakers surround50:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio 5.0 Surround output to Front, Center and Rear speakers surround51:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio 5.1 Surround output to Front, Center, Rear and Subwoofer speakers surround71:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio 7.1 Surround output to Front, Center, Side, Rear and Woofer speakers iec958:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio IEC958 (S/PDIF) Digital Audio Output dmix:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio Direct sample mixing device dsnoop:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio Direct sample snooping device hw:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio Direct hardware device without any conversions plughw:CARD=HDMI,DEV=0 USB Capture HDMI, USB Audio Hardware device with all software conversions sysdefault:CARD=C525 HD Webcam C525, USB Audio Default Audio Device front:CARD=C525,DEV=0 HD Webcam C525, USB Audio Front speakers surround21:CARD=C525,DEV=0 HD Webcam C525, USB Audio 2.1 Surround output to Front and Subwoofer speakers surround40:CARD=C525,DEV=0 HD Webcam C525, USB Audio 4.0 Surround output to Front and Rear speakers surround41:CARD=C525,DEV=0 HD Webcam C525, USB Audio 4.1 Surround output to Front, Rear and Subwoofer speakers surround50:CARD=C525,DEV=0 HD Webcam C525, USB Audio 5.0 Surround output to Front, Center and Rear speakers surround51:CARD=C525,DEV=0 HD Webcam C525, USB Audio 5.1 Surround output to Front, Center, Rear and Subwoofer speakers surround71:CARD=C525,DEV=0 HD Webcam C525, USB Audio 7.1 Surround output to Front, Center, Side, Rear and Woofer speakers iec958:CARD=C525,DEV=0 HD Webcam C525, USB Audio IEC958 (S/PDIF) Digital Audio Output dmix:CARD=C525,DEV=0 HD Webcam C525, USB Audio Direct sample mixing device dsnoop:CARD=C525,DEV=0 HD Webcam C525, USB Audio Direct sample snooping device hw:CARD=C525,DEV=0 HD Webcam C525, USB Audio Direct hardware device without any conversions plughw:CARD=C525,DEV=0 HD Webcam C525, USB Audio Hardware device with all software conversions
A lot of the many devices can be eliminated based on the audio you are trying to capture, such as the surround sound and in my case other HDMI capture devices.
Related videos on Youtube
Apologician
I'm an IT professional. An Internet pioneer where I was one of the first provider of ISP services (Apollo III Communications). People often ask about my signature. I sign my messages so that any question I ask or participate in can easily be found. Back in the 80's when I was starting out I hoped to never ask the same question twice. So I use text in my signature to find past discussions to give support to users. My email address is the oldest one on the Internet. -- L. James -- L. D. James [email protected] www.apollo3.com/~ljames
Updated on September 18, 2022Comments
-
Apologician over 1 year
Similar questions have been asked and answered regarding capturing video from a webcam. There are a number of accepted answers. However, they are either from ancient versions of Ubuntu that are no longer supported or are related to video only, not both video and Audio as the subject of this question. Also, the subject of this question is how to use the current LTS Ubuntu version.
At present, the official Ubuntu Wiki documentation also have outdated resolutions that don't work.
There are some GUI applications that can capture both audio and video from a webcam. But those will not suffice for the object of being able to have the ease of including the video in scripts.
Many of the other references to this subject are including variations of Ubuntu's Official Documentation wor Webcam support:
The line with the preceding
$
prompt is the command. The lines that follow is the error response.$ ffmpeg -f oss -i /dev/dsp -f video4linux2 -s 320x240 -i /dev/video0 out.mpg [oss @ 0x15f38a0] /dev/dsp: No such file or directory /dev/dsp: Input/output error
and
$ avconv -f oss -i /dev/dsp -f video4linux2 -s 320x240 -i /dev/video0 out.mpg [oss @ 0xd9b8a0] /dev/dsp: No such file or directory /dev/dsp: Input/output error
While the two commands above will fail, this command will capture 10 seconds of video from the webcam:
$ timeout 10 ffmpeg -f video4linux2 -s 320x240 -i /dev/video0 out.mpg
As you can see removing the problematic audio input option will remove the error. So what is missing is the supported audio input that would match this option. I'm trying to get input on how to use the official built-in audio support before installing and trying to configuration other audio packages.
I'm seeing references to installing other packages for sound support, but I would like to first have input on how to use what is already installed on Ubuntu 16.04LTS by default, if it's workable.
The output of
dmesg -w
after the USB Webcam is plugged in:[41763.153707] CPU7: Core temperature/speed normal [41763.153708] CPU3: Core temperature/speed normal [41845.576323] retire_capture_urb: 66 callbacks suppressed [41845.699994] usb 2-1.4: USB disconnect, device number 14 [41872.523599] usb 2-1.4: new high-speed USB device number 15 using ehci-pci [41872.882616] usb 2-1.4: New USB device found, idVendor=046d, idProduct=0826 [41872.882620] usb 2-1.4: New USB device strings: Mfr=0, Product=2, SerialNumber=1 [41872.882622] usb 2-1.4: Product: HD Webcam C525 [41872.882624] usb 2-1.4: SerialNumber: 94D25250 [41873.142026] usb 2-1.4: current rate 5666600 is different from the runtime rate 16000 [41873.145382] usb 2-1.4: current rate 8859453 is different from the runtime rate 24000 [41873.149264] usb 2-1.4: current rate 12572339 is different from the runtime rate 32000 [41873.152635] usb 2-1.4: current rate 10782671 is different from the runtime rate 48000 [41873.165044] usb 2-1.4: set resolution quirk: cval->res = 384 [41873.165529] uvcvideo: Found UVC 1.00 device HD Webcam C525 (046d:0826) [41873.178089] input: HD Webcam C525 as /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.2/input/input33 [41873.222700] usb 2-1.4: current rate 5530139 is different from the runtime rate 48000 [41873.226055] usb 2-1.4: current rate 698870 is different from the runtime rate 48000 [41873.243770] usb 2-1.4: current rate 1805767 is different from the runtime rate 48000 [41873.246765] usb 2-1.4: current rate 1763607 is different from the runtime rate 48000
-
ridgy over 6 yearsI'm not sure why you relate to oss and /dev/dsp. Does your webcam have a microphone included? Is there any device like
/dev/dsp*
? support.metacdn.com/hc/en-us/articles/… has some information on how to use alsa default audio input. -
Apologician over 6 yearsAs I mentioned in the question, I got the information from Ubuntu's support documentation. The documentation doesn't work. I don't fully understand oss and /dev/dsp. I'm sure since it once worked, but there is changes that have happened, but the documentation hasn't caught up yet. My Webcam has a mic. I use it all the time for audio note taking with a number of GUI applications. I also use it with security applications that captures both video and audio.
-
Apologician over 6 years@ridgy Thanks for the link! I've been experimenting with many commands for two days and so far haven't been able to capture any sounds. Are you able to capture sounds from any of your devices using
ffmpeg
? -
ridgy over 6 yearsI suppose your webcam is USB based. Thus, before plugging in the cam, call
dmesg -w
; then plug in the cam. This should show you if and to which devices the cam is connected. Please add the output to your question to maybe help you further. -
ridgy over 6 yearsYes. I am able to record audio (did that for more than 15 years). The only difficulty is to find the correct alsa device and to not have it blocked by PulseAudio. But you already asked that in another thread.
-
Apologician over 6 years@ridgy Thanks! I added the requested output to my question. I had been using something similar to what's in the Ubuntu Documentation (
/dev/dsp
) for years also. I'm just noticing that it stopped working. If we get it to working with 16.04 I'll give the suggestions to the community.ubuntu.com maintainers to update the docs. -
ridgy over 6 yearsSorry for the long delay. There is one way to have a specific USB device always linked to a defined device node: The udev rules. To find out which rules will be affected and which ones have to be modified, pleas run
udevadm test /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.2/input/input33
(as this was reported bydmesg
). There is a lot of information, but at the end there will be the important part (after the line reflecting the device path). The information supplied will then lead to creating a rule for this specific device. -
Apologician over 6 years@ridgy Thanks for the information. After nearly a week of experimenting and investigating I was able to find a definitive solution that works and is supported by the latest Ubuntu version. I added details as an answer.
-