Software Steeplechase

Hayden Steep’s development obstacle course. (Java, JEE, and beyond)

October 4, 2006

Comskip Monitor - A Java service

Filed under: Java, General

I used to own a VCR, but it broke down in the year 2000 and I never looked back. I had the mind set that Television will rot your brain and that there was nothing on worth watching, yet alone taping.

A marriage, busy job, and 2 kids later, I’ve rediscovered the joy of Vegging Out in front of the boob-tube. However, during my 1 hour time slot for watching, there was rarely anything on worth watching. So, I finally took the plunge and configured a PVR (Personal Video Recorder) system. I research Tivo (subscription fees), vs a stand-alone computer hooked up to my television (noise/up-front cost) and decided upon SageTV.

SageTV allowed me to leverage my existing home computer. All I had to do was install a TV Tuning Card, the Sage TV software, and a wireless device that connects to my television. The initial cost was about the price of a Tivo, but I don’t have subscription fees and I don’t have a noisy computer in my den.

There is a 3rd party commercial skipping program called Comskip that can be run on recorded video files that creates a text file containing the time indexes where commercials appear in the videos. The only problem with this program is that it has to be run manually. Manual goes against the very ease-of-use nature of PVR devices, so I decided to author a program that would run as a service which would automatically run Comskip on video files as SageTV created them.

Thus, Comskip Monitor was born. One of the more interesting challenges was making use of the Java Runtime and Process classes.

  Runtime runtime = Runtime.getRuntime();
  Process process = runtime.exec(cmd);
  int exitCode = process.waitFor();

Despite my best efforts, the executed program “Comskip” would always hang about a minute into processing. As it turns out, Windows processes have a stdout, errout, and stdin. The buffers for these streams aren’t very big, and if you don’t consume the output streams, the process will halt until you do.

  Process process = runtime.exec(cmd);
  StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
  StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "OUTPUT");

  errorGobbler.start();
  outputGobbler.start();

  int exitCode = process.waitFor();

  //Stream Gobbler
  class StreamGobbler extends Thread
  {
    InputStream is;
    String type;

    StreamGobbler(InputStream is, String type)
    {
      this.is = is;
      this.type = type;
    }

    public void run()
    {
      try
      {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line=null;
        while ( (line = br.readLine()) != null)
        {
          System.out.println(type + ">" + line);
        }
      }
      catch (IOException ioe)
      {
        ioe.printStackTrace();
      }
    }
  }

This JavaWorld article is the best resource I could find for explaining the intricacies of launching external processes.

Writing a Java service is a great way to learn about threading, process handling, and synchronizing blocks of code.

If you happen to be running a Windows based video recording software, I also recommend trying out Comskip Monitor.

2 Comments »

The URI to TrackBack this entry is: http://steep.blogsome.com/2006/10/04/comskip-monitor-a-java-service/trackback/

  1. If you can use Java 5+, you will probably want to look at ProcessBuilder instead of Runtime.exec().

    Comment by Curt Cox — October 4, 2006 @ 4:12 pm

  2. Thanks for the tip Curt. ProcessBuilder looks slightly more elegant than having to use the Runtime class. The SageTV software I use states Java 1.4.x as a requirement, so I targeted my code to that version. Maybe I can switch over to ProcessBuilder in a future release.

    Comment by Hayden — October 4, 2006 @ 4:21 pm

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com