summaryrefslogtreecommitdiff
path: root/dot/local/bin/selfie
blob: ef0adb208ce258f94cddf3502ba245db910a1a60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash

video_devices()
{
    v4l2-ctl --list-devices | sed -ne 's/^\t//p'
}

AUDIO_DEVICE=pulse
VIDEO_DEVICE=$(video_devices | head -n1)

[ "$VIDEO_DEVICE" ] || exit 1

MAX_RECORDING_TIME=00:05:00
[ "$RECORD_FOREVER" ] && unset MAX_RECORDING_TIME

NOW=$(date -Iseconds)

OUTPUT=recording.${NOW//[:T]/.}.avi
OUTPUT_LINK=recording.latest.avi

XVID_QUALITY=10

info()
{
    ffmpeg -hide_banner -f v4l2 -list_formats all -i "$VIDEO_DEVICE"
    v4l2-ctl --list-formats-ext

    # Such commands as these do nothing.  ffmpeg overrides them.
    #v4l2-ctl --set-fmt-video=width=800,height=600,pixelformat=MJPG
    #v4l2-ctl --set-fmt-video=width=1600,height=1200,pixelformat=MJPG
}

# The higher resolution matches the display, and so fills the screen while
# recording, but the field of view is larger in the 800x600 resolution, which
# also supports a higher framerate. Thus 800x600 seems like the "natural"
# resolution of the camera.  Though, it is still quite low.  :(

V4L2_OPTIONS='-input_format mjpeg -framerate 30 -video_size 800x600'
# V4L2_OPTIONS='-input_format mjpeg -framerate 15 -video_size 1280x800'
# V4L2_OPTIONS='-input_format mjpeg -framerate 15 -video_size 1600x1200'

drawtext='text=%{pts\\:hms}:x=5:y=h-lh-5:fontsize=18:fontcolor=white:box=1:boxcolor=0x00000099'
record()
{
	local timeout="${MAX_RECORDING_TIME+-t $MAX_RECORDING_TIME}"
	ffmpeg -hide_banner -loglevel error -stats \
		$timeout -f video4linux2 ${V4L2_OPTIONS} -i "$VIDEO_DEVICE" \
		$timeout -f "$AUDIO_DEVICE" -i default \
    \
		-c:v rawvideo \
    -vf scale=iw/2:ih/2,drawtext="${drawtext}" \
    -pix_fmt yuv420p \
    -f xv selfie \
    \
		-c:v libxvid \
		-c:a libmp3lame \
    -q:v $XVID_QUALITY \
		"$1"
}

silently() { "$@" >/dev/null 2>&1; }

silently amixer-enable-mic
banish
record "$OUTPUT"

[ -h "$OUTPUT_LINK" -o ! -e "$OUTPUT_LINK" ] && ln -sf "$OUTPUT" "$OUTPUT_LINK"