[mythtv-users] Re: DVB card and nuvexport....

George Styles george at ripnet.co.uk
Thu Sep 30 15:23:12 EDT 2004


It is very unlovely, but here it is (its php)

This is the new version (awaiting full command-line support for nuvexport).

(see below for old version which uses old nuvexport)

---------------------------------------------------
g

---------------------------------------------------

#!/usr/bin/php
<?php
// NEW VERSION

class RipArchiver
{
  // Settings
  var $sqlhost = "localhost";
  var $sqlusername = "mythweb";
  var $sqlpassword = "mythweb";
  var $sqldatabase = "mythconverg";

  var $archivedflag = "[archived] ";

  var $radiochannels = array("1019","1043","1017");

  var $archivedir = "/dump/mythtv/archive"; // NB NO TRAILING /

  // Private
  var $chandle; // Connection handle to mysql

  // nuvexport --chanid=1019 "--starttime=20040731090000" --mode 
lamemp3 --outfile="/dump/georgeout.mp3" --a_bitrate=64
  function pathExistsAndContainsFile($path)
  {
//    echo "Checking if $path exists...\n";
    if (! file_exists($path))
      return false;
    if (! is_dir($path))
      die("Expected directory $path is a file!\n");

    $res = false;
    $handler = opendir($path);

    // keep going until all files in directory have been read
    while ($file = readdir($handler))
    {
      if ($file != '.' && $file != '..')
        $res = true;
    }
    // tidy up: close the handler
    closedir($handler);

    return $res;
  }

  function runNuvExport($row, $path, $flags)
  {
    $starttime = $row["starttime"];
    // Want from 2004-08-02 20:30:00 to 20040802203000

    $starttime = str_replace(array("-"," ",":"), array("","",""), 
$starttime);


    $cmd = 
"nuvexport --chanid=".$row["chanid"]." --starttime=".$starttime." --path 
\"".$path."\" ".$flags;
    echo "$cmd\n";
//    return false; //DEBUG
    system($cmd);
    // Check that output file exists - return false if not...
    $res = ($this->pathExistsAndContainsFile($path));
    if (! $res)
      echo "Failed to create output file in path $path.\n";
    return $res;
  }

  function archiveHQAudio($row)
  {
    $path = $this->generateExportPath($row, "audio", "mp3");

    if ($this->pathExistsAndContainsFile($path))
    {
      echo "Already Archived as audio.\n";
      return true;
    }
    echo "Archiving as audio to path $path\n";
    return  $this->runNuvExport($row, $path, "--mode mp3 --a_bitrate=128");
  }

