
video
base
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| const video = document.createElement("video");
video.src = "video.mp4";
document.body.appendChild(video);
video.play();
video.pause();
video.currentTime = 30;
console.log(video.duration);
console.log(video.paused);
console.log(video.ended);
video.controls = true;
video.muted = true;
|
在JavaScript中,可以通过DOM元素的addEventListener方法来监听loadedmetadata事件。例如,如果要监听视频元素的loadedmetadata事件,可以这样写:
1 2 3 4 5 6
| const video = document.getElementById('myVideo');
video.addEventListener('loadedmetadata', function() { });
|
在loadedmetadata事件的处理函数中,您可以访问到视频元素的metadata属性,以获取有关视频的元数据信息,例如持续时间、视频宽高等。
1 2 3 4 5 6
| video.addEventListener('loadedmetadata', function() { console.log('Duration: ' + video.duration + ' seconds'); console.log('Width: ' + video.videoWidth + ' pixels'); console.log('Height: ' + video.videoHeight + ' pixels'); });
|
当浏览器加载完视频的元数据后,就会触发loadedmetadata事件,并执行相应的处理函数。这样,您就能够在JavaScript中处理有关视频元数据的逻辑。
play
在video元素加载视频文件时,会依次触发 “loadedmetadata” 和 “play” 事件。
1 2
| <video id="videoElement" src="path/to/video.mp4" controls></video> <canvas id="canvasElement"></canvas>
|
获取视频画面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
const video = document.getElementById("videoElement"); const canvas = document.getElementById("canvasElement"); const ctx = canvas.getContext("2d");
video.addEventListener("loadedmetadata", () => { canvas.width = video.videoWidth; canvas.height = video.videoHeight; });
video.addEventListener("play", () => { function capturePreview() { if (video.paused || video.ended) { return; } ctx.drawImage(video, 0, 0, canvas.width, canvas.height); requestAnimationFrame(capturePreview); } capturePreview(); });
|