Help me write a script to record from TV Tuner in Linux [control c] [tv tuner]

Q: I try a script that at some point will tune my , start recording, the recording for a certain amount of time, then stop. I can do the following from the console:

ivtv-tune-d 30-c / dev/video0
cat / dev/video0u003e recording.mpg

This work, but the problem is the only way I know to stop it is to hit c. control I also need a way to start the script at a certain time. For example, when I half-hour show starting at 8 hours record, the script needs to automatically start at 8, a period of exactly half hours, then stop. Anyone know how to do this?


Re:Sorry I did that without testing.

I used CAT=`which cat`, because I was on OSX when I first did this. It couldn't find cat!!


Re:maybe add an exit line at the end though?

I believe without one it'll exit with the return code of the last command so as long as his echo succeeds it'll always exit 0.


Re:Originally posted by: guy
I found a solution from another form. If I put a & after the cat command it will run in the background so the script can continue. Right after the cat command I put CATPID=$! which gets me the pid of the cat command so i can terminate it later. Here is my script so far:

#!/bin/bash

if [ $# == 0 ] || [ "$1" = "-h" ] ; then
echo "Usage: $0 channel duration"
exit 1
fi

ivtv-tune -c $1 -d /dev/video0

cat /dev/video0 > rec.mpg &
CATPID=$!
echo "Recording started, Process ID is $CATPID"

RECMINS=$2
RECTIME=$((RECMINS * 60))

sleep $RECTIME
kill $CATPID
echo "Recording complete, process $CATPID terminated"

guy, what is the reason you put CAT=`which cat`? Is it just in case the cat command is not in the $PATH? Also, why would you have a loop that seems to keep tuning, starting the recording, again and again till time is up?

I like your script (maybe add an exit line at the end though? :) ). The first one in this thread won't work. I also noticed the re-tuning, re-starting, etc. This is a problem, especially with

$CAT $VIDEO_DEVICE > $OUTPUT_FILE

If you do this every time, the > operator will clobber your original file. The final product (assuming the rest of the script works fine) will be a .mpg file containing the last frame of your show. I definitely agree the way to do it is something like

start recording
sleep a certain time
kill recording


Re:That script is a lot simpler and should work fine, it could use a little more error checking for things like making sure two instances of it aren't running at the same time but that's minor as long as you're careful.

Re:I found a solution from another form. If I put a & after the cat command it will run in the background so the script can continue. Right after the cat command I put CATPID=$! which gets me the pid of the cat command so i can terminate it later. Here is my script so far:

#!/bin/bash

if [ $# == 0 ] || [ "$1" = "-h" ] ; then
echo "Usage: $0 channel duration"
exit 1
fi

ivtv-tune -c $1 -d /dev/video0

cat /dev/video0 > rec.mpg &
CATPID=$!
echo "Recording started, Process ID is $CATPID"

RECMINS=$2
RECTIME=$((RECMINS * 60))

sleep $RECTIME
kill $CATPID
echo "Recording complete, process $CATPID terminated"

guy, what is the reason you put CAT=`which cat`? Is it just in case the cat command is not in the $PATH? Also, why would you have a loop that seems to keep tuning, starting the recording, again and again till time is up?


Re:$SECONDS is automatically initialized at 0, and incremented every iteration of the loop.

Where is it incremented? And even if it was, it wouldn't matter because the loop will only run once because the cat command will never return.

You need 2 scripts, one to start the capture and one to kill it. The first one should start the capture in the background (in nohup, screen, whatever) and the second one should be scheduled to kill it $RECORD_TIME minutes later, probbaly with at.


Re:$SECONDS is automatically initialized at 0, and incremented every iteration of the loop.
I'm not sure it works though looking at it. I'll have to try it again when I get home. I think it needs a "sleep 1" before "done"

Re:Maybe I'm missing something, but that script won't do what he wants. $SECONDS is never set to anything, so assuming bash sets it to nothing or 0 it'll always start the recording and then the script will hang at the $CAT command since cat will never return from reading the video device.

Re:################################################## ########
#!/bin/bash

#You need to set the following 4 parameters to your needs
##enter the time to record in minutes
RECORD_TIME=30
##The channel you want to tune to
CHANNEL=30
##The video device ivtv uses
VIDEO_DEVICE=/dev/video0
##The file you want to output video to.
##If using crontab, you'll need to set the full path
OUTPUT_FILE=recording.mpg

RECORD_SECONDS=$((RECORD_TIME * 60))
TUNE=`which ivtv-tune`
CAT=`which cat`

while [ "$SECONDS" -le "$RECORD_SECONDS" ]
do
$TUNE -c $CHANNEL -d $VIDEO_DEVICE
$CAT $VIDEO_DEVICE > $OUTPUT_FILE
done

exit 0
################################################## ########
Try that. You'll have to save it into a file with a name of your choosing, and make it executable.

You can then use crontab to schedule it whenever you want.
man crontab

You can set it up, so that you can set the time to record, channel, and output file with command-line parameters, but I'll refer you to a bash scripting guide if you want to do that


Related posts

Leave a comment

0 Comments.

Leave a Reply


click to changeSecurity Code

[ Ctrl + Enter ]