<?php
// voiceover.php
// RJM Programming
// May, 2023


$rs='';
$outf='';
if (isset($_GET['fcommand'])) {
   if (PHP_OS == "Darwin") {
    putenv('PATH=/usr/local/bin');
   }
   //file_put_contents('x.x', str_replace('+',' ',urldecode($_GET['fcommand'])));
   $cmdis=str_replace('+',' ',urldecode($_GET['fcommand']));
   $precmd=explode('>', $cmdis);
   $subcmd=explode('-shortest ', trim($precmd[0]));
   if (sizeof($subcmd) > 1) {
      $outf=$subcmd[1];
      if (file_exists($outf)) { rename($outf, $outf . '_was'); }
   }
   $rs=shell_exec($cmdis);
   if ($outf != '') {
     if (file_exists($outf)) {
       $rs.='<br><br><video controls><source src="' . $outf . '" type="video/' . strtolower(explode('.', $outf)[-1 + sizeof(explode('.', $outf))]) . '"></source></video>';  
     }
   }
} else if (PHP_OS == "Darwin") {
   $rs='<details><summary>Files ...</summary>' . str_replace("\n","<br>",shell_exec('file *.m*')) . '</details>';
}


if (isset($_GET['infilegetsize'])) {
   exit;
}

if (isset($_GET['ffmpeg'])) {
  $fnd=false;
  if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
    if (file_exists('./ffmpeg.txt')) {
      $pplace=file_get_contents('./ffmpeg.txt');
      if (strpos($pplace, 'ffmpeg.exe') !== false) {
        $fnd=true;
      }
    }
    if (!$fnd) {
     $huhexe=shell_exec("where ffmpeg.exe");
     if ($huhexe == '') {
      $huhexes=rglob("C:\\ffmpeg.exe"); //shell_exec("where pdfimages.exe");
      if (sizeof($huhexes) > 0) {
       file_put_contents('./ffmpeg.txt', $huhexes[0]);
      }
     }
    }
  }
  exit;
}

$dn='';
$ffmpegpre='';
$ffmpegsuf='';
$whereffmpeg="?infilegetsize=";
if (PHP_OS == "Darwin") {
  $dn=' /dev/null';
} else if (PHP_OS =='WINNT' || PHP_OS =='WIN32' || PHP_OS =='Windows') {
  $ffmpegpre="\"C:\\Program Files\\ImageMagick-7.1.0-Q16-HDRI\\";
  $ffmpegsuf=".exe\"";
  if (!file_exists($ffmpegpre . 'ffmpeg' . $ffmpegsuf)) {
   if (file_exists('./ffmpeg.txt')) {
     if (!file_exists(file_get_contents('./ffmpeg.txt'))) {
       unlink('./ffmpeg.txt');
       $whereffmpeg="?ffmpeg=where";
     } else {
       $ffmpegpre="\"" . str_replace('ffmpeg.exe','',file_get_contents('./ffmpeg.txt')) . "\"";
     }
   }
   if (file_exists('ffmpeg' . $ffmpegsuf)) {
    $ffmpegpre="";
   } else if (!file_exists('./ffmpeg.txt')) {
     $huhexe=shell_exec("where ffmpeg.exe");
     if (strpos($huhexe, 'ffmpeg.exe') === false) {
       $whereffmpeg="?ffmpeg=where";
     } else {
       $ffmpegpre=str_replace('ffmpeg.exe','',$huhexe);
     }
   }
  }
  $pdfimagespre="\"C:\\MAMP\\htdocs\\xpdf-tools-win-4.04\\bin32\\";
  $pdfimagessuf=".exe\"";
  if (!file_exists($pdfimagespre . 'pdfimages' . $pdfimagessuf)) {
   if (file_exists('./pdfimages.txt')) {
     if (!file_exists(file_get_contents('./pdfimages.txt'))) {
       unlink('./pdfimages.txt');
       $wherepdfimages="?pdfimages=where";
     } else {
       $pdfimagespre="\"" . str_replace('pdfimages.exe','',file_get_contents('./pdfimages.txt')) . "\"";
     }
   }
   if (file_exists('pdfimages' . $pdfimagessuf)) {
     $pdfimagespre="";
   } else if (!file_exists('./pdfimages.txt')) {
     $huhexe=shell_exec("where pdfimages.exe");
     if (strpos($huhexe, 'pdfimages.exe') === false) {
       $wherepdfimages="?pdfimages=where";
     } else {
       $pdfimagespre=str_replace('pdfimages.exe','',$huhexe);
     }
   }
  }
}

if ($ffmpegpre != '' && strpos($ffmpegpre, ' ') === false) {
  $ffmpegsuf=str_replace('"','',$ffmpegsuf);
  $ffmpegpre=str_replace('"','',$ffmpegpre);
}

echo "<html>
<body onload=\" if (document.URL.indexOf('rjmprogramming.com.au/') != -1) {   if (('' + window.navigator.platform).toLowerCase().indexOf('mac') == 0) { window.open('HTTP://localhost:8888/voiceover.php','_blank','top=50,left=50,width=' + eval(0.8 * screen.width) + ',height=600'); } else {  window.open('HTTP://localhost/voiceover.php','_blank','top=50,left=50,width=' + eval(0.8 * screen.width) + ',height=600');  }  }\">
<h1>Add Voiceover Audio to Video via ffmpeg</h1>
<h3>RJM Programming</h3>
<h4>May, 2023</h4>
<div>" . $rs . "</div><br>
<form method=GET action=./voiceover.php>
<textarea id=fcommand name=fcommand value='' style='width:98%;height:100px;'>
" . $ffmpegpre . "ffmpeg" . $ffmpegsuf . " -i inv.mp4 -i inva.mp4 -c copy -map 0:v:0 -map 1:a:0 -shortest out.mp4 > voiceover.bad
</textarea><br><br>
<input type=submit value='Create New Video with Audio Voiceover' style='background-color:lightgreen;'></input>
</form>
</body>
</html>";

?>