1

Hello have successfully built a web application that allows users upload videos, but i am trying to compress videos before uploading them so as to save bandwidth. Please how do I go about these? e.g, In pictures compression it is GD library but in video searched post on Youtube and stack overflow, none answers my question these is my code. NOTE:these code works perfectly but I am trying to compress the video before upload in php. Thanks in advance..

I looked at this post Server-side video conversion and compression not really what i am looking for because i have never really worked with ffmpeg

               <?php include "connect.php"; ?>

 <?php 
if ((isset($_POST['videoname'])) && (isset($_POST['aboutvideo'])) && (isset($_POST['timestart'])) && (isset($_POST['timestop'])) && (isset($_POST['streamersid']))){

$videoname=$_POST['videoname']; 
$aboutvideo=$_POST['aboutvideo'];
$timestart=$_POST['timestart'];
$timestop=$_POST['timestop'];
$streamersid=$_POST['streamersid'];
$streamerstype=$_POST['streamerstype'];
$streamersname=$_POST['streamersname'];
$date=$_POST['date'];

 $fileName = $_FILES["file1"]["name"]; //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

$tex = pathinfo($fileName, PATHINFO_EXTENSION); //get video extension

if($tex=='mp4' || $tex=='avi' ||
$tex=='webm' || $tex=='flv' ||
$tex=='MP4' || $tex=='3gp')
{

$rand = substr(md5(microtime()),rand(0, 26) , 26);

@$filez = $rand.$_FILES['file1']['name'];


$videoname= mysqli_real_escape_string($con, $videoname);
$aboutvideo= mysqli_real_escape_string($con, $aboutvideo);
$timestart = mysqli_real_escape_string($con, $timestart);
$timestop = mysqli_real_escape_string($con, $timestop);


//compression script from here video would be compressed before path being being saved to database and moved to folder

if(move_uploaded_file($fileTmpLoc, "plays/$filez")){    
$insert="INSERT INTO `plays`(`streamers_id`, `file1`, `video_name`, `about_video`, `streamers_type`, `time_start`, `time_stop`, `date`, `streamers_name`) VALUES ('$streamersid','$filez','$videoname','$aboutvideo','$streamerstype','$timestart','$timestop','$date','$streamersname')";
$run=mysqli_query($con, $insert);
echo "Upload complete ";
} else {
     echo "Upload Failed";
}

} else {
echo "Invalid video";   
}   
}
?>
Community
  • 1
  • 1
chiefo
  • 281
  • 1
  • 5
  • 15
  • 2
    Possible duplicate of [Server-side video convertion and compression](http://stackoverflow.com/questions/26591690/server-side-video-convertion-and-compression) – Jeremy Harris Nov 17 '16 at 16:46
  • no it is not mine is a simple snippet i am looking for @JeremyHarris – chiefo Nov 17 '16 at 17:26
  • 3
    There isn't a "simple snippet" when it comes to video encoding. If you want do do that sort of thing with PHP, you will have to get your hands dirty and learn ffmpeg – Jeremy Harris Nov 17 '16 at 17:35

1 Answers1

3

You are looking to compress BEFORE upload, then it must be done using code running on the client. Your PHP runs on the server. Therefore, you must write some sort of app that the user can download that can compress and upload.

szatmary
  • 29,969
  • 8
  • 44
  • 57
  • You can wrap ffmpeg in an electron app that will compress the clips and upload them. I've implemented a similar project successfully. You just need to make sure you include the appropriately compiled binaries with each distribution. – UltrasoundJelly Nov 18 '16 at 14:05