如何在输入类型的文件上传上创建视频缩略图并将其保存到mysql

时间:2019-02-21 19:33:41

标签: javascript php

我使用表单输入类型文件上传多个视频,我想自动创建缩略图并保存到mysql中,而没有任何FFMPEG或其他类似GETID3的搜索,并且我找到了能够创建缩略图但如何将其上传到mysql的JavaScript代码

1:这是我可以随时捕获的非常好的代码,但是如何上传到mysql服务器

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=no">
<style type="text/css">
body {margin: 0;}

#video-demo-container {
    width: 400px;
    margin: 40px auto;
}   
#main-video {
    display: none;
    max-width: 400px;
}
#thumbnail-container {
    display: none;
}    
#get-thumbnail {
    display: none;
}    
#video-canvas {
    display: none;
}    
#upload-button {
    width: 150px;
    display: block;
    margin: 20px auto;
}

#file-to-upload {
    display: none;
}

</style>
</head>

<body>

<div id="video-demo-container">
    <button id="upload-button">Select MP4 Video</button> 
    <input type="file" id="file-to-upload" accept="video/mp4" />
    <video id="main-video" controls>
        <source type="video/mp4">
    </video>
    <canvas id="video-canvas"></canvas>
    <div id="thumbnail-container">
         Seek to <select id="set-video-seconds"></select> seconds <a id="get-thumbnail" href="#">Download Thumbnail</a>
    </div>
</div>   

<script>    
var _CANVAS = document.querySelector("#video-canvas"),
    _CTX = _CANVAS.getContext("2d"),
    _VIDEO = document.querySelector("#main-video");   
// Upon click this should should trigger click on the #file-to-upload file input element
// This is better than showing the not-good-looking file input element
document.querySelector("#upload-button").addEventListener('click', function() {
    document.querySelector("#file-to-upload").click();
});    
// When user chooses a MP4 file
document.querySelector("#file-to-upload").addEventListener('change', function() {
    // Validate whether MP4
    if(['video/mp4'].indexOf(document.querySelector("#file-to-upload").files[0].type) == -1) {
        alert('Error : Only MP4 format allowed');
        return;
    }

    // Hide upload button
    document.querySelector("#upload-button").style.display = 'none';

    // Object Url as the video source
    document.querySelector("#main-video source").setAttribute('src', URL.createObjectURL(document.querySelector("#file-to-upload").files[0]));

    // Load the video and show it
    _VIDEO.load();
    _VIDEO.style.display = 'inline';

    // Load metadata of the video to get video duration and dimensions
    _VIDEO.addEventListener('loadedmetadata', function() { console.log(_VIDEO.duration);
        var video_duration = _VIDEO.duration,
            duration_options_html = '';

        // Set options in dropdown at 4 second interval
        for(var i=0; i<Math.floor(video_duration); i=i+4) {
            duration_options_html += '<option value="' + i + '">' + i + '</option>';
        }
        document.querySelector("#set-video-seconds").innerHTML = duration_options_html;

        // Show the dropdown container
        document.querySelector("#thumbnail-container").style.display = 'block';

        // Set canvas dimensions same as video dimensions
        _CANVAS.width = _VIDEO.videoWidth/2;     // half the width for video thumbnail image
        _CANVAS.height = _VIDEO.videoHeight/2;  // half the height for video thumbnail image
    });
});

// On changing the duration dropdown, seek the video to that duration
document.querySelector("#set-video-seconds").addEventListener('change', function() {
    _VIDEO.currentTime = document.querySelector("#set-video-seconds").value;

    // Seeking might take a few milliseconds, so disable the dropdown and hide download link 
    document.querySelector("#set-video-seconds").disabled = true;
    document.querySelector("#get-thumbnail").style.display = 'none';
});

// Seeking video to the specified duration is complete 
document.querySelector("#main-video").addEventListener('timeupdate', function() {
    // Re-enable the dropdown and show the Download link
    document.querySelector("#set-video-seconds").disabled = false;
    document.querySelector("#get-thumbnail").style.display = 'inline';
});

// On clicking the Download button set the video in the canvas and download the base-64 encoded image data
document.querySelector("#get-thumbnail").addEventListener('click', function() {



    _CTX.drawImage(_VIDEO, 0, 0, _CANVAS.width, _CANVAS.height);

    document.querySelector("#get-thumbnail").setAttribute('href', _CANVAS.toDataURL());
    document.querySelector("#get-thumbnail").setAttribute('download', 'thumbnail.png');
});

</script>

</body>
</html>

2:视频上传后,它会自动创建Thumbnai ...

<input type="file" accept=".jpg,.jpeg.,.gif,.png,.mov,.mp4" />  
<div></div> 
<style>
div {
  line-height: 200px;
}

img {
  max-width: 200px;
  max-height: 200px;
  padding: 5px;
  vertical-align: middle;
  text-align: center;
}

@supports (object-fit: cover) {
  img {
    width: 200px;
    height: 200px;
    object-fit: cover;
  }
}
</style>


<script>
document.getElementsByTagName('input')[0].addEventListener('change', function(event) {
  var file = event.target.files[0];
  var fileReader = new FileReader();
  if (file.type.match('image')) {
    fileReader.onload = function() {
      var img = document.createElement('img');
      img.src = fileReader.result;
      document.getElementsByTagName('div')[0].appendChild(img);
    };
    fileReader.readAsDataURL(file);
  } else {
    fileReader.onload = function() {
      var blob = new Blob([fileReader.result], {type: file.type});
      var url = URL.createObjectURL(blob);
      var video = document.createElement('video');
      var timeupdate = function() {
        if (snapImage()) {
          video.removeEventListener('timeupdate', timeupdate);
          video.pause();
        }
      };
      video.addEventListener('loadeddata', function() {
        if (snapImage()) {
          video.removeEventListener('timeupdate', timeupdate);
        }
      });
      var snapImage = function() {
        var canvas = document.createElement('canvas');
        canvas.width = video.videoWidth/2;    // half the width for video thumbnail image
        canvas.height = video.videoHeight/2;    // half the height for video thumbnail image
        canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
        var image = canvas.toDataURL();
        var success = image.length > 100000;
        if (success) {
          var img = document.createElement('img');
          img.src = image;   
          document.getElementsByTagName('div')[0].appendChild(img);
          URL.revokeObjectURL(url);
        }
        return success;
      };
      video.addEventListener('timeupdate', timeupdate);
      video.preload = 'metadata';
      video.src = url;
      // Load video in Safari / IE11
      video.muted = true;
      video.playsInline = true;
      video.play();
    };
    fileReader.readAsArrayBuffer(file);
  }
});
</script>

所以,我只希望输入类型文件将视频信息上传到我的视频中,我想将视频信息保存到mysql,将视频保存到视频目录, 并保存到视频/缩略图和缩略图信息到mysql,我只希望使用纯php,javascript,请减少上述脚本中的多余javascript代码或您的想法。

0 个答案:

没有答案