<?php

include('myUtility.php');
$count = 0;
for ($c = $argv[1]; $c <= $argv[2]; $c++) {
  $start = $c * 1000;
  $end = ($c+1) * 1000;

  echo "Check from $start to $end\n";
  $dbh = new PDO('mysql:host=115.84.178.137;dbname=tvtl', 'tvtl2007', 'tVtL2007remote', array( PDO::ATTR_PERSISTENT => false));
  $stmt = $dbh->prepare(
    "select doc_id, doc_category, doc_filename from tbldocument ".
    "where doc_type in ('avi','mpg','mpeg','asf','wmv','mov','3gp','dat','flv') ".
    "and doc_status<2 and doc_id>=$start and doc_id<$end");
  $stmt->execute();

  while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
    list($doc_id, $doc_cat, $doc_file) = array_values($row);
    $thumb = "/data/violet/uploads/thumbnails/$doc_cat/thumbnails/$doc_file.jpg";
    if (file_exists($thumb) && filesize($thumb)<1000) {
      $file = "/srv/www/tvtl/web/uploads/resources/$doc_cat/$doc_file";
      if (!file_exists($file) || filesize($file)==0) continue;
      $info = GetVideoInfo($file);
      $time = doubleval($info['ID_LENGTH'])/2;
      $cmd = "ffmpeg -y -v 0 -itsoffset -$time -i $file -s 133x100 -f mjpeg -vframes 1 $thumb";
      echo "$doc_id $cmd\n";
      error_log(date("H:i:s ")."$doc_id $cmd\n", 3, "command.log");
      exec($cmd);
      $count++;
    }
  }
}
echo "Finish convert $count files\n\n";