Month: November 2008

Short script to add a timestamp on pictures

Here is a short script (1.6kb) to add a timestamp on all PNG pictures in a directory. It requires Python and the Python Image Library (PIL). In order to use it, modify some parameters in the beginning to suit your needs (images directory, font file and size, etc.) and launch ./timestampFiles.py. Here is a before/after example (size of pictures is reduced to fit in this blog):

Before/after example of adding a timestamp to a picture

Note 1: the font is not included in the script. Here, I used FreeSans which is a true free font (GNU GPL with font exception). It is available here (local copy, just the FreeSans font, 753kb).

Note 2: I chose to convert PNG images to JPG ones for 2 reasons. First, my capture script gives me PNG images (see previous posts). Second, I want to be able to copy all JPG pictures in a MJPEG movie. It shouldn’t be difficult to change the different file formats to suit your needs (ask me if you can’t do that).

Note 3: I guess this would also have been possible using a bash script and imagemagick, Perl or any other programming language 😉

Taking automated screenshots from a live video camera

Following my previous post, I attached a video camera to the composite input of my tv tuner. One good thing I didn’t noticed yesterday is that mplayer can be told to directly use pvr:// as a source instead of the generic tv:// (with many options). So you just have to enter mplayer pvr:// -tv device=/dev/video1:input=0 in order to watch tv.

Noticed the input=0 above? This tells the tuner to take the video signal from the tv (read the mplayer man page to see how to change the channel). Now, since I connected my video camera to the composite video in, I need to tell mplayer to use it with input=1. One last thing: taking a screenshot in mplayer is done by pressing the ‘s’ key (with option -vf screenshot. In summary, the image below was taken with mplayer pvr:// -tv device=/dev/video1:input=1:noaudio -vo x11 -vf screenshot
(camera facing the screen).

blurry capture of computer screen taken by camera connected to tv tuner

Now I want to take one screenshot every 5 seconds, even when I’m not there to press the ‘s’ key! For this purpose, we need to use a fifo file and mplayer in slave mode. Mplayer in slave mode will listen to commands we automatically send into the fifo file (by a different process, see below). This is how we do this:

mkfifo myfifofile.tmp
mplayer -slave -input file=myfifofile.tmp pvr:// -tv device=/dev/video1:input=1:noaudio -vo x11 -vf screenshot

And from another console, we can type echo "screenshot 0" >> myfifofile.tmp to take the screenshot. To automate all this, the following simple bash code is sufficient:

#!/bin/bash
# will send mplayer screenshot command every 5 seconds to fifo file
# stop this with Ctrl + C
LIMIT=0
while [ $LIMIT -lt 1 ]; do
echo "screenshot 0" >> myfifofile.tmp
sleep 5
done

In the end, stop the bash script with Ctrl + C and quit mplayer with echo "quit" >> myfifofile.tmp.

A first step toward TV on my Linux laptop

I recently got a Hauppauge WinTV-PVR-USB2 (a TV tuner, video recorder and FM receiver) because I read it was well supported on GNU/Linux. The following post explains how I installed it on a Fedora Core 9. If you want to install it with another Linux distribution, some information may vary but most of the following steps will be exactly the same.

First connect the USB device, the list of USB devices shows my system has recognised it:

[root@localhost ~]# lsusb
Bus 002 Device 003: ID 2040:2900 Hauppauge
...
[root@localhost ~]# dmesg
usbcore: registered new interface driver pvrusb2
pvrusb2: Hauppauge WinTV-PVR-USB2 MPEG2 Encoder/Tuner : V4L in-tree version
pvrusb2: Debug mask is 31 (0x1f)
firmware: requesting v4l-pvrusb2-29xxx-01.fw
pvrusb2: ***WARNING*** Device fx2 controller firmware seems to be missing.
pvrusb2: Did you install the pvrusb2 firmware files in their proper location?
pvrusb2: request_firmware unable to locate fx2 controller file v4l-pvrusb2-29xxx-01.fw
pvrusb2: Failure uploading firmware1
pvrusb2: Device initialization was not successful.
pvrusb2: Giving up since device microcontroller firmware appears to be missing.

Although my tuner is fully recognised, the system needs a firmware in order to make it work. Where is the firmware? A quick search on a well-known search engine redirects us to Mike Isely’s website where he explains how to get the driver (Mike actually also wrote the driver for this TV tuner). Although I got the CD with drivers for Windows, I prefer to use the latest driver that can be found on the Hauppage support website. Mike also writes you can get the driver from the ivtv project.

So I uncompress the driver in the “win_driver” directory and launch Mike Isely’s fwextract.pl script:

[jepoirrier@localhost hauppauge]$ mkdir win_driver
[jepoirrier@localhost hauppauge]$ unzip hauppauge_cd_3.4d1.zip -d win_driver/
[jepoirrier@localhost hauppauge]$ ./fwextract.pl

The script extracted 4 drivers (2 decoders and 2 encoders) and dmesg already told me the two I need: v4l-pvrusb2-29xxx-01.fw and v4l-cx2341x-enc.fw (as encoder). To know where to put these files, check a little bit on the web (this depends on your GNU/Linux distribution). On the Fedora Core 9, it’s in /lib/firmware, so:

[root@localhost hauppauge]# cp v4l-cx2341x-enc.fw /lib/firmware
[root@localhost hauppauge]# cp v4l-pvrusb2-29xxx-01.fw /lib/firmware

Unplug your USB cable, re-plug it and a dmesg should give you a long list of what your system detected, the most interesting part being the last line where it request the firmware and doesn’t stop: firmware: requesting v4l-cx2341x-enc.fw 🙂 Now check your video devices and you should have an additional /dev/videox:

[root@localhost hauppauge]# ls /dev/vid*
/dev/video /dev/video0 /dev/video1

In my case, /dev/video1 is the one I need …

Now, how to watch TV? TVTIME seems to be good but, unfortunately, my video card driver doesn’t support hardware YUY2 overlay (I have an ATI Radeon Mobility HD 2600 with the open source radeon driver). No problem: mplayer /dev/video1 shows a very beautiful snow. Now I need an antenna or a cable to connect to my tuner …

mplayer -vo x11 /dev/video1 -vf and then s