title: PHP convert any video to MP4 using ffmpeg
body: <p>I have a website in wich I allow users to upload videos. But with the HTML5 tag video, only MP4 videos are allowed</p>
  
  <p>So, I want to convert any type of videos that the users upload to MP4 and then add the path in my database.</p>
  
  <p>I tried something, changing the file extension to MP4 but it didn't work. 
  I've read something about ffmepg but I can't figure out how to use it.</p>
  
  <p>Here is my PHP script where I change the file extension and then add the path in my data base, please how can I convert the video correctly, what should I add/change?</p>
  
  <pre><code>&lt;?php 
      if(file_exists($_FILES['media-vid']['tmp_name']) &amp;&amp; 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);
  
          $video_formats = array(
              "mpeg",
              "mp4",
              "mov",
              "wav",
              "avi",
              "dat",
              "flv",
              "3gp"
          );
          foreach ($video_formats as $valid_video_format)
          {
              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"] &gt; 5000000000000)
          {
              $uploadOk = 0;
              echo "Sorry, your file is too large.";
          }
  
          // Check if $uploadOk is set to 0 by an error
          if ($uploadOk == 0 &amp;&amp; isset($format_error))
          {
              echo "Sorry, your video was not uploaded.";
              // 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;
                  $nbvid = 1;
              }
          }
      }
  ?&gt;
  </code></pre>
  
  <p>Thank you.</p>