  function archiveHQVideo($row)
  {
    $path = $this->generateExportPath($row, "hqvideo", "avi");

    if ($this->pathExistsAndContainsFile($path))
    {
      echo "Already Archived as HQ Video.\n";
      return true;
    }
    echo "Archiving as HQ Video to path $path\n";
    return $this->runNuvExport($row, $path, "--mode 
XviD --a_bitrate=128 --v_bitrate=1024 --h_res=704 --v_res=576");
  }

  function archiveLQVideo($row)
  {
    $path = $this->generateExportPath($row, "lqvideo", "avi");

    if ($this->pathExistsAndContainsFile($path))
    {
      echo "Already Archived as LQ Video.\n";
      return true;
    }
    echo "Archiving as LQ Video to path $path\n";
    return $this->runNuvExport($row, $path, "--mode 
XviD --a_bitrate=64 --v_bitrate=256 --h_res=320 --v_res=240");
  }

  function archiveAudio($row)
  {
//    return; //DEBUG
    $res = $this->archiveHQAudio($row);
    if ($res)
      $this->flagAsArchived($row);
  }

  function archiveVideo($row)
  {
    $res1 = $this->archiveLQVideo($row);
//    return; //DEBUG
    $res2 = $this->archiveHQVideo($row);
    if ($res1 && $res2)
      $this->flagAsArchived($row);
  }


  function flagAsArchived($row)
  {
     // Check if already flagged
     if (strpos("-".$row["title"], $this->archivedflag)!=false)
     {
       echo "This row already flagged as archived\n";
       return;
     }

     $sql = "update recorded set title = concat(\"$this->archivedflag\", 
title) where (chanid = ".$row["chanid"].") and (starttime = 
'".$row["starttime"]."')";
     echo $sql."\n";
     mysql_query ($sql)       or die("Error running query: 
".mysql_error()."\n");

  }

  function openDatabase()
  {
    echo "Connecting to mysql database $this->sqldatabase on $this->sqlhost 
with username $this->sqlusername\n";

    $this->chandle = mysql_pconnect($mythhost, $this->sqlusername, 
$this->sqlpassword)
      or die("Connection Failure to mysql on host ".$mythhost." with 
username ".$username."\n");

    mysql_select_db($this->sqldatabase, $this->chandle)
      or die ("Database ".$this->sqldatabase." not found.\n\n");
  }

  function closeDatabase()
  {
    mysql_close($this->chandle);
  }

  function scanRecorded()
  {
    echo "Scanning recorded table.\n";

    // Looking for any records not modified in last 10 mins
    $sql = "select * from recorded where commflagged=1";

    $result = mysql_query ($sql)
      or die("Error running query: ".mysql_error()."\n");

    while ($row = mysql_fetch_assoc($result))
    {
      $this->processRecorded($row);
    }
    mysql_free_result ($result);
  }

  function processRecorded($row)
  {
    $desc = "ChannelID: ".$row["chanid"]." Start Time: ".$row["starttime"]." 
Title: ".$row["title"];
    echo "\n*** Found recording ".$desc."\n";
    // Establish if its a audio only channel, or has video
    $audioonly = false;
    foreach ($this->radiochannels as $achannel)
      if ($achannel == $row["chanid"])
        $audioonly = true;
    if ($audioonly)
      $this->archiveAudio($row);
    else
      $this->archiveVideo($row);
  }

  function safeFilename($dirtyname)
  {
    $res = "";
    for ($i=0; $i < strlen($dirtyname); $i++)
    {
      $c = $dirtyname[$i];
      if (($c != ".") && ($c != "/") && ($c != "\\") && ($c != ":") && ($c 
!= "*") && ($c != "?") && ($c != "\"") && ($c != "<") && ($c != ">") && ($c 
!= "|")&& ($c != "'"))
        $res = $res . $c;
    }
    return $res;
  }
  function sortPathPart($in)
  {
    // Remove archived flag
    $res = str_replace(array($this->archivedflag), array(""), $in);
    // Limit length to 200 chars
    if (strlen($res) > 200)
      $res = substr($res, 0, 200);
    // Ensure safe filename
    $res = $this->safeFilename($res);
    return $res;
  }

  function generateExportPath($row, $subdir) // NB $subdir is the directory 
under exportdir
  {
    $res = $this->archivedir."/".$subdir;
    // Add in bits for subdirs
    $res .= "/" . $this->sortPathPart($row["chanid"]);
    $res .= "/" . $this->sortPathPart($row["title"]);
    $res .= "/" . $this->sortPathPart($row["starttime"]);
    $res .= "/" . $this->sortPathPart($row["description"]);

    return $res;
  }

  function Execute()
  {
    $this->openDatabase();
    $this->scanRecorded();
    $this->closeDatabase();
  } // execute

} // class


// Actually create an instance and execute it...
$theArchiver = new RipArchiver();
$theArchiver->Execute();
?>

















---------------------------------------
OLD VERSION - THIS WORKS OK WITH OLD NUVEXPORT


#!/usr/bin/php
<?php


class RipArchiver
{
  // Settings
  var $sqlhost = "localhost";
  var $sqlusername = "mythweb";
  var $sqlpassword = "mythweb";
  var $sqldatabase = "mythconverg";

  var $archivedflag = "[archived] ";

  var $radiochannels = array("1019","1043","1017");

  var $archivedir = "/dump/mythtv/archive"; // NB NO TRAILING /

  // Private
  var $chandle; // Connection handle to mysql

  // nuvexport --chanid=1019 "--starttime=20040731090000" --mode 
lamemp3 --outfile="/dump/georgeout.mp3" --a_bitrate=64
  function runNuvExport($row, $filename, $flags)
  {
    $starttime = $row["starttime"];
    // Want from 2004-08-02 20:30:00 to 20040802203000

    $starttime = str_replace(array("-"," ",":"), array("","",""), 
$starttime);


    $cmd = 
"nuvexport --chanid=".$row["chanid"]." --starttime=".$starttime." --outfile=\"".$filename."\" 
".$flags;
    echo "$cmd\n";
    system($cmd);
    // Check that output file exists - return false if not...
    $res = (file_exists($filename));
    if (! $res)
      echo "Failed to create output file $filename.\n";
    return $res;
  }

  function archiveHQAudio($row)
  {
    $filename = $this->generateExportFilename($row, "audio", "mp3");

    if (file_exists($filename))
    {
      echo "Already Archived as audio.\n";
      return true;
    }
    echo "Archiving as audio to file $filename\n";
    return  $this->runNuvExport($row, $filename, "--mode 
lamemp3 --a_bitrate=128");
  }

  function archiveHQVideo($row)
  {
    $filename = $this->generateExportFilename($row, "hqvideo", "avi");

    if (file_exists($filename))
    {
      echo "Already Archived as HQ Video.\n";
      return true;
    }
    echo "Archiving as HQ Video to file $filename\n";
    return $this->runNuvExport($row, $filename, "--mode 
divx --a_bitrate=128 --v_bitrate=1024 --h_res=704 --v_res=576");
  }

  function archiveLQVideo($row)
  {
    $filename = $this->generateExportFilename($row, "lqvideo", "avi");

    if (file_exists($filename))
    {
      echo "Already Archived as LQ Video.\n";
      return true;
    }
    echo "Archiving as LQ Video to file $filename\n";
    return $this->runNuvExport($row, $filename, "--mode 
divx --a_bitrate=64 --v_bitrate=256 --h_res=320 --v_res=240");
  }

  function archiveAudio($row)
  {
    $res = $this->archiveHQAudio($row);
    if ($res)
      $this->flagAsArchived($row);
  }

  function archiveVideo($row)
  {
    $res1 = $this->archiveLQVideo($row);
    $res2 = $this->archiveHQVideo($row);
    if ($res1 && $res2)
      $this->flagAsArchived($row);
  }


  function flagAsArchived($row)
  {
     // Check if already flagged
     if (strpos("-".$row["title"], $this->archivedflag)!=false)
     {
       echo "This row already flagged as archived\n";
       return;
     }

     $sql = "update recorded set title = concat(\"$this->archivedflag\", 
title) where (chanid = ".$row["chanid"].") and (starttime = 
'".$row["starttime"]."')";
     echo $sql."\n";
     mysql_query ($sql)       or die("Error running query: 
".mysql_error()."\n");

  }

  function openDatabase()
  {
    echo "Connecting to mysql database $this->sqldatabase on $this->sqlhost 
with username $this->sqlusername\n";

    $this->chandle = mysql_pconnect($mythhost, $this->sqlusername, 
$this->sqlpassword)
      or die("Connection Failure to mysql on host ".$mythhost." with 
username ".$username."\n");

    mysql_select_db($this->sqldatabase, $this->chandle)
      or die ("Database ".$this->sqldatabase." not found.\n\n");
  }

  function closeDatabase()
  {
    mysql_close($this->chandle);
  }

  function scanRecorded()
  {
    echo "Scanning recorded table.\n";

    $sql = "select * from recorded where commflagged <> 0";

    $result = mysql_query ($sql)
      or die("Error running query: ".mysql_error()."\n");

    while ($row = mysql_fetch_assoc($result))
    {
      $this->processRecorded($row);
    }
    mysql_free_result ($result);
  }

  function processRecorded($row)
  {
    $desc = "ChannelID: ".$row["chanid"]." Start Time: ".$row["starttime"]." 
Title: ".$row["title"];
    echo "\n*** Found recording ".$desc."\n";
    // Establish if its a audio only channel, or has video
    $audioonly = false;
    foreach ($this->radiochannels as $achannel)
      if ($achannel == $row["chanid"])
        $audioonly = true;
    if ($audioonly)
      $this->archiveAudio($row);
    else
      $this->archiveVideo($row);
  }

  function safeFilename($dirtyname)
  {
    $res = "";
    for ($i=0; $i < strlen($dirtyname); $i++)
    {
      $c = $dirtyname[$i];
      if (($c != ".") && ($c != "/") && ($c != "\\") && ($c != ":") && ($c 
!= "*") && ($c != "?") && ($c != "\"") && ($c != "<") && ($c != ">") && ($c 
!= "|")&& ($c != "'"))
        $res = $res . $c;
    }
    return $res;
  }

  function generateExportFilename($row, $subdir, $ext) // NB $subdir is the 
directory under exportdir
  {
    $res = 
$row["title"]."-".$row["chanid"]."-".$row["starttime"]."-".$row["description"];
    // Remove archived flag
    $res = str_replace(array($this->archivedflag), array(""), $res);
    // Limit length to 200 chars
    if (strlen($res) > 200)
      $res = substr($res, 0, 200);

    $res = $this->safeFilename($res); // Remove crap from filename

    // Build full dirname, which is $this->archivedir + / + $subdir
    $fulldir = $this->archivedir."/".$subdir;
    // Make dir if doesnt exist
    if (!file_exists($fulldir))
      mkdir($fulldir) or die("Failed to create directory $fulldir\n");

    $res = $fulldir . "/" . $res . "." . $ext;

    return $res;
  }

  function Execute()
  {
    $this->openDatabase();
    $this->scanRecorded();
    $this->closeDatabase();
  } // execute

} // class


// Actually create an instance and execute it...
$theArchiver = new RipArchiver();
$theArchiver->Execute();
?>


----- Original Message ----- 
From: <myth-lane at dowobeha.net>
To: "Discussion about mythtv" <mythtv-users at mythtv.org>
Sent: Thursday, September 30, 2004 7:30 PM
Subject: Re: [mythtv-users] Re: DVB card and nuvexport....


> If you have a minute, would you be willing to share those auto-archive 
> scripts?
> :)
>
> Lane
>
> Quoting George Styles <george at ripnet.co.uk>:
>
>> Thanks Chris :)
>>
>> Im glad of this, as ive just spent the entire day re-writing my 
>> auto-archive
>> scripts to work with the 'new' nuvexport tools
>>
>> g
>>
>> > CVS is now the same as the beta, which is also available at
>>
>> _______________________________________________
>> mythtv-users mailing list
>> mythtv-users at mythtv.org
>> http://mythtv.org/cgi-bin/mailman/listinfo/mythtv-users
>>
>
>
>
> _______________________________________________
> mythtv-users mailing list
> mythtv-users at mythtv.org
> http://mythtv.org/cgi-bin/mailman/listinfo/mythtv-users



More information about the mythtv-users mailing list