Showing posts with label convert. Show all posts
Showing posts with label convert. Show all posts

Upload dan Convert Video PHP

Setelah menginstal ffmpeg pada windows dan mengetahui cara menggunakan sintaknya.
Baca : menggunakan-ffmpeg-untuk-konversi-video
           install-ffmpeg-di-windows

Sekarang bagaimana menggunakannya dengan PHP?
Contoh Dibawah ini adalah untuk meng upload dan sekaligus konversi video dengan PHP dan ffmpeg.

1. Buat file index.php
<!DOCTYPE HTML>
<html>
 <head>
  <title></title>
  <link href="css/style.css" rel='stylesheet' type='text/css' />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!----webfonts---->
  <link href='css/font.css' rel='stylesheet' type='text/css'>
  <!----//webfonts---->
    <link rel="stylesheet" href="css/main.css">

<link rel="stylesheet" type="text/css" media="all" href="css/switchery.min.css">
  <!-- //Global CSS for the page and tiles -->
<!---start-click-drop-down-menu----->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type='text/javascript' src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
  
  
  
        <!----start-dropdown--->
         <script type="text/javascript">
   
   function _(el){
    return document.getElementById(el);
   }
   //videoupload
   function uploadFile(){
    var file = _("file1").files[0];
    // alert(file.name+" | "+file.size+" | "+file.type);
    var formdata = new FormData();
    formdata.append("file1", file);
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "file_upload_parser.php");
    ajax.send(formdata);
   }
   
   function progressHandler(event){
    _("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
    var percent = (event.loaded / event.total) * 100;
    _("progressBar").value = Math.round(percent);
    _("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
   }
   function completeHandler(event){
    _("status").innerHTML = event.target.responseText;
    _("progressBar").value = 0;
   }
   function errorHandler(event){
    _("status").innerHTML = "Upload Failed";
   }
   function abortHandler(event){
    _("status").innerHTML = "Upload Aborted";
   }
   
     
   $(function() {
    $("video").osmplayer({
      width: '100%',
      height: '400px'
    });
   });
   
   
  </script>
        <!----//End-dropdown--->
  <!---//End-click-drop-down-menu----->
 </head>
<?php    
echo"
 <body>
 UPLOAD VIDEO
 <div id='container'>
  <!---start-wrap---->
  <div class='content'>
   <div class='wrap'>
    <div class='register'>
     <div class='uploadvideo'>
      <label>Upload Video 
        <input type='file' name='file1' id='file1'><br>
        <input type='button' value='UPLOAD' onclick='uploadFile()'>
        <h3 id='status'></h3>
        <p id='loaded_n_total'></p>
        <progress id='progressBar' class='progressbarkosong' value='0' max='100'></progress>
      </label>
     </div>
     
   </div>
   </div>
  </div>";
 echo"
 <!----start-footer--->
  <div class='footer'>
  <div class='wrap'>
   <p align='right'>&copy; Elaiscom 2015</p>
  </div>
  </div>
  <!----//End-footer--->
  <!---//End-wrap---->
 </div>
 </body>";
?>
</html>

2. Buat file file_upload_parser.php
<?php
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
$allowedMimeTypes = array('video/avi','video/mp4','video/mpeg','video/quicktime','video/x-msvideo','video/msvideo','video/x-ms-wmv');

$FileName2   = strtolower($_FILES['file1']['name']);
$ImageExt   = substr($FileName2, strrpos($FileName2, '.'));
$extension    = explode(".", $FileName2);
$date = date("dmY");
$NewFileName2 = preg_replace(array('/s/', '/.[.]+/', '/[^w_.-]/'), array('_', '.', ''), strtolower($date));
$NewFileName = $NewFileName2.$FileName2;

$NewFileName3 = $NewFileName2.$extension[0];
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
}
if (in_array($_FILES["file1"]["type"],$allowedMimeTypes )){
 if(move_uploaded_file($fileTmpLoc, "images/asli/$NewFileName")){ 
   
  exec('C:ffmpegbinffmpeg -i images/asli/"'.$NewFileName.'" -ar 22050 -ab 32 -f flv -s 320x240 images/video/"'.$NewFileName3.'".flv');
  exec('C:ffmpegbinffmpeg -i images/asli/"'.$NewFileName.'" -an -ss 00:00:03 -t 00:00:01 -r 1 -y -s 320x240 images/cover/"'.$NewFileName3.'".gif');
  
  echo "$fileName upload is complete <input type='hidden' name='fileVideo' value='$NewFileName3.mp4'>
   <input type='hidden' name='coverVideo' value='$NewFileName3.gif'>";
 } else {
  echo "move_uploaded_file function failed";
 }
}else{
 echo"File Not Video/File Not Support";
}
?>

3. Buat folder images yang didalamnya terdapat folder video, cover, dan asli.

Simpan keduanya dan jalankan melalui webserver yang anda gunakan. Catatan ffmpeg harus sudah terinstall.
NB: Jika hanya kedua file tersebut yang dijalankan. maka tampilan akan kacau karena tidak ada file css nya. untuk lengkap projectnya bisa anda download DISINI

Semoga Bermanfaat..

[Read more]