<?php
// tmp_ffmpeg.php
// RJM Programmingzxx
// August, 2024
  ini_set('max_execution_time', 60000);

  $smallpath='./';
  $defext=".mp4"; // ".MOV"
  $sv='ffmpeg';
  
  $cbidea=' -vf eq=brightness=0.06:saturation=2 '; //'-c:a copy ';
  $sepiaidea=' -filter_complex colorchannelmixer=.393:.769:.189:0:.349:.686:.168:0:.272:.534:.131 -pix_fmt yuv420p ';
  $gb=' -vf gblur=sigma=5 '; // '-c:a copy ';
  
  $whereplace=shell_exec("ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'");
  if (strpos(($whereplace . ' ' . $_SERVER['SERVER_ADDR']), '65.254.92.213') !== false) {
     $sv='/usr/bin/ffmpeg';
     header('Location: https://65.254.95.247/PHP/tmp_ffmpeg.php'); //$smallpath='https://65.254.95.247/PHP/'; //header('Location: https://65.254.95.247/PHP/tmp_ffmpeg.php');
     exit; //exit;
  }
  
  if (isset($_GET['delbit'])) {
     if (isset($_GET['rand'])) {
       if (file_exists(str_replace('  ',' +',str_replace('+',' ',urldecode($_GET['delbit']))))) {
         if (('' . filesize(str_replace('  ',' +',str_replace('+',' ',urldecode($_GET['delbit']))))) != ('' . $_GET['rand'])) {
           exit;
         }
       }
     }
     unlink(str_replace('  ',' +',str_replace('+',' ',urldecode($_GET['delbit']))));
     //file_put_contents('xz.xz','rm -f ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . basename(str_replace('  ',' +',str_replace('+',' ',urldecode($_GET['delbit'])))));
     if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . basename(str_replace('  ',' +',str_replace('+',' ',urldecode($_GET['delbit'])))))) {
       exec('rm -f ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . basename(str_replace('  ',' +',str_replace('+',' ',urldecode($_GET['delbit'])))));
     }
     exit;
  } else if (isset($_POST['delbit'])) {
     if (isset($_POST['rand'])) {
       if (file_exists(str_replace('  ',' +',str_replace('+',' ',urldecode($_POST['delbit']))))) {
         if (('' . filesize(str_replace('  ',' +',str_replace('+',' ',urldecode($_POST['delbit']))))) != ('' . $_POST['rand'])) {
           exit;
         }
       }
     }
     unlink(str_replace('  ',' +',str_replace('+',' ',urldecode($_POST['delbit']))));
     if (file_exists($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . basename(str_replace('  ',' +',str_replace('+',' ',urldecode($_POST['delbit'])))))) {
       exec('rm -f ' . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . basename(str_replace('  ',' +',str_replace('+',' ',urldecode($_POST['delbit'])))));
     }
     exit;
  }

  if (isset($_GET['defext'])) {
     $defext=(str_replace('  ',' +',str_replace('+',' ',urldecode($_GET['defext']))));
  } else if (isset($_POST['defext'])) {
     $defext=(str_replace('  ',' +',str_replace('+',' ',urldecode($_POST['defext']))));
  }

  $relvurl='/ffmpeg/govetts_leap' . $defext;
  $endout=' /tmp/govetts_leap_tmp_' . server_remote_addr() . '.mp4';
  $lns='; ln -s ' . $endout . ' ~rjmprogr/public_html/govetts_leap_tmp_' . server_remote_addr() . '.mp4';
  if (file_exists('/usr/local/bin/ffmpeg')) {
    $sv='/usr/local/bin/ffmpeg';
  } else if (file_exists('/usr/bin/ffmpeg')) {
    $sv='/usr/bin/ffmpeg';
  }
  $startverb=$sv . '  -i ~rjmprogr/public_html/ffmpeg/govetts_leap' . $defext . ' ';
  if (file_exists('/Applications/MAMP/htdocs/govetts_leap' . $defext)) {
    $startverb=$sv . ' -i /Applications/MAMP/htdocs/govetts_leap' . $defext . ' ';
    $relvurl='/govetts_leap' . $defext;
    $lns='; ln -s ' . $endout . ' /Applications/MAMP/htdocs/govetts_leap_tmp_' . server_remote_addr() . '.mp4';
  }
  $midbit='';
  
  function server_remote_addr() {
    global $ipadless;
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    $ipadless=str_replace(".", "_", str_replace(":", "_", $rma));
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    return str_replace(".", "_", str_replace(":", "_", $rma));
  }

  if (isset($argc)) {
     if ($argc > 1) {
          $midbit=$argv[1];
          for ($ws=2; $ws<sizeof($argv); $ws++) {
            $midbit.=' ' . $argv[$ws];
          }
     }
  } else if (isset($_GET['midbit'])) {
     $midbit=str_replace('  ',' +',str_replace('+',' ',urldecode($_GET['midbit'])));
  } else if (isset($_POST['midbit'])) {
     $midbit=str_replace('  ',' +',str_replace('+',' ',urldecode($_POST['midbit'])));
  }

  if ($midbit != '') {
    if (strpos($midbit, ' -c:a copy ') !== false) {
      $xpre=explode(' -c:a copy ', $midbit);
      $midbit=$xpre . ' -c:a copy ';
    }
    if (strpos($midbit, '/') !== false) {
      $thewords=explode(' ', $midbit);
      $midbit='';
      for ($iw=0; $iw<sizeof($thewords); $iw++) {
         if (strpos($thewords[$iw], '/') === false || strpos($thewords[$iw], '/0') !== false) { // && strpos($thewords[$iw], '/1') === false && strpos($thewords[$iw], '/2') === false && strpos($thewords[$iw], '/3') === false && strpos($thewords[$iw], '/4') === false && strpos($thewords[$iw], '/5') === false && strpos($thewords[$iw], '/6') === false && strpos($thewords[$iw], '/7') === false && strpos($thewords[$iw], '/8') === false && strpos($thewords[$iw], '/9') === false) {
           if ($midbit == '') {
             $midbit.='' . $thewords[$iw];
           } else {
             $midbit.=' ' . $thewords[$iw];
           }
         }
      }
    }
    
    if (file_exists(trim($endout))) {
      unlink(trim($endout));
    }
    //file_put_contents('xzz.xzz',$startverb . $midbit . $endout . $lns); //'<html><body onload="parent.document.getElementById(' . "'mysub'" . ').style.border=' . "'1px solid black'" . '; parent.document.getElementById(' . "'mysub'" . ').value=' . "'" . $startverb . $midbit . $endout . "'" . ';  if (1 == 1) { parent.document.getElementById(' . "'tdvid'" . ').innerHTML=' . "'" . '<video id=myvideo autoplay controls><source id=mysource type=video/mp4 src=' . "" . '/' . basename(trim($endout)) . "></source></video>'" . ';  } else { parent.document.getElementById(' . "'mysource'" . ').src=' . "'" . '/' . basename(trim($endout)) . "'" . '; } parent.document.getElementById(' . "'myvideo'" . ').play(); setTimeout(parent.dellater, 50000);"></body></html>');
    exec($startverb . $midbit . $endout . $lns);
    if (file_exists(trim($endout))) {
      //echo '<html><body onload="parent.document.getElementById(' . "'mysub'" . ').style.border=' . "'1px solid black'" . '; parent.document.getElementById(' . "'mysub'" . ').value=' . "'" . $startverb . $midbit . $endout . "'" . ';  parent.document.getElementById(' . "'mysource'" . ').src=' . "'" . 'data:video/mp4;base64,' . base64_encode(file_get_contents(trim($endout))) . "'" . '; parent.document.getElementById(' . "'myvideo'" . ').play();"></body></html>';
      //file_put_contents('xz.xz','<html><body onload="parent.document.getElementById(' . "'mysub'" . ').style.border=' . "'1px solid black'" . '; parent.document.getElementById(' . "'mysub'" . ').value=' . "'" . $startverb . $midbit . $endout . "'" . ';  if (1 == 1) { parent.document.getElementById(' . "'tdvid'" . ').innerHTML=' . "'" . '<video id=myvideo autoplay controls><source id=mysource type=video/mp4 src=' . "" . '/' . basename(trim($endout)) . "></source></video>'" . ';  } else { parent.document.getElementById(' . "'mysource'" . ').src=' . "'" . '/' . basename(trim($endout)) . "'" . '; } parent.document.getElementById(' . "'myvideo'" . ').play(); setTimeout(parent.dellater, 50000);"></body></html>');
      echo '<html><body onload="parent.document.getElementById(' . "'mysub'" . ').style.border=' . "'1px solid black'" . '; parent.document.getElementById(' . "'mysubtwo'" . ').style.border=' . "'1px solid black'" . '; parent.document.getElementById(' . "'mysub'" . ').value=' . "'" . $startverb . $midbit . $endout . "'" . ';  parent.document.getElementById(' . "'mysubtwo'" . ').value=' . "'" . $startverb . $midbit . $endout . "'" . ';  if (1 == 1) { parent.document.getElementById(' . "'tdvid'" . ').innerHTML=' . "'" . '<video style=width:95%; id=myvideo autoplay controls><source id=mysource type=video/mp4 src=' . "" . '/' . basename(trim($endout)) . "?rand=" . filesize(trim($endout)) . "></source></video>'" . ';  parent.document.getElementById(' . "'svid'" . ').innerHTML=' . "'" . '<a target=_blank href=' . "" . '/' . basename(trim($endout)) . ">Output Video</a>'" . ';  } else { parent.document.getElementById(' . "'mysource'" . ').src=' . "'" . '/' . basename(trim($endout)) . "?rand=" . filesize(trim($endout)) . "'" . '; }  setTimeout(parent.dellater, 50000); parent.document.getElementById(' . "'myvideo'" . ').play();"></body></html>';
      //sleep(600);
      //unlink(trim($endout));
      exit;
    }
    
  } else {
    echo "<html>
<head>
<title>Your Own Ffmpeg Video Changes - RJM Programming - August, 2024</title>
<style>
  th {
     padding-top: 5px;
  }

  td {
     border-bottom: 3px solid blue;
     padding-bottom: 5px;
  }
  
  input[type=submit] {
     background-color: lightgreen;
  }
  
  #tdvid {
     background-color: #f0f0f0;
  }
</style>
<scr" . "ipt type=text/javascript>
  function dellater() {
     if (document.getElementById('mysource').src.indexOf('?rand=') != -1) {
     document.getElementById('myif').src='" . $smallpath . "tmp_ffmpeg.php?delbit=" . urlencode(trim($endout)) . "&rand=' + document.getElementById('mysource').src.split('?rand=')[1]; 
     } else {
     document.getElementById('myif').src='" . $smallpath . "tmp_ffmpeg.php?delbit=" . urlencode(trim($endout)) . "'; 
     }
     //alert(document.getElementById('myif').src);
  }
</scr" . "ipt>
</head>
<body data-onload=\" document.getElementById('mytable').style.width='' + screen.width + 'px';\">
<h1>Your Own Ffmpeg Video Changes</h1>
<h3>RJM Programming - August, 2024</h3>
<form onsubmit=\" if (document.getElementById('midbit').value.length == 0) { return false;  } document.body.style.cursor='progress'; document.getElementById('mysub').style.border='5px dotted pink';  document.getElementById('mysubtwo').style.border='5px dotted pink';  document.getElementById('mysub').value='Processing from ' + ('' + new Date()) + ' ...';  document.getElementById('mysubtwo').value='Processing from ' + ('' + new Date()) + ' ...';  return true;\" target=myif method=POST action=" . $smallpath . "tmp_ffmpeg.php>
<input id=mysubtwo type=submit value=Display></input>
<table id=mytable cellspacing=5 style=width:98%;>
<tr><th>Ffmpeg command innards (any / usage words will be stripped) ... <select style=background-color:yellow; onchange=\"if (this.value.length != 0) { document.getElementById('midbit').value=this.value.replace(/\`/g, String.fromCharCode(34)).replace(/\~/g, String.fromCharCode(39));  } \"><option value=''>Optional Basis Ideas ...</option><option value=\"-c:v libx264 -preset slow -crf 18 -vf scale=out_color_matrix=bt709 -color_primaries bt709 -color_trc bt709 -colorspace bt709 -c:a aac -ar 48000 -ac 2 -b:a 320k -profile:v high -level 4.0 -bf 2 -coder 1 -pix_fmt yuv420p -b:v 10M -threads 4 -cpu-used 0 -r 30 -g 15 -movflags +faststart -vf smartblur=lr=2.00:ls=-0.90:lt=-5.0:cr=0.5:cs=1.0:ct=1.5\">Sharpened ready ...</option><option value=\"-c:v libx264 -preset slow -crf 18 -vf scale=out_color_matrix=bt709 -color_primaries bt709 -color_trc bt709 -colorspace bt709 -c:a aac -ar 48000 -ac 2 -b:a 320k -profile:v high -level 4.0 -bf 2 -coder 1 -pix_fmt yuv420p -b:v 10M -threads 4 -cpu-used 0 -r 30 -g 15 -movflags +faststart\">YouTube ready ...</option><option value='" . $cbidea . "'>Brightness and Saturation idea ...</option><option value='" . $sepiaidea . "'>Sepia idea ...</option><option value='" . $gb . "'>Gaussian Blur idea ...</option></select> ...</th></tr>
<tr><td><textarea style='width:98%;font-size:8px;background-color:yellow;' placeholder='Most of ffmpeg command should be typed here ...' data-type=text name=midbit id=midbit value='' rows=4></textarea>&nbsp;</td></tr>
<tr><th><span id=svid>Output Video</span> ... will show and maybe play if you succeed ...</th></tr>
<tr><td id=tdvid><video id=myvideo controls><source id=mysource type='video/mp4' src='" . "" . "'></source></video></td></tr>
<tr><th><details open><summary><a target=_blank href=" . $relvurl . ">Input Video</a> below ...</summary>
<video style=width:95%; controls><source type='video/mp4' src='" . $relvurl . "'></source></video></th></tr>
</table>
<br>
<input id=mysub type=submit value=Display></input>
</form>
<iframe name=myif id=myif style=display:none; src=./tmp_ffmpeg.php></iframe>
</body>
</html>";
  }
  
?>