0

here is my problem : On my website I want to allow users to upload any type of video. But with the HTML5 tag only .mp4 video can be used. So I want to convert any type of video submit by the user to MP4 then add the path to the databse.

I've read something about FFmpeg but I can't figure out how to use it. I tried to use shell_exec("ffmpeg -i ".$vid." -vcodec libx264 -crf 20 out.mp4 2>&1") without success.

The html

<form method="post" enctype="multipart/form-data" name="form" action="post.php">
   
                     
 <input type="file" name="media-vid"  class=" file_multi_video" accept="video/*">
              
</form>

The php script:

if(file_exists($_FILES['media-vid']['tmp_name']) && is_uploaded_file($_FILES['media-vid']['tmp_name']))
 {
  
  
  
  $targetvid = md5(time());
  $target_dirvid = "videos/";
  $target_filevid =  $targetvid.basename($_FILES["media-vid"]["name"]);
  $uploadOk = 1;
  $videotype = pathinfo($target_filevid,PATHINFO_EXTENSION);
 
  
   
  if ($_FILES["media-vid"]["size"] > 500000000) {
  $uploadOk = 0;
  echo "Sorry, your file is too large.";
  }
 
 // Check if $uploadOk is set to 0 by an error
 if ($uploadOk == 0) {
  echo "Sorry, your video was not uploaded."; 
 // if everything is ok, try to upload file
  } else {
  
   $target_filevid = strtr($target_filevid, 
     'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 
     'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
   $target_filevid = preg_replace('/([^.a-z0-9]+)/i', '_', $target_filevid);
    
  if (!move_uploaded_file($_FILES["media-vid"]["tmp_name"], $target_dirvid. $target_filevid)) {
   
   echo "Sorry, there was an error uploading your file. Please retry.";
  }else{
   
   $vid= $target_dirvid.$target_filevid;
 
   shell_exec("ffmpeg -i ".$vid." -vcodec libx264 -crf 20  out.mp4 2>&1");

  
   }
  }
 }
 
 
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Dilak
  • 105
  • 2
  • 2
  • 13

1 Answers1

7

Here is how I would do it:

Try out this code! ( Tested and works fine )

<form method="post" enctype="multipart/form-data" name="form">
   
                     
<input type="file" name="media-vid"  class=" file_multi_video" accept="video/*">
  
<input type="submit" name="submit" value="upload"/> 

</form>
        <?

    if (isset($_POST['submit'])) {

        if (file_exists($_FILES['media-vid']['tmp_name']) && is_uploaded_file($_FILES['media-vid']['tmp_name'])) {

            $targetvid     = md5(time());
            $target_dirvid = "videos/";

            $target_filevid = $targetvid . basename($_FILES["media-vid"]["name"]);

            $uploadOk = 0;

            $videotype = pathinfo($target_filevid, PATHINFO_EXTENSION);

    //these are the valid video formats that can be uploaded and
                  //they will all be converted to .mp4

            $video_formats = array(
                "mpeg",
                "mp4",
                "mov",
                "wav",
                "avi",
                "dat",
                "flv",
                "3gp"
            );

            foreach ($video_formats as $valid_video_format) {

      //You can use in_array and it is better

                if (preg_match("/$videotype/i", $valid_video_format)) {
                    $target_filevid = $targetvid . basename($_FILES["media-vid"] . ".mp4");
                    $uploadOk       = 1;
                    break;

                } else {
              //if it is an image or another file format it is not accepted
                    $format_error = "Invalid Video Format!";
                }

            }

            if ($_FILES["media-vid"]["size"] > 500000000) {
                $uploadOk = 0;
                echo "Sorry, your file is too large.";
            }

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0 && isset($format_error)) {

                echo $message;

                // if everything is ok, try to upload file

            } else if ($uploadOk == 0) {


                echo "Sorry, your video was not uploaded.";

            }

            else {

                $target_filevid = strtr($target_filevid, 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
                $target_filevid = preg_replace('/([^.a-z0-9]+)/i', '_', $target_filevid);

                if (!move_uploaded_file($_FILES["media-vid"]["tmp_name"], $target_dirvid . $target_filevid)) {

                    echo "Sorry, there was an error uploading your file. Please retry.";
                } else {

                    $vid = $target_dirvid . $target_filevid;

                }
            }
        }

    }

    ?>

Test it and let me know how it goes. If you have any other questions or need anything else, please do not hesitate to ask. I am always here to help and I would like you to have this file fully coded. Good luck bro!

Mr Pro Pop
  • 666
  • 5
  • 19
  • I have another question here : https://stackoverflow.com/q/45773272/8186242 Can you help me please ? Sorry to disturb you – Dilak Aug 19 '17 at 19:20
  • @Dilak Have you managed to save the video path into a MySQL database table? If not let me know so I can help you with storing and retrieving videos from and to MySQL database table! – Mr Pro Pop Aug 20 '17 at 01:32
  • Yes I did it buddy! Thank you -) You helped me a lot! – Dilak Aug 20 '17 at 12:41
  • I have a problem again. The video that I convert using this method can't be played with the video tag. The video is actually converted and I can play it with VLC per example but on my website it is not working. Can you help me please? – Dilak Sep 05 '17 at 02:12
  • Can you help me ? @MrProPop – Dilak Sep 05 '17 at 22:17
  • Nice! Could you explain what `$target_filevid = strtr($target_filevid, 'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');` is the purpose of this? – JGuarate May 15 '20 at 17:33
  • @JGuarate It replaces special characters with normal characters. – Tom Feb 04 '21 at 01:01