php - 上传视频并使用 php 显示其缩略图预览

标签 php video file-upload video-thumbnails

我需要在特定目录中上传视频,当它上传成功时,它会显示在页面上并带有缩略图预览:

代码:

<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="500000" />  
          <table width="400" cellpadding="3" > 
            <tr> 
              <td colspan="3"> </td> 
            </tr> 
            <tr> 
              <td width="10" rowspan="2"> </td> 
              <td width="120"><strong>Choose a file to upload:</strong></td> 
              <td width="242"><input type="file" name="uploaded_file" /></td> 
            </tr> 
            <tr> 
              <td> </td> 
              <td> </td> 
            </tr> 
            <tr> 
              <td> </td> 
              <td> </td> 
              <td><input type="submit" name="sendForm" value="Upload File" /> 
                <br /></td> 
            </tr> 
            <tr> 
              <td colspan="3"> </td> 
            </tr> 
          </table> 
</form> 
<?php 

  // C:\wamp\www\scriptDir\uploads\;

  $idir = "C:/xampp/htdocs/xampp/video";   // Path To Images Directory 
  $tdir = "C:/xampp/htdocs/xampp/video";   // Path To Thumbnails Directory 
  $twidth = "200";   // Maximum Width For Thumbnail Images 
  $theight = "150";   // Maximum Height For Thumbnail Images 

  error_reporting(E_ALL ^ E_NOTICE); // Show all major errors. 

  // Check to see if the button has been pressed 
  if (!empty($_REQUEST['sendForm'])) 
  { 
    // Assign the name to a variable 
    $name = $_FILES['uploaded_file']['name']; 
    // Assign the tmp_name to a variable 
    $tmp_name = $_FILES['uploaded_file']['tmp_name']; 
    // Assign the error to a variable 
    $error = $_FILES['uploaded_file']['error']; 
    // Assign the size to a variable 
    $size = $_FILES['uploaded_file']['size'];
    // No trailing slash 
    $uploadFilesTo = 'video'; 
    // Create safe filename 
    $name = preg_replace('[^A-Za-z0-9.]', '-', $name); 
    // Disallowed file extensions 
    //what files you don't want upoad... leave this alone and you should be fine but you could add more 
    $naughtyFileExtension = array("php", "php3", "asp", "inc", "txt", "wma","js", "exe", "jsp", "map", "obj", " ", "", "html", "mp3", "mpu", "wav", "cur", "ani");  // Returns an array that includes the extension 

    //Allowable file Mime Types. Add more mime types if you want 
    $FILE_MIMES = array('video/mpg','video/avi','video/mpeg','video/wmv'); 
    //Allowable file ext. names. you may add more extension names. 
    $FILE_EXTS = array('.avi','.mpg','.mpeg','.asf','.wmv','.3gpp') ;
    $file_ext = strtolower(substr($name,strrpos($name,".")));  
    // to check the extensio
    if (!in_array($file_type, $FILE_MIMES) && !in_array($file_ext,$FILE_EXTS) ) 
    $message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";  

    $fileInfo = pathinfo($name); 
    // Check extension 
    if (!in_array($fileInfo['extension'], $naughtyFileExtension)) 
    { 
      // Get filename 
      $name = getNonExistingFilename($uploadFilesTo, $name); 
      // Upload the file 
      if (move_uploaded_file($tmp_name, $uploadFilesTo.'/'.$name))  
      { 
          // Show success message 
          echo '<center><p>Your Video File has uploaded successfully<br />'.$uploadFilesTo.'/'.$name.'</p></center>'; 
      } 
      else 
      { 
          // Show failure message 
          echo '<center><p>File failed to upload to /'.$name.'</p></center>'; 
      } 
    } 
    else 
    { 
        // Bad File type 
        echo '<center><p>The file uses an extension we don\'t allow.</p></center>'; 
    } 
  } 

  // Functions do not need to be inline with the rest of the code 
  function getNonExistingFilename($uploadFilesTo, $name) 
  { 
      if (!file_exists($uploadFilesTo . '/' . $name)) 
          return $name; 

      return getNonExistingFilename($uploadFilesTo, rand(100, 200) . '_' . $name); 
  } 
?>

我已经尝试使用此代码上传视频并生成特定视频的缩略图,但它仅给出“文件无法上传到”,如果有人有想法这样做,建议一些解决方案来执行此任务。

最佳答案

这里是创建视频缩略图的简单方法

你必须指定路径,拇指将保存为 .jpg 格式

$videos_dir = 'path/to/videos';
$videos_dir = opendir($videos_dir);
$output_dir = 'path/to/output/dir/';
while (false !== ($file = readdir($videos_dir))) {
    if ($file != '.' && $file != '..'){
        $in = $videos_dir.'/'.$file;
        $out = $output_dir.$file.'.jpg';
        exec("/usr/local/bin/ffmpeg -itsoffset -105 -i ".$in." -vcodec mjpeg -vframes 1 -an -f rawvideo -s 100x100 ".$out);
    }
}

已更新

<script>
    var vidDOM = $('article').children('video');
    var vid = vidDOM.get(2);
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');

    vidDOM.bind({
        'paused':function () {
            vid.width = canvas.width = vid.offsetWidth;
            vid.height = canvas.height = vid.offsetHeight;
            var $this = this; 
            ctx.drawImage($this, 0, 0, vid.width, vid.height);
            uploadbase64();
        }
    })

    function uploadbase64(){
        canvasData = canvas.toDataURL("image/jpeg"); 
        var ajax = new XMLHttpRequest();
        ajax.open("POST",'ImageUpload.php?filename='+vidDOM.id,false);
        ajax.setRequestHeader('Content-Type', 'application/upload');
        ajax.send(canvasData);
    }
</script>



<?php
    $filename =$_GET['filename'];

    if (file_get_contents('php://input')){
        // Remove the headers (data:,) part.
        $filteredData=substr(file_get_contents('php://input'), strpos(file_get_contents('php://input'), ",")+1);

        // Need to decode before saving since the data we received is already base64 encoded
        $decodedData=base64_decode($filteredData);

        //create the file
        if($fp = fopen( $filename, 'wb' )){
            fwrite( $fp, $decodedData);
            fclose( $fp );
        } else {
            echo "Could not create file.";
        }
}

echo "Created image ".$filename;

?>

关于php - 上传视频并使用 php 显示其缩略图预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27777955/

相关文章:

javascript - HTML 5视频或音频播放列表

c# - 使用 ASP.net C# 读取文本文件和操作数据

由于权限 : Ubuntu/Linux,PHP 脚本可能会阻止上传文件

PHP 检查用户是否存在 PDO

c++ - 如何跳过前两帧?

video - 如何在 YouTube 视频标题前添加关键字?

javascript - Facebook Graph API - 使用 JavaScript 上传照片

php - Json 在我的列表框中获取 "Undefined"值

php - 无法修改 Wordpress 中的标题信息错误

php - Join 函数用于将一个表中的字段值与另一表中的字段值进行匹配