• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help me write a script to record from tv tuner in Linux

Sureshot324

Diamond Member
I'm trying to make a script that at a certain time will tune my tv tuner, start recording, keep recording for a certain amount of time, then stop. I can do the following from console:

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

This works, but the problem is the only way i know of to stop it is to hit control c. Also, I need a way to launch the script at a certain time. For example, if i want to record a half hour show that starts at 8pm, the script needs to auto launch at 8, run for exactly half an hour, then stop. Anyone know how to do this?
 
##########################################################
#!/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
 
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.
 
$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"
 
$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.
 
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"

NewBlackDak, 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?

 
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.
 
Originally posted by: Sureshot324
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"

NewBlackDak, 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
 
Back
Top