LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-15-2006, 12:19 PM   #1
sureshot324
Member
 
Registered: Mar 2004
Posts: 49

Rep: Reputation: 15
Help me make a script to record from tv tuner


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?
 
Old 01-15-2006, 01:03 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

You can use the 'at' demon to start the recording.
Put you cammands into a file (e.g. tvrec.tmp). e.g.
Code:
ivtv-tune -c 30 -d /dev/video0
cat /dev/video0 > recording.mpg &
CATPID = $!
sleep 1800
kill -INT $CATPID
Then do
Code:
$ at -f tvrec.tmp 20:00
$ rm -f tvrec.tmp
That should record at 8pm for 30min (the sleep 1800 is 30 minutes).

Here's the script I use for just this purpose, but it uses mencoder.
Code:
#!/bin/bash

if [ $# = 0 ]
    then
    echo "Usage: $0 <TV Channel> <'at' time> <duration in minutes>"
    exit 1
fi

if [ $1 = "-h" ] || [ $1 = "--help" ]
    then
    echo "Usage: $0 <TV Channel> <'at' time> <duration in minutes>"
    echo "You probably want to start a bit early, and stop a bit late."
    exit 1
fi

if [ $# -ne 3 ]
    then
    echo "Usage: $0 <TV Channel> <'at' time> <duration in minutes>"
    exit 1
fi

case $1 in
    1)
	TVTUNE=40
	CHANNEL="BBC1";;
    2)
	TVTUNE=46
	CHANNEL="BBC2";;
    3)
	TVTUNE=43
	CHANNEL="ITV";;
    4)
	TVTUNE=50
	CHANNEL="C4";;
    5)
	TVTUNE=37
	CHANNEL="C5";;
    *)
	echo "Bad TV channel number."
	echo "Expecting 1-5."
	exit 1;;
esac

if ! echo "amixer set PCM 25%" > tvrec.temp
    then
    echo "Couldn't write to $PWD/tvrec.tmp"
    exit 1
fi
echo "amixer set PCM cap" >> tvrec.temp
echo "amixer set PCM,1 cap" >> tvrec.temp
echo "amixer set 'PCM Capture Source' 'Input 1'" >> tvrec.temp
echo 'DATESTRING=`date +%F_%T`' >> tvrec.temp
echo "mencoder -tv driver=v4l2:norm=PAL:input=0:width=320:height=240:chanlist=europe-west:audiorate=48000 -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=2500:keyint=15:acodec=ac3:abitrate=192:aspect=4/3 -ofps 25 -o tvtimed_\$DATESTRING\_$CHANNEL.mpg -endpos $3:00 tv://$TVTUNE" >> tvrec.temp

if ! at -f tvrec.temp $2
    then
    echo "Couldn't add job to 'at' demon."
    exit 1
fi

rm -f tvrec.temp
Everything above relies on atd running. Do 'ps -A | grep atd' to see if you have atd running.

Last edited by ilikejam; 01-15-2006 at 01:07 PM.
 
Old 01-15-2006, 09:06 PM   #3
nicois
LQ Newbie
 
Registered: Oct 2005
Distribution: ubuntu
Posts: 5

Rep: Reputation: 0
scripts

I've written 2 scripts which might be of use to you.

The first one (tvguide.py) is only really useful in australia, as it scans an australian site for tv programs you're interested in and updates your crontab to record them by calling tvrecord.sh

tvrecord.sh looks a bit similar to the previous poster. It will certainly need rework to make it do what you want, but it's probably worth looking at.

Partly because I'm lazy and have to go to work in a sec I'm going to just point you to my repo. This silly forum code won't let me insert a URL directly so you'll have to build it:
http s colon-slash-slash geheimdienst.no-ip.org slash svn/media

If you have any questions I'll be happy to answer them. If you want to post bits of my scripts that's fine, but I didn't want to post large scripts in a forum unless they're actually useful.

Just a few words:
- I use sleep instead of at to get the recording length right
- I have to first dump the stream directly from /dev/dvb/... for some reason. It might be something about my card.
- I've been experimenting with a few different modes. For example, some transmissions aren't 16/9 but just 4/3, but the dvb stream pads them with black.

If you want to test out tvguide.py for any reason, try pasting this into a file (e.g. programs.lst) and running:
tvguide.py --cron --listing programs.lst
Code:
doctor who, priority=4, fudge=-20, narrowscreen
south park
ali g, narrowscreen
the office, fudge=-20
cnnnn
new inventors, channelmatch=ABC2
speaking in tongues, duration=1510, fudge=0, priority=3
glass house, channelmatch=ABC2,fudge=-90
world news, fudge=0, priority=6, hourmatch=21, duration=1745, method=1PASS
I'm happy to explain how any of this works, but only if it's useful to someone.
 
Old 01-16-2006, 12:45 PM   #4
sureshot324
Member
 
Registered: Mar 2004
Posts: 49

Original Poster
Rep: Reputation: 15
Thanks a lot for the excellent replies! Here's the script I ended up with.

Code:
#!/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 * 10))

sleep $RECTIME
kill $CATPID
echo "Recording complete, process $CATPID terminated"
I was surprised how picky bash is about little syntax issues like where you can put spaces. What are the advantages of using mencode instead of cat? Does it make a smaller filesize? Is it practical to use mpeg4 encoding (like divx or xvid) on a realtime stream like this if I have a fast computer?

nicois I'm still interested in your tvlistings script because if that australian site gives the listings in xml format, it shouldn't be hard to adapt it to work with zap2it labs, which gives listings for many countries including canada. I'll check out your site.
 
Old 01-16-2006, 01:05 PM   #5
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
I use mencoder because my TV card's an old WinTV PCI effort which output YUV raw(ish) video. The files are about three times larger than full quality DVD standard MPEG. Not nice.
I also don't get any audio if I just cat /dev/video0.

mencoder uses about 50% processor time on an Athlon 2500+ when it's recording, so you could probably reliably get away with a 1.8GHz P4 or equivalent.

Dave
 
Old 01-16-2006, 01:36 PM   #6
bmcgonag
LQ Newbie
 
Registered: Jan 2006
Posts: 14

Rep: Reputation: 0
have you thought of using a mythbox? Not sure of all the necessities for it to work, but it looked pretty nifty on the show that gave a brief overview of how to set it up. I can, however, see the promise in using your own script...

anyway,

Mac
 
Old 01-16-2006, 01:38 PM   #7
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
You might want to consider storing your mpeg on some seperately mounted filesystem if you're not doing that already. That way, if for some reason your script gets accidently killed while in it's sleep, you won't end up recording into infinity and running yourself out of disk space in some critical filesystem. You might also set a backup for your sleep/kill algorithm. Right before your sleep statement add an "at" command to also do the kill one minute AFTER the sleep/kill should have already gotten it. Just some extra insurance.

e.g.,
Code:
echo "kill $PID" | xargs at "now + 61 minutes"
sleep 3600
kill $PID
NOTE: That's just pseudo-code on the "at" command above, you'll need to look up the syntax, I forget the exact syntax at the moment.
 
Old 01-16-2006, 03:31 PM   #8
nicois
LQ Newbie
 
Registered: Oct 2005
Distribution: ubuntu
Posts: 5

Rep: Reputation: 0
Quote:
Originally Posted by sureshot324
Thanks a lot for the excellent replies! Here's the script I ended up with.
What are the advantages of using mencode instead of cat? Does it make a smaller filesize? Is it practical to use mpeg4 encoding (like divx or xvid) on a realtime stream like this if I have a fast computer?

nicois I'm still interested in your tvlistings script because if that australian site gives the listings in xml format, it shouldn't be hard to adapt it to work with zap2it labs, which gives listings for many countries including canada. I'll check out your site.
I use mencoder for a couple of reasons - but I first dump the raw (mpeg2) stream to disk. This means that I don't depend on mencoder to keep up with real-time, and lets me end up with much smaller files - the DVB stream is fine if you want to burn what you captured to DVD or watch it on that system, but I stream it across a wireless network which can't keep up with 6000bit quality. Not to mention the files take a lot less disk space.

Quote:
Originally Posted by sureshot324
nicois I'm still interested in your tvlistings script because if that australian site gives the listings in xml format, it shouldn't be hard to adapt it to work with zap2it labs, which gives listings for many countries including canada. I'll check out your site.
The tvguide program doesn't act on XML unfortunately. It's a bit of a mess, partly because it was one of the first python scripts I wrote, but also because the tv site isn't even valid HTML, let alone XML. I've ended up using regular expressions and a state machine to identify the right values.

Having said that, I think it's only one method which generates a dict (aka map) of properties for each tv program to record - so you could use the rest of the script to update your crontab etc, if you liked.

ps: my uplink is a bit saturated at the moment - my apologies if you have trouble accessing my svn repo.
 
Old 01-16-2006, 10:52 PM   #9
sureshot324
Member
 
Registered: Mar 2004
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by haertig
You might want to consider storing your mpeg on some seperately mounted filesystem if you're not doing that already. That way, if for some reason your script gets accidently killed while in it's sleep, you won't end up recording into infinity and running yourself out of disk space in some critical filesystem. You might also set a backup for your sleep/kill algorithm. Right before your sleep statement add an "at" command to also do the kill one minute AFTER the sleep/kill should have already gotten it. Just some extra insurance.

e.g.,
Code:
echo "kill $PID" | xargs at "now + 61 minutes"
sleep 3600
kill $PID
NOTE: That's just pseudo-code on the "at" command above, you'll need to look up the syntax, I forget the exact syntax at the moment.
What would happen if I used the space on my partion? Obviously the cat command would run into an error, but Linux has it's own swap partion so it would still run fine wouldn't it? And i could just delete the mpeg file. My Linux partition is only 15gb and all my windows partitions are ntfs so they are not writeable from linux.
 
Old 01-16-2006, 11:03 PM   #10
nicois
LQ Newbie
 
Registered: Oct 2005
Distribution: ubuntu
Posts: 5

Rep: Reputation: 0
Quote:
Originally Posted by sureshot324
What would happen if I used the space on my partion? Obviously the cat command would run into an error, but Linux has it's own swap partion so it would still run fine wouldn't it? And i could just delete the mpeg file. My Linux partition is only 15gb and all my windows partitions are ntfs so they are not writeable from linux.
yes, but when your root partition fills up and /tmp becomes unwritable, you're in a world of pain.

At the very least you want one partition for media/home and one for the system. Use lvm if you want extra control, if you dare.
 
Old 01-16-2006, 11:51 PM   #11
sureshot324
Member
 
Registered: Mar 2004
Posts: 49

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by nicois
yes, but when your root partition fills up and /tmp becomes unwritable, you're in a world of pain.

At the very least you want one partition for media/home and one for the system. Use lvm if you want extra control, if you dare.
A world of pain eh? Can you be more specific?
 
Old 01-16-2006, 11:58 PM   #12
nicois
LQ Newbie
 
Registered: Oct 2005
Distribution: ubuntu
Posts: 5

Rep: Reputation: 0
Quote:
Originally Posted by sureshot324
A world of pain eh? Can you be more specific?
I'd better not put a link to goatse.cx here :-)

If your root partition runs out of room, any process which tries to write a file (e.g. a lock file, log file, etc.) will fail, and will terminate. This often means you can't log into your system, run any commands except built-in bash commands on already-opened bash sessions, etc. And because log files can't be written to you won't see any meaningful errors in them.

This is one reason why you want to put /home on a separate partition - so over-zealous users can't bring the entire system to its knees. It's also good to farm off /usr or at least /usr/local, and personally I have /usr/bulk which is where I put all my big files - I've also got that partition formatted to use a different block size.

If you don't mind your system crashing when you forget to check your disk free status before recording a program then don't worry. It's only TV after all.
 
Old 01-17-2006, 12:52 AM   #13
sureshot324
Member
 
Registered: Mar 2004
Posts: 49

Original Poster
Rep: Reputation: 15
Ah well, I guess if worst comes to worst I can download a live cd with windows, boot off that, and then delete the file.
 
Old 01-17-2006, 10:38 AM   #14
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Quote:
Originally Posted by sureshot324
Ah well, I guess if worst comes to worst I can download a live cd with windows, boot off that, and then delete the file.
You could do that. Most people would prefer not to crash their system in the first place. But, you could do that.

If you only have 15Gb for Linux, consider this. The OS and installed apps may well be using close to 5Gb, depending on what you have installed. 5Gb or more would not be an unusual setup. That leaves you 10Gb to play with, assuming you have no other big stuff already eating into that remaining 10Gb . Depending on the resolution and bitrates you're recording at, 10Gb might hold anywhere from 10 hours of captured video all the way down to TWO hours or less if you've gone way overboard on the "quality" settings. You may not have a lot of breathing room in the disk space department.
 
Old 01-17-2006, 10:55 AM   #15
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
If you have a seperate /home partition, the feel free to fill it. (Even more so if it's an ext2/3 filesystem - 5% is reserved for the root user anyway).

Dave
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
genius tvgo u202 tv tuner - no idea how to make work sett Linux - Hardware 0 10-19-2005 08:58 AM
how to make a tv-tuner work? spazm Linux - Hardware 3 09-01-2004 05:25 PM
Streamripper script to auto record Nigel_Tufnel Linux - Software 0 05-28-2004 11:33 AM
Tv-tuner with PHILIPS SAA7130. How to make it work? buboleck Slackware 4 04-30-2004 03:30 AM
I have an old school TV Tuner.. how can I make it work? trey85stang Linux - Hardware 3 12-21-2003 06:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration