[mythtv-users] How to ensure STB is powered ON? Re: SA3250HD is turning itself off?

Adam Hirsch mythtv at quakerporn.com
Thu Jan 10 18:50:03 UTC 2008

Evuraan (evuraan at gmail.com) wrote:

> Will this (or something like this?) work?

> vid="/dev/video0"
> cat $vid > /tmp/out1 ; sleep 10;  cat $vid > /tmp/out2
> diff /tmp/out1 /tmp/out2 1>/dev/null 2>/dev/null && echo "STB is powered off?"

> That is, out1, out2 will not differ if the STB is dead as it is
> grabbing blank frames.( and blank grab files /hopefully/ are exactly
> identical?) ;)

> out1,out2 will be differing it STB is sending stuff over to grab.

The files will be slightly different, so diff won't help you... but I'll
tell you what will: gzip.  10 seconds of all-black video will compress down
to a very small file, while 10 seconds of actual running video with
changing images and sound will not.  Someone smart on this list suggested
this solution to me a year ago, and it's worked a treat for me since then.

I'm attaching the quick hack I use to detect a blank screen from my
occasionally powered-off STB.  It's kinda stupid, but it's working okay.
If anyone else uses it and it works, great -- send me any suggestions!  If
it blows up your machine I don't know you and never have.  :)


Your ego is like your liver.  We evolved with egos because we need them 
to survive.  The problem is once you start letting your ego run the joint.  
It's like letting your spleen be in charge.  -- David Johansen
                       <adam at baz.org> Adam Hirsch <http://web.baz.org/~adam/>
-------------- next part --------------

# This is a very quick and dirty hack, mostly using cargo-cult code, to detect
# whether or not my STB is powered off and sending a blank video signal, or
# powered on and sending live video.  I grab 10 seconds of video from
# /dev/video, capture it to a file in /tmp, and gzip it.  10 seconds of live
# video will compress down to 5-6 megabytes; 10 seconds of blank screen
# compresses down to 500-600 kilobytes.  Unfortunately, while mythtv is
# recording something, I can't cat from /dev/video, so I also catch the
# situation where the resulting file is too small.
# I run this script as a non-privileged user out of cron every thirty minutes.  
# Please don't run this unless you understand what it's doing and feel
# comfortable adjusting it to suit your own setup.

use strict;
use warnings;

my $pid;
my $ownpid = $$;
my $finish = 0;
chomp (my $date = `date +%Y%m%d.%H:%M`);

 # How many seconds of video do you want to test?  10 is a good number, I've found.
my $howmanyvideoseconds = 10;

 # Here's the command to toggle the power to the STB; unless you're using a
 # MyBlaster serial IR blaster, your command will be different.
my $togglepower = "/usr/local/bin/MyBlaster.pl power";

 # $minbound is how small the video file has to be to be considered truly
 # erroneous.  This seems to happen only when MythTV is recording something
 # and this little job can't touch /dev/video0.  $normalbound is the lower
 # bound for how big the compressed video file needs to be in order to be
 # considered blank. In my setup, 10 seconds of blank video safely compresses
 # to much less than (1000000 bytes = a little under 1 megabyte), so that's my standard.

my $minbound = 100;

my $normalbound = 1000000;

 # Here's the command I use to grab some raw video off my capture device.

my $capturecmd = "/bin/cat /dev/video0 > /tmp/${ownpid}.mpg";

my $gzip = "/bin/gzip";
my $mpeg = "/tmp/${ownpid}.mpg";
my $mpeggz = "/tmp/${ownpid}.mpg.gz";

# Here's the subroutine to kill the fork'd children processes

sub timeout {

	# kill the process group, but not the parent process
	local $SIG{INT}  = 'IGNORE';
	local $SIG{TERM} = 'IGNORE';
	kill 'INT' => -$$;

	# eventually try also with TERM and KILL if necessary
	die 'alarm';

eval {

	local $SIG{ALRM} = sub { $finish = 1 };

# How many seconds of video to capture for the test.
	alarm $howmanyvideoseconds;

	die "Can't fork!" unless defined($pid = fork);    # check also this!
	if ($pid) {                                       # parent

  ## debugging statement, not really necessary
	# 	warn "child pid: $pid\n";

		# Here's the code that checks for the timeout and calls the timeout() subroutine

		while (1) {
			$finish and timeout() and last;
			sleep 1;

		waitpid($pid, 0);

	} else {                                          # child

 # Run!  Run!  Capture video!


		exit;    # the child shouldn't execute code hereafter

	alarm 0;


# So now the capture should be done.
# gzip the resulting video file.

system ("$gzip $mpeg") == 0 or die ("system call to gzip failed!\n");

my $filesize = (stat("$mpeggz"))[7];

print "$date ";

if ($filesize < $minbound) {

	print "ERROR: video filesize smaller than $minbound bytes: recording is likely running\n";

} elsif ($filesize < $normalbound) {

	print "WARNING: video file smaller than $normalbound bytes: $filesize bytes\n";
	print "Toggling power switch to cable box\n";

	system ("$togglepower");

} else {

	print "File appears large enough: $filesize bytes\n";


# Clean up the leftover file
unlink "$mpeggz";

# Sadly, sometimes the "cat /dev/video0" job doesn't kill properly; this is a
# stupid hack to work around that.  And requires a linux system which has pkill.

system ("pkill -f video"); 

print "---\n";

More information about the mythtv-users mailing list