Skip to content

Instantly share code, notes, and snippets.

@koppi
Created May 27, 2016 21:51
Show Gist options
  • Save koppi/83d19855140575ef26bee1a50915347b to your computer and use it in GitHub Desktop.
Save koppi/83d19855140575ef26bee1a50915347b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Record a webcam's video and audio stream defined by the $V and $A variables
# to ~/Desktop/webcam-$timestamp.mkv with cvlc and PulseAudio.
#
# Required tools:
#
# sudo apt -y install v4l-utils uvcdynctrl pulseaudio-utils vlc-nox
#
# Webcams tested:
#
# * 046d:0809 Logitech, Inc. Webcam Pro 9000
#
# For all camera controls of the Logitech Pro 9000 to work properly,
# the following line is required in /etc/udev/rules.d/80-uvcdynctrl.rules :
#
# ACTION=="add", SUBSYSTEM=="video4linux", DRIVERS=="uvcvideo", RUN+="/lib/udev/uvcdynctrl"
#
# After adding this line, disconnect and reconnect your camera to trigger
# the new udev rules. If you are not yet a member of the "video" group,
# you should add yourself to it and login anew. After this you should be
# able to use the cameras full feature set, including autofocus, even as a
# regular user.
#
V=/dev/video1
# pactl list | grep -A2 'Source #' | grep 'Name: ' | cut -d" " -f2
A=alsa_input.usb-046d_0809_361D8E54-02
# pactl list | grep -A2 'Card #' | grep 'Name: ' | cut -d" " -f2
C=alsa_card.usb-046d_0809_361D8E54-02
D=~/Desktop
F="$D/webcam-`date +%Y-%m-%d-%H%M%S`"
###############################################################################
PROGRAM_FILE="$0"
PROGRAM_NAME="$(basename $0)"
#echo >&2 "$PROGRAM_NAME: started from '$PROGRAM_FILE' with options: $*"
if [ $(( ${BASH_VERSINFO[0]} )) -lt 4 ]
then
echo >&2
echo >&2 "$PROGRAM_NAME: ERROR"
echo >&2 "BASH version 4 or later is required."
echo >&2 "You are running version: ${BASH_VERSION}"
echo >&2 "Please upgrade."
echo >&2
exit 1
fi
require_cmd() {
which "$1" >/dev/null
if [ $? -ne 0 ]
then
echo >&2 "$PROGRAM_NAME: ERROR: Command '$1' is not found in the system path."
return 1
fi
return 0
}
require_cmd v4l2-ctl || exit 1
require_cmd pactl || exit 1
require_cmd uvcdynctrl || exit 1
require_cmd cvlc || exit 1
if [ ! -e $V ]; then
echo "$V does not exist. Exiting."
exit 1
fi
v4l2-ctl -d $V --list-formats --all -D
pactl list | grep -A2 'Source #' | grep 'Name: ' | cut -d" " -f2
ctl_set() {
uvcdynctrl -d $V -s "$1" "$2"
}
#
# set Logitech, Inc. Webcam Pro 9000 specific parameters
#
ctl_set "Raw bits per pixel" 0 # 8 bit colour depth
#ctl_set "Raw bits per pixel" 1 # 12 bit colour depth
ctl_set "Brightness" 150
ctl_set "Contrast" 31
ctl_set "Saturation" 24
ctl_set "White Balance Temperature, Auto" 0
ctl_set "Power Line Frequency" 1 # 50Hz
ctl_set "White Balance Temperature" 4700
ctl_set "Sharpness" 80
ctl_set "Backlight Compensation" 0
ctl_set "Exposure, Auto" 1
ctl_set "Exposure (Absolute)" 220
ctl_set "Exposure, Auto Priority" 0
ctl_set "Focus" 0
ctl_set "LED1 Mode" 0 # turn off led
ctl_set "LED1 Frequency" 0
#pactl set-port-latency-offset $C analog-input-mic 100000 # fix for audio sync?
cvlc v4l2://$V :v4l2-width=1280 :v4l2-height=720 :input-slave=pulse://$A.analog-mono ":sout=#transcode{vcodec=HFYU,scale=1,acodec=mp3,ab=256,channels=1,samplerate=48000,audio-sync}:file{mux=mkv,dst=$F.mkv}" -I dummy --clock-jitter=0 --sout-mux-caching=2000 --sout-transcode-high-priority
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment