2017-08-19 10 views
-1

ここは私の問題です。私のウェブサイトでは、どのタイプのビデオをアップロードしてもかまいません。 HTML5タグでは、.mp4ビデオしか使用できません。 だから私は、ユーザーがMP4に提出するビデオの任意のタイプを変換し、そのパスをデータベースに追加したいと思う。ビデオをMP4に変換する

私はFFmpegについて何かを読んだことがありますが、私はそれを使用する方法を理解できません。私はshell_exec( "ffmpeg -i"。$ vid。 "-vcodec libx264 -crf 20 out.mp4 2> &")を使ってみました。

HTML

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

PHPスクリプト:

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

 
\t \t 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 
\t 
 
\t

+0

'shell_exec'の結果を見ることはできません。あなたはそれを試してみませんか? – mario

+0

実際に何もしていません。私はそれを正しく使用していますか? – Dilak

+0

再度:関数の結果を出力します。 – mario

答えて

2

ここでは、私はそれを行うだろうかです:

このコードを試してみてください! (テストおよび正常に動作します)

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

 
</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; 

       } 
      } 
     } 

    } 

    ?> 

テストそれを、私はそれが行く方法を知ってみましょう。他にご質問がありましたら、お気軽にお問い合わせください。私はいつも助けに来ており、このファイルを完全にコード化していただきたいと思います。幸運の仲間!

+0

ありがとうございました!それは私が必要とするものです – Dilak

+0

私はここで別の質問があります:https://stackoverflow.com/q/45773272/8186242私を助けてくれますか?あなたを邪魔して申し訳ありません – Dilak

+0

@Dilak大丈夫、確かに仲間。それを見てみましょう! –

関連する問題