Copy video for given duration

Needed a simple script to copy video data for a fixed amount of time.

A shell script is available on ivtv driver page at Example script to schedule recordings, but wanted to do something without sending kill signals, so here's a script using the alarm signal.

Here's a perl script: timed-copy that uses an alarm signal instead of kill.

To use this, save the contents of the timed-copy link as text file, make it executable, and fix the perl path in line 1 if needed, and it is then ready to run.

I use this for capturing PVR-150 MPEG2 encoded video, by running something like this:
  timed-copy -v -m 30 < /dev/video0 > `date +%b%d-%H%M`.mpg
and scheduling this with the at command, for example:
  at -v 1:30AM Sep 14

which will end up creating a file named Sep14-0130.mpg.

Simple! Beats installing large, complex packages to do simple recordings.

Run as timed-copy -h to get help on all arguments, use -v to get verbose messages, and -v -v to get debug messages also.
Version 1.2 added a --clock option to use elapsed wall clock instead of alarm(), if needed.
Version 1.3 fixed the problem with alarm() - Time::HiRes has issues with alarm times, if the conversion to microseconds is greater than 2^32. Fix is to eliminate use of Time::HiRes, since the higher time resolution is not needed for this script, and a resolution in seconds is fine. Script now uses the default alarm() which works fine. So, the --clock option should no longer be needed.

Tested on a Fedora FC5 Linux system.


Attempt at timed-copy

Hi there,

I gave this a try to record TV-input from my Hauppauge card using IVTV on Ubuntu. It worked, but only for 38 minutes, not the 110 that I specified. Have you had any similar experiences with it?


was there any error or debug message?

I use this all the time, but for 30 minutes, and it works fine.

In your case, did it print any error message? If the program fails, it prints out error messages ( lines start with word error)

And, try running with the -v option, to print debug messages also, and see if any meaningful message is displayed to help with this issue.

timer not working.

~/bin/timed-copy -v -m 80 blah.mpg
...will read using 524288 bytes buffer, for 80 minutes 282.36, wrote 282.36 MBytes, took 8.42 minutes

So it looks like the alarm is fubar.

I fixed it as such...

while (1)
my $tn = time();
last if ( (($tn - $t1) / 60) > $minutes)
# last if ($timerExpired);

Yeah not as fancy as using signals and timers, but it works :)

Ubuntu issues?

Any other Ubuntu users who can test and comment on this script? It is very surprising that alarm() is not working on Ubuntu.

In any case, to take of reported problem above, where alarm is exiting too early, have now added a option --clock to this script, it will continually sample the wall clock, and exit when the required time has elapsed. Of course, this may introduce problems if the input read call blocks for too long, then, this may never exit!

alarm() issue in perl's Time::HiRes

Using Fedora FC5, alarm exited after 38 minutes - and it always exits like clockwork, after 38 minutes, ran this test multiple times.
timed-copy -v -v -m 110 < /dev/zero > /dev/null
...will read using 524288 bytes buffer, for 6600 seconds
--debug: timer expired, with signal: ALRM 45319743.00, wrote 45319743.00 MBytes, took 38.42 minutes (using alarm timer)

Discovered that alarm() from perl's Time::HiRes (Version 1.86) converts 110 minutes to 2305 seconds, which is 38.42 minutes. This is probably an issue when the alarm() is set for a time, that in microseconds, is more than can fit in a 32-bit integer, for example:
110 mins becomes 6600*10^6 % 2^32 == 2305 secs, i.e 38.4 minutes, and 71.666 minutes becomes 5 seconds.

This is now fixed in the timed-copy script (Version 1.3 or later), by using the default system alarm(), and not using Time::HiRes.

Hi, thanks for this

Hi, thanks for this script.
Works great & with cron it solved the "recording tv" issue for me.
Installing Myth can wait for a while..

(hauppauge pvr 500mce, suse 10.1, ivtv-0.6.4)