From http://www.jwz.org/xscreensaver/xscreensaver-5.23.tar.gz
[xscreensaver] / driver / xscreensaver-getimage-video
1 #!/usr/bin/perl -w
2 # Copyright © 2001-2013 Jamie Zawinski <jwz@jwz.org>.
3 #
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation.  No representations are made about the suitability of this
9 # software for any purpose.  It is provided "as is" without express or 
10 # implied warranty.
11 #
12 # This program attempts to grab a single frame of video from the system's
13 # video capture card, and then load it on to the root window using the
14 # "xscreensaver-getimage-file" program.  Various frame-grabbing programs
15 # are known, and the first one found is used.
16 #
17 # The various xscreensaver hacks that manipulate images ("slidescreen",
18 # "jigsaw", etc.) get the image to manipulate by running the
19 # "xscreensaver-getimage" program.
20 #
21 # The various screen savers invoke "xscreensaver-getimage", which will in
22 # turn invoke this program, depending on the value of the "grabVideoFrames"
23 # setting in the ~/.xscreensaver file (or in the app-defaults file, usually
24 # /usr/lib/X11/app-defaults/XScreenSaver).
25 #
26 # Created: 13-Apr-2001.
27
28 require 5;
29 #use diagnostics;       # Fails on some MacOS 10.5 systems
30 use strict;
31
32 my $progname = $0; $progname =~ s@.*/@@g;
33 my $version  = q{ $Revision: 1.22 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
34
35 my $tmpdir   = $ENV{TMPDIR} || "/tmp";
36 my $tmpfile  = sprintf("%s/xssv.%08x.ppm", $tmpdir, rand(0xFFFFFFFF));
37
38 my $verbose = 0;
39
40
41 # These are programs that can be used to grab a video frame.  The first one
42 # of these programs that exists on $PATH will be used, and the image file
43 # is assumed to be written to $tmpfile (in some image format acceptable to
44 # "xscreensaver-getimage-file", e.g., PPM or JPEG.)
45 #
46 # If you add other programs to this list, please let me know!
47 #
48 my @programs = (
49
50   "bttvgrab -d q -Q -l 1 -o ppm -f $tmpfile",   # BTTV
51   "qcam > $tmpfile",                            # Connectix Qcam
52   "gqcam -t PPM -d $tmpfile",                   # GTK+ Qcam clone
53
54   "v4lctl snap ppm full $tmpfile",              # XawTV 3.94.
55
56   "streamer -a -s 768x576 -o $tmpfile",         # XawTV
57   #   the "-a" option ("mute audio") arrived with XawTV 3.76.
58
59   "atitv snap $tmpfile",                        # ATI video capture card
60
61   "grab -type ppm -format ntsc -source 1 " .    # *BSD BT848 module
62         "-settle 0.75 -output $tmpfile",
63
64   "motioneye -j $tmpfile",                      # Sony Vaio MotionEye
65                                                 # (hardware jpeg encoder)
66
67   "vidcat -b -f ppm -s 640x480 > $tmpfile 2>-", # w3cam/ovcam
68
69   "vidtomem -f $tmpfile 2>&- " .                # Silicon Graphics
70         "&& mv $tmpfile-00000.rgb $tmpfile",
71
72   # "mplayer -really-quiet tv://0 " .           # Maybe works with some cams?
73   #         "-ao null -vo pnm -frames 1 2>&- " .
74   #     "&& mv 00000001.ppm $tmpfile",
75
76 );
77
78
79 sub error($) {
80   my ($e) = @_;
81   print STDERR "$progname: $e\n";
82   exit 1;
83 }
84
85 sub pick_grabber() {
86   my @names = ();
87
88   foreach my $cmd (@programs) {
89     $_ = $cmd;
90     my ($name) = m/^([^ ]+)/;
91     push @names, "\"$name\"";
92     print STDERR "$progname: looking for $name...\n" if ($verbose > 2);
93     foreach my $dir (split (/:/, $ENV{PATH})) {
94       print STDERR "$progname:   checking $dir/$name\n" if ($verbose > 3);
95       if (-x "$dir/$name") {
96         return $cmd;
97       }
98     }
99   }
100
101   $names[$#names] = "or " . $names[$#names];
102   error ("none of: " . join (", ", @names) . " were found on \$PATH.");
103 }
104
105
106 sub grab_image() {
107   my $cmd = pick_grabber();
108   unlink $tmpfile;
109
110   print STDERR "$progname: executing \"$cmd\"\n" if ($verbose);
111   system ($cmd);
112
113   if (! -s $tmpfile) {
114     unlink $tmpfile;
115     error ("\"$cmd\" produced no data.");
116   }
117
118   print STDERR "$progname: wrote \"$tmpfile\"\n" if ($verbose);
119   print STDOUT "$tmpfile\n";
120 }
121
122
123 sub usage() {
124   print STDERR "usage: $progname [--verbose] [--name | --stdout]\n";
125   exit 1;
126 }
127
128 sub main() {
129   while ($_ = $ARGV[0]) {
130     shift @ARGV;
131     if    (m/^--?verbose$/s) { $verbose++; }
132     elsif (m/^-v+$/s)        { $verbose += length($_)-1; }
133     elsif (m/^--?name$/s)    { }   # ignored, for compatibility
134     elsif (m/^-./)           { usage; }
135     else                     { usage; }
136   }
137   grab_image();
138 }
139
140 main;
141 exit 0;