如何获得和存储视频文件的缩略图没有ffmpeg


how to get and store thumbnail of video file without ffmpeg?

我是PHP初学者,我不知道如何从PHP视频文件中获得缩略图。我没有找到具体的解决办法。我尝试了ffmpeg库。但现在我想没有ffmpeg缩略图。所以请帮帮我。这是我的编码器代码。请审查。

<?php
$extension = pathinfo($_FILES["url"]["name"], PATHINFO_EXTENSION);/* Get file extension */
$filename = time() . '_' . mt_rand() . '.' . $extension;
move_uploaded_file($_FILES['url']['tmp_name'], $destinationPath . $filename);
$params['filename'] =$destinationPath.$filename;
$params['thumbImageName'] =$destinationPath.$thumbImageName;

        $this->load->library('resize',$params);
        $this->resize->resizeImage(WEIGHT, HEIGHT);
        $this->resize->saveImage($destinationPath.$thumbImageName, 100);
?>  

其实是有办法的。

上传方法:用户选择要上传的视频。选择之后,在弹出的div中显示一个加载消息,或者只使用一个警告框。然后,你要做的就是将视频文件加载到视频标签中。最好显示视频标签:无。待视频标签处于"就绪"状态后,跳到视频的50%。然后,获取视频标签的屏幕截图。你可以获取base64格式的图像,然后通过Ajax将其与视频一起发送到PHP。

包含base64脚本的输入标签。

<html>
<form action="" method="post" enctype="multipart/form-data" id="uploadvidform" accept="video/*">
    <input type="file" name="file" id="file" />
    <input type="text" name="screenshotbase64" id="screenshotbase64" value="" style="display: none;" />
</form>
<input type="button" value="Upload" id="uploadvidbut" />

和不可见的video元素。

<video width="400" id="videoelem" style="display: none;" controls>
    <source src="" id="video_src">
    Your browser does not support HTML5 video.
</video>

和脚本。首先,在文件被选中时执行一个操作。确保在head元素中链接了Google Ajax文件。

<script>
var scalefac = 0.25; 
// Scale of image;
var screenshots = []; 
// An array for multiple screenshots;
// This function will create an image. It's not used now, it's used in the below action (when you change the file).
function capture(video, scalefac) {
    if(scaleFactor == null){
        scaleFactor = 1;
    }
    var w = video.videoWidth * scalefac;
    var h = video.videoHeight * scalefac;
    var canvas = document.createElement('canvas');
        canvas.width  = w;
        canvas.height = h;
    var ctx = canvas.getContext('2d');
        ctx.drawImage(video, 0, 0, w, h);
    return canvas;
} 
$(document).ready(function(){
$(document).on("change", "#file", function(){
    alert("Please wait while we verify your video. This will only take a couple of seconds.");
    // The next 3 lines will load the video
    var lasource = $('#video_src');
    lasource[0].src = URL.createObjectURL($('#file').prop("files")[0]);
    lasource.parent()[0].load();
    var video = document.getElementById("videoelem");
    setTimeout(function(){ 
        // Video needs to load then we check the state.
        if (video.readyState == "4"){
            var videoduration = $("#videoelem").get(0).duration;
            var timetogoto = videodurationinseconds / 2;
            $("#videoelem").get(0).currentTime = timetogoto;
            setTimeout(function(){
                // Video needs to load again
                var video  = document.getElementById("videoelem");
                // function the screen grab.
                var canvas = capture(video, scalefac);
                screenshots.unshift(canvas);
                for(var i=0; i<4; i++){
                    $("#screenshotbase64").val(screenshots[i].toDataURL());
                } 
            }, 500); 
    }, 3000);      
});
// Now that the form is filled, you can send your data to your PHP file.
$(document).on('click', '#uploadvidbut', function(){
    var form = new FormData($("#uploadvidform")[0]); 
    $.ajax({
        url: '/uploadvideodocument.php', // PHP file
        type: 'POST', 
        data: form,                
        cache: false,
        contentType: false,
        processData: false,  
        success: function (result){
            if (result == 1){
                alert("The video has been uploaded.");
            }
        }
    }).fail(function(){
        alert("Oh no, the video wasn't uploaded.");
    });
});
});
</script>

现在是PHP文件。我只打算包括base64转换为图像,我希望你知道如何做剩下的。

<?php
    $data = $_POST['screenshotbase64'];
    list($type, $data) = explode(';', $data);
    list(, $data) = explode(',', $data);
    $data = base64_decode($data);
    // The following 2 lines will create the time in microseconds which you can use as the name. Microseconds ensures an almost impossibility of two people uploading at the same time.
    $mt = explode(' ', microtime());
    $millies = ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
    $screenshotfilename = time(). $millies . '.png';
    // In the next line, replace YOUR DIRECTORY with your home path and then include the folder of where you want to save the screenshot.
    file_put_contents('YOUR DIRECTORY AND FOLDER' . $screenshotfilename, $data);
    // Now, the screen shot has been saved to the server and the name of the file is $screenshotfilename.
?>

注意:

有些浏览器可能不接受video元素。如今,这种情况几乎不会发生。但是一定要记住。