【问题标题】:How to append HTML5 video snapshots to a scrolling DIV without Jquery如何在没有 Jquery 的情况下将 HTML5 视频快照附加到滚动 DIV
【发布时间】:2015-11-17 06:23:24
【问题描述】:

如何从本地播放的 HTML5 视频中获取多个快照(通过 onclick 按钮),并将带有时间戳的快照图像添加到没有 jQuery 或外部库的可滚动 div 中?

我可以通过画布制作快照,但如何将画布图像转换为小文件大小格式并将图像(带有 href=""scrpt)附加到滚动 div?

codepine 示例http://codepen.io/anon/pen/OVGZEg

HTML

<span> Working example</span>
<br>


<video controls id="sourceVid">
    <source src="http://html5multimedia.com/code/ch9/media/elephants-dream-medium.mp4" type="video/mp4">
    <source src="../media/elephants-dream-medium.webm" type="video/webm">
</video>
<canvas></canvas>
<br>
    <button id="snap" onclick="snap()">Take Snapshot</button>
<br>
<br>
    <br>
<!----------------------------------------
Desired Result
---------------------------------------->
<div id="DesiredResult" style="background-color:grey;width: 100%;">
<br>
Desired Result
<br>
    How can I take a snapshot & add it to scrolling DIV on the left?
    <br>
<video controls id="Result" style="float: left;" >
        <source src="http://html5multimedia.com/code/ch9/media/elephants-dream-medium.mp4" type="video/mp4">
        <source src="../media/elephants-dream-medium.webm" type="video/webm">
    </video>
<div id="snapshotscroll" >

<div id="snapshot#1" class="snapshots" style="background-color:#FE2E2E;" >
    snapshot image
    <br>#1
    </div>
    <div id="snapshot#1" class="snapshots" style="background-color:#FA5858;" >
    snapshot image
    <br>#2
    </div>
    <div id="snapshot#1" class="snapshots" style="background-color:#F78181;" >
    snapshot image
    <br>#3   
    </div>
    <div id="snapshot#1" class="snapshots" style="background-color:#F5A9A9;" >
    snapshot image
    <br>#4   
    </div>
    <div id="snapshot#1" class="snapshots" style="background-color:#F6CECE;" >
    snapshot image
    <br>#5   
    </div>
</div>
</div>
        <button id="snap" onclick="alert('How can I take a snapshot & add it to scrolling DIV on the left?')">Take Snapshot</button>


CSS

video, canvas {
    border:1px solid #000;
}
#snapshotscroll {
overflow-y: scroll;
height:258px;
width:220px;
}
.snapshots {
width: 200px;
height: 100px;
border: 1px solid #000;
}

JAVASCRIPT



        // Get handles on the video and canvas elements
        var video = document.getElementById('sourceVid');
        var canvas = document.querySelector('canvas');
        // Get a handle on the 2d context of the canvas element
        var context = canvas.getContext('2d');
        // Define some vars required later
        var w, h, ratio;

        // Add a listener to wait for the 'loadedmetadata' state so the video's dimensions can be read
        video.addEventListener('loadedmetadata', function() {
            // Calculate the ratio of the video's width to height
            ratio = video.videoWidth / video.videoHeight;
            // Define the required width as 100 pixels smaller than the actual video's width
            w = 200;
            // Calculate the height based on the video's width and the ratio
            h = 100;
            // Set the canvas width and height to the values just calculated
            canvas.width = w;
            canvas.height = h;          
        }, false);

        // Takes a snapshot of the video
        function snap() {
            // Define the size of the rectangle that will be filled (basically the entire element)
            context.fillRect(0, 0, w, h);
            // Grab the image from the video
            context.drawImage(video, 0, 0, w, h);
        }

【问题讨论】:

  • 我认为这个问题应该简单地改写为“如何拍摄 HTML5 视频的快照”,并切断另一部分。
  • 我认为这个问题应该叫做“如何将图像附加到DIV”,因为他的快照部分已经工作了哈哈。

标签: javascript html5-canvas html5-video


【解决方案1】:

我最终让您的示例进行了一些调整。此示例使用计数器来跟踪要将快照附加到哪个 div。

实时工作示例(全屏):

var w = 200, h = 100, snapnum = 1;

// Takes a snapshot of the video
function snap() {
    var cv = document.createElement("canvas");
    cv.width = w;
    cv.height = h;
    console.log(document.getElementById("snapshot#" + snapnum));
    document.getElementById("snapshot#" + snapnum).textContent = "";
    document.getElementById("snapshot#" + snapnum).appendChild(cv);
    var cx = cv.getContext('2d');
    cx.fillRect(0, 0, w, h);
    // Grab the image from the video
    cx.drawImage(document.getElementById('Result'), 0, 0, w, h);
    snapnum++;
}
video, canvas {
    border:1px solid #000;
}
#snapshotscroll {
    overflow-y: scroll;
    height:258px;
    width:220px;
}
.snapshots {
    width: 200px;
    height: 100px;
    border: 1px solid #000;
}
<!---------------------------------------- Desired Result ---------------------------------------->
<div id="DesiredResult" style="background-color:grey;width: 100%;">
    <br>Desired Result
    <br>
    <video controls id="Result" style="float: left;">
        <source src="http://html5multimedia.com/code/ch9/media/elephants-dream-medium.mp4" type="video/mp4">
            <source src="../media/elephants-dream-medium.webm" type="video/webm">
    </video>
    <div id="snapshotscroll">
        <div id="snapshot#1" class="snapshots" style="background-color:#FE2E2E;">snapshot image
            <br>#1</div>
        <div id="snapshot#2" class="snapshots" style="background-color:#FA5858;">snapshot image
            <br>#2</div>
        <div id="snapshot#3" class="snapshots" style="background-color:#F78181;">snapshot image
            <br>#3</div>
        <div id="snapshot#4" class="snapshots" style="background-color:#F5A9A9;">snapshot image
            <br>#4</div>
        <div id="snapshot#5" class="snapshots" style="background-color:#F6CECE;">snapshot image
            <br>#5</div>
    </div>
</div>
<button id="snap" onclick="snap()">Take Snapshot</button>

JSFiddle 版本:https://jsfiddle.net/y80m3z33/

【讨论】:

  • @PixelPimp 很高兴它对你有用!请不要忘记通过单击左侧的灰色复选标记将此答案标记为已接受,使其变为绿色。谢谢!
  • 感谢 Maximillian Laumeister,你真棒!我很惊讶终于看到它工作了。我删除了几行代码以获得我正在寻找的结果JSFiddle Version link 我还有一些事情要弄清楚......如何将单个链接附加到快照?我如何在不将 CSS 中的字体大小更改为 0 的情况下消除快照之间的空格,因为我最终想在它们上面覆盖时间戳和此类文本?感谢您的帮助
  • @PixelPimp 要将单独的链接附加到每个快照,我会将每个快照 div 包装在带有 href 的 标记中。为了摆脱快照之间的空间,给画布显示:块。希望有帮助!
猜你喜欢
  • 2013-12-11
  • 2022-10-07
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 2015-10-13
  • 2018-01-14
相关资源
最近更新 更多