Install a webcam in Slacko 5.3

How to do things, solutions, recipes, tutorials
Message
Author
User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#16 Post by MochiMoppel »

greengeek wrote:Most of the answers I have seen seem to require v4l2-ctl utility but I don't have it in my system
So do I, but this utility is not necessary.
This lists the attached cameras :

Code: Select all

# cat  /sys/class/video4linux/*/name
ELECOM UCAM-DLK130T
HP Webcam-50
Funny, yesterday my external ELECOM webcam was video1, today it's on top of the list, i.e in directory video0. Might depend of the timing of its plug in and makes it even more inportant to check the parameter.

This should do. Takes a single shot with my webcam:

Code: Select all

#!/bin/bash
CAMERA=ELECOM
VIDEOx=$(grep -rH "$CAMERA" /sys/class/video4linux/*/name | cut -d/ -f5)
ffmpeg -f video4linux2 -s 640x480 -i /dev/$VIDEOx -vframes 1 /tmp/$(date +%T)_$CAMERA.jpg
Edit: Fixed grep command
Last edited by MochiMoppel on Mon 10 Jul 2017, 08:54, edited 1 time in total.

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#17 Post by greengeek »

MochiMoppel wrote:This should do. Takes a single shot with my webcam:

Code: Select all

#!/bin/bash
CAMERA=ELECOM
VIDEOx=$(grep -r "$CAMERA" /sys/class/video4linux/*/name | cut -d/ -f5)
ffmpeg -f video4linux2 -s 640x480 -i /dev/$VIDEOx -vframes 1 /tmp/$(date +%T)_$CAMERA.jpg
I was not able to get this to work for my Chicony webcam. It returned an error that /dev/Chicony was not found (it's not there of course - it should have been looking for /dev/video0 or video1 instead.

I tried using the line:

Code: Select all

grep -r "Chicony" /sys/class/video4linux/*/name | cut -d/ -f5
to see what it produced but it returned nothing for me. Is that line trying to identify the "0" or "1" to be grafted into "VIDEOx"?

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#18 Post by MochiMoppel »

Aaah...I know what's wrong. Works only with both video0 and video1 directories present. Your Chicony is probably the only camera attached, so there will be only one directory.

Change grep -r to grep -rH

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#19 Post by greengeek »

Excellent, many thanks.

Code: Select all

# grep -rH "Chicony" /sys/class/video4linux/*/name | cut -d/ -f5
video0
# 

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#20 Post by greengeek »

That works nicely - with the exception that the first frame from my camera is very dark. This is apparently because the camera has to run several frames to adjust it's autoexposure, so I tried changing the frames parameter to 10 with the intention of seeing how many frames it takes to lighten up to a satisfactory state. However I got only one frame (too dark again) and the following messages in terminal:

Code: Select all

# ./camshot
ffmpeg version 2.0 Copyright (c) 2000-2013 the FFmpeg developers
  built on Jul 16 2013 10:17:34 with gcc 4.7.1 (GCC)
  configuration: --prefix=/usr --cpu=i686 --enable-libmp3lame --enable-libx264 --enable-libfaac --enable-pthreads --enable-small --enable-postproc --enable-libvorbis --enable-gpl --enable-shared --enable-nonfree --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-debug --enable-bzlib --enable-zlib --enable-libspeex --enable-version3 --enable-runtime-cpudetect --enable-x11grab --enable-libdc1394 --enable-libschroedinger --enable-libtheora --enable-libxvid --enable-swscale --enable-libvpx
  libavutil      52. 38.100 / 52. 38.100
  libavcodec     55. 18.102 / 55. 18.102
  libavformat    55. 12.100 / 55. 12.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 79.101 /  3. 79.101
  libswscale      2.  3.100 /  2.  3.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  3.100 / 52.  3.100
Input #0, video4linux2,v4l2, from '/dev/video0':
  Duration: N/A, start: 2156.844362, bitrate: 147456 kb/s
    Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640x480, 147456 kb/s, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
Output #0, image2, to '/tmp/20:50:48_Chicony.jpg':
  Metadata:
    encoder         : Lavf55.12.100
    Stream #0:0: Video: mjpeg, yuvj422p, 640x480, q=2-31, 200 kb/s, 90k tbn, 30 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (rawvideo -> mjpeg)
Press [q] to stop, [?] for help
[image2 @ 0x8081260] Could not get frame filename number 2 from pattern '/tmp/20:50:48_Chicony.jpg' (either set updatefirst or use a pattern like %03d within the filename pattern)
av_interleaved_write_frame(): Invalid argument
# 
Any thoughts how multiple frames could be gathered? I would like to throw out all of the dark frames and just grab the first clear one - which is probably about frame 8 judging by my previous multiframe tests using the following:

Code: Select all

mplayer tv:// -tv driver=v4l2:device=/dev/video0:width=1600:height=1200:outfmt=rgb24 -frames 12 -vo jpeg

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#21 Post by MochiMoppel »

Maybe this already helps:
Aunt Google wrote:ffmpeg -f video4linux2 -s 640x480 -i /dev/video0 -ss 0:0:2 -frames 1 /tmp/out.jpg

The -ss parameter is used to allow the device to start up correctly. Here in my tests, there is a fade-in effect while the camera is being turned on, so, if I just omit -ss 2, the captured frame will be very dark.
At least it helped me. When taking consecutive shots at 1 frame/sec the first 2 pictures turn out too dark. I noticed that ffmpeg takes 3 pictures rapidly before adjusting to the 1 per second rate, which may explain the dark (under exposed) shots.

I also discovered that I can take less than 1 picture per second.
This will take 30 consecutive pictures, one every 2 seconds. Names start with shot-0001.jpg:

Code: Select all

ffmpeg -f video4linux2 -s 640x480 -i /dev/video0 -ss 2 -vf fps=0.5 -vframes 30 /tmp/shot-%04d.jpg

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#22 Post by greengeek »

Awesome. That is great.
cheers!

Code: Select all

#!/bin/bash 
#Test script to take PC webcam images using MochiMoppel code from
#http://www.murga-linux.com/puppy/viewtopic.php?p=960402#960402
#Substitute the name of your preferred webcam (you may have more than one)
#Find your webcam name by opening a terminal and typing the following:
#lsusb
#Note: multiple uses of the script will overwrite previous images.
CAMERA=Chicony
VIDEOx=$(grep -rH "$CAMERA" /sys/class/video4linux/*/name | cut -d/ -f5) 
ffmpeg -f video4linux2 -s 640x480 -i /dev/$VIDEOx -ss 2 -vf fps=0.5 -vframes 5 /tmp/shot-%04d.jpg
The only problem is it appears to generate images of a rather dishevelled looking individual sitting in my living room hunched over a keyboard. Ugly.

Post Reply