Capturing Image from webcam in linux - linux

I want to capture a image through webcam in linux.
So i searched on internet and found out it's can be done using mplayer or vlc.
Following is the command for capturing image by mplayer and its corresponding error.
[root#localhost ~]# mplayer -vo png -frames 1 tv://
MPlayer SVN-r31628-4.4.4 (C) 2000-2010 MPlayer Team
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
Playing tv://.
TV file format detected.
Selected driver: v4l2
name: Video 4 Linux 2 input
author: Martin Olschewski <olschewski#zpr.uni-koeln.de>
comment: first try, more to come ;-)
v4l2: unable to open '/dev/video0': No such file or directory
v4l2: ioctl set mute failed: Bad file descriptor
v4l2: 0 frames successfully processed, 0 frames dropped.
Exiting... (End of file)
[root#localhost ~]#
Please Help me in solving this error.
I searched on net and found out about v4l2, but still can't solve the problem.

Your error is pretty clear. The webcam apparently is not registering at /dev/video0. In some instances, I've found that encoders or other video devices register under /dev/videoX, where X can be any integer. Try modifying your statement to try different video devices.

You can also try using fswebcam which has a useful feauture of skipping the first few frames
-some webcam show corrupt (or green) images for the first frame or two:
fswebcam --skip 2
It can be set to capture an image every second:
fswebcam --skip 2 --loop 1

I made it by doing this:
mplayer tv:// -tv driver=v4l2:device=/dev/video0 -fps 1 -frames 2 -sstep 100 -vo jpeg; rm 00000001.jpg ; mv 00000002.jpg capture.$(date +%F_%R).jpg.
where:
"tv:// -tv driver=v4l2:device=/dev/video0" specifies the necessary driver and video device. You need to check if that device is the one you want to capture and or exists by doing ls /dev/video*.
-fps 1 -frames 2 -sstep 100 specifies the framerate and it's set to only one per second to ensure you will have enough time to wake up properly the camera, captures 2 frames and in between frames you had 100 fps from the output dropped to help with the camera focus.
-vo jpeg stands for jpg output format images, on my computer png doesn't work with all this configuration.
rm 00000001.jpg deletes the first frame capture, because always it's ging to be obscure, green or no focused, this is caused by the small amount of time neede by the camera to wake up.
mv 00000002.jpg capture.$(date +%F_%R).jpg makes the second frame image switch name from 00000002.jpg for "capture." plus the current date time.

Related

arecord records silence instead of audio output

I am trying to record what my device is currently playing using arecord (it is playing audio via mplayer).
I've done everything I have been able to read so far such as checking under which user mplayer is running, setting the user as per the command below, but it only ever records silence.
This is the list of users where my user is ID 1000:
user:x:1000:1000:,,,:/home/user:/bin/bash
This shows that what I am trying to record is running by that user:
user 1217 2.3 1.3 131704 25524 ? SL 09:11 12:15 /usr/bin/mplayer http://example.com/stream
This is the command I am running:
XDG_RUNTIME_DIR=/run/user/1000 /usr/bin/arecord -d 1 /home/user/rec.waw
The command will output:
Recording WAVE '/home/user/rec.waw' : Unsigned 8 bit, Rate 8000 Hz, Mono
And it writes the file, but the content is silent.
I have tried specifying --device but not sure how to specify it from the list of devices below (I tried --device=CARD=Headphones and --device=hw:CARD=Headphones but I get errors.
This is the output of arecord -L
user#device:~ $arecord -L
null
Discard all samples (playback) or generate zero samples (capture)
jack
JACK Audio Connection Kit
pulse
PulseAudio Sound Server
default
Playback/recording through the PulseAudio sound server
output
usbstream:CARD=b1
bcm2835 HDMI 1
USB Stream Output
usbstream:CARD=Headphones
bcm2835 Headphones
USB Stream Output
Any help would be appreciated.

SOX exiting unusually, as if the user is canceling

I am trying to read a FM signal and piping it to sox. This is exiting unusually. Any idea what's going on?
Thanks.
$ rtl_fm -f $137M -s 60k -g 45 -p 55 -E wav -E deemp -F 9 - | sox -t wav - abc.wav rate 11025
Found 1 device(s):
0: Realtek, RTL2838UHIDIR, SN: 00000001
Using device 0: Generic RTL2832U OEM
Found Rafael Micro R820T tuner
Tuner gain set to 44.50 dB.
Tuner error set to 55 ppm.
Tuned to 37480000 Hz.
Oversampling input by: 32x.
Oversampling output by: 1x.
Buffer size: 4.27ms
sox FAIL formats: can't open input `-': WAVE: RIFF header not found
Sampling at 1920000 S/s.
Output at 60000 Hz.
Signal caught, exiting!
User cancel, exiting...
WAV header needs size of the audio content. Normally it is set after the whole content was captured. In your example you pipe the content as you capture it, so header is not set.
You should either use those separately instead of piping, or, assuming this rtl command allows it, pipe raw data. This probably will require to manually specify format (sampling frequency, bitdepth, etc) of the incoming data on the sox side.

Trim off N bytes from audio file using SoX / FFmpeg etc, on Windows?

My team accidentally on purpose clicked NO when Audacity asked to save the recording. So I left with bunch of *.au files, after using recovery program.
Some of them did have header and still open-able with audacity itself (example : this one), and some other are just complete nonsense, sometimes having the header filled with text from random javascript or HTML code (like this one). Probably hard disk half overwritten with browser cache? I don't know. And at this point, I almost don't care.
The audacity is on default settings, with sample rate 44100Hz. I can open a-113.au using audacity, from standard open files. I also was able to achieve open files using "Open RAW files" from Audacity, using this settings :
so I assume header takes 12384 bytes.
Now, how do I trim 12384 bytes from the file when opened as RAW, with either SoX or ffmpeg? because if I open it as RAW with 0 offset (default settings), it will add the header as a noise.
Current ffmpeg command I used : ffmpeg.exe -f f32le -ar 44.1k -ac 1 -i source destination
Current sox command I used : sox -t raw --endian little --rate 44100 -b 1 -b 32 --encoding floating-point %%A "converted/%%~nxA.wav"
Both still have header as a noise in the converted files.
With ffmpeg, use the subfile protocol.
ffmpeg.exe -f f32le -ar 44.1k -ac 1 -start 12384 -i "subfile:source" destination

Recording from multiple channels through sox

I am trying to record 2 different microphones through Sox for one of my application. I am currently just testing on Mac terminal for the audio recording. However, I am only able to get audio through one microphone.
The sox command I am using is:
sox -b 32 -e unsigned-integer -r 96k -c 2 -d --clobber --buffer $((96000*2*10)) /tmp/soxrecording.wav trim 0 10
Which give me a good .wav file.
I have two different usb microphones which show up as 2 channel I/p each in Sound/System Preferences. I tried to do -c 4 with the sox command to record from both microphones.
sox -b 32 -e unsigned-integer -r 96k -c 4 -d --clobber --buffer $((96000*2*10)) /tmp/soxrecording.wav trim 0 10
However, I get a warning saying;
sox WARN formats: can't set 4 channels; using 2
And I can just get audio in from only one usb microphone. I have been trying to fiddle and understand what’s wrong but any hints would be really helpful.
You need to specify that you are using ALSA, then run two different SOX command and have to specify the hardware you use for each one.
Please refer to soc, play, rec man page, basically you may use the -d, --default-device parameter when starting your command to specify which device you want to use.
Something like (simplified):
sox -r 44100 -c 2 -e s -t alsa hw:4,0 -d
sox -r 44100 -c 2 -e s -t alsa hw:3,0 -d
This page and this page may also help you.

alsa tool arecord not recognizing plughw:1,0 on Arch Linux

Edit: All of this was probably caused by a terribly configured microphone (or a faulty one, I changed laptops and now use Ubuntu instead of Arch Linux, so I actually don't have any idea). To record to a wav file, all I do now is run:
arecord -d $DURATION -f cd -t wav $OUTPUT_FILE_PATH
...replacing $DURATION with the duration of the recording in seconds, and $OUTPUT_FILE_PATH with the path to the desired file to write. I omitted the -D sysdefault argument as it caused problems for me (as with most things, your mileage may vary, so if the command doesn't work for you, try playing with several variables until it works).
Goes without saying, but all of this requires alsa-utils to be installed.
(The original question is left below, for those that still want to see it.)
Tl;dr version: arecord not recognizing plughw:1,0 , nor hw:1,0 , nor anything without the -D option
Whole story: I'm trying to make a simple voice assistant using a Bash script (I don't find Python/Perl easy for me to use, but that's just me). Dialogs are made in Zenity/KDialog. Voice recognition isn't included yet, so one has to type in the phrase/command. For now the program is represented in Spanish, but I plan to have an English version as well.
Doing my research, I found: http://blog.oscarliang.net/raspberry-pi-voice-recognition-works-like-siri/
But it doesn't work correctly on my machine.
[owner#arch-hp-2000-notebook-pc ~]$ ~/test-speech-input
“Recording… Press Ctrl+C to Stop.”
ALSA lib pcm.c:2267:(snd_pcm_open_noupdate) Unknown PCM “plughw:1,0″
arecord: main:722: audio open error: No such file or directory
“Processing…”
^C
[owner#arch-hp-2000-notebook-pc ~]$
It apparently has to do with the arecord -D "plughw:1,0" -q -f cd -t wav part.
Output of arecord -l:
[owner#arch-hp-2000-notebook-pc ~]$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: Generic_1 [HD-Audio Generic], device 0: ALC269VC Analog [ALC269VC Analog]
Subdevices: 1/1
Subdevice #0: subdevice #0
Output of arecord -L:
[owner#arch-hp-2000-notebook-pc ~]$
null
Discard all samples (playback) or generate zero samples (capture)
pulse
PulseAudio Sound Server
default
Default ALSA Output (currently PulseAudio Sound Server)
sysdefault:CARD=Generic_1
HD-Audio Generic, ALC269VC Analog
Default Audio Device
front:CARD=Generic_1,DEV=0
HD-Audio Generic, ALC269VC Analog
Front speakers
surround21:CARD=Generic_1,DEV=0
HD-Audio Generic, ALC269VC Analog
2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=Generic_1,DEV=0
HD-Audio Generic, ALC269VC Analog
4.0 Surround output to Front and Rear speakers
surround41:CARD=Generic_1,DEV=0
HD-Audio Generic, ALC269VC Analog
4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Generic_1,DEV=0
HD-Audio Generic, ALC269VC Analog
5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Generic_1,DEV=0
HD-Audio Generic, ALC269VC Analog
5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Generic_1,DEV=0
HD-Audio Generic, ALC269VC Analog
7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
[owner#arch-hp-2000-notebook-pc ~]$
Following the first part of the answer by #CharlesDuffy (thanks for the help):
[owner#arch-hp-2000-notebook-pc ~]$ ~/test-speech-input
Recording… Press Ctrl+C to Stop.
Processing…
You Said: [owner#arch-hp-2000-notebook-pc ~]$
Following the new answer, also by #CharlesDuffy (although this system is all AMD I think, no intel):
[owner#arch-hp-2000-notebook-pc ~]$ test-speech-input
Recording… Press Ctrl+C to Stop.
ALSA lib pcm.c:2267:(snd_pcm_open_noupdate) Unknown PCM CARD=Generic_1
arecord: main:722: audio open error: No such file or directory
Processing…
You Said: [owner#arch-hp-2000-notebook-pc ~]$
Following the newest answer by #CharlesDuffy:
[owner#arch-hp-2000-notebook-pc ~]$
Recording… Press Ctrl+C to Stop.
ALSA lib pcm_dsnoop.c:614:(snd_pcm_dsnoop_open) unable to open slave
arecord: main:722: audio open error: No such file or directory
Processing…
^C
[owner#arch-hp-2000-notebook-pc ~]$
Double-checked the volume of the internal mic, and it seemed to have selected a non-existent mic. Switching to the real mic yielded the same results.
I'm lost right now. Any other ideas? Is there any other command-line voice recording tool that might work or that might be easier to use (at least for me)?
Machine: HP 2000 Notebook PC, Arch Linux, uname -a returns Linux HOST_NAME 4.1.2-2-ARCH #1 SMP PREEMPT Wed Jul 15 08:30:32 UTC 2015 x86_64 GNU/Linux
The plughw:1,0 suggestion is specific to Raspberry Pi hardware, and doesn't necessarily apply elsewhere.
The first thing I'd suggest is removing the -D DEVICE argument entirely.
If that doesn't work, I'd suggest trying:
-D sysdefault
...for your basic on-board audio, as listed by arecord -L.

Resources