How to get the first frame of video in H5 as cover
This article will give you a detailed introduction to the method of obtaining the first frame of video in H5 as the cover. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
1. Import the library
The code is as follows (example):
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
2.HTML part
<p class="upload-title mg-b10 title-all">上传视频(必填)</p> <p class="file-input-trigger" onclick="document.getElementById('uploadBtn').click()"> <p class="upload-icon">+</p> <p class="upload-text">上传文件</p> </p> <input type="file" class="upload-video" id="uploadBtn"> </p>
The display effect is as shown below. As for the style, I can write it myself. I am lazy, hahahaha
<video src="" onloadeddata='vSetImg(this)' autoplay> </video> <img class="video-img lazy" src="/static/imghw/default1.png" data-src="" class=" alt="">
The above part of the code is to display pictures, the effect is as follows, the pictures are displayed normally
3.JS part
let result$('#uploadBtn').on('change', function (e) { const file = e.target.files // console.log(file) let fr = new FileReader() fr.readAsDataURL(file[0]) //将文件读取为tata Url fr.onload = function (e) { result = e.target.result // 视屏上传 if (/video/g.test(file[0].type)) { $('.upload-img').show() $('.upload-img video').attr('src', result) $('.upload-file').hide() } }})function vSetImg(obj) { $(obj).removeAttr("poster"); var vimg = $("<img / alt="How to get the first frame of video in H5 as cover" >")[0]; captureImage(obj, vimg); $(obj).attr("poster", $(vimg).attr("src")); //展示获取的第一帧图片 $(".upload-img img").attr("src", $(vimg).attr("src")); $('.upload-img video').hide() $('.upload-img img').show()}function captureImage(video, output) { const scale = 0.8 try { var videocanvas = $("<canvas/>")[0]; videocanvas.width = video.videoWidth * scale; videocanvas.height = video.videoHeight * scale; videocanvas.getContext('2d').drawImage(video, 0, 0, videocanvas.width, videocanvas.height); output.src = videocanvas.toDataURL("image/png"); delete videocanvas; } catch (e) { output.src = "加载动画.gif"; }}
The attribute autoplay on the video tag is necessary, otherwise the image may not be displayed. This is what I discovered after several experiments. I used jquery for the convenience of operating elements. Of course, you can also use native ones.
Recommended learning: html video tutorial
The above is the detailed content of How to get the first frame of video in H5 as cover. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Click to enter: ChatGPT Tool Plug-in Navigation Collection Some users complained that their computers frequently crashed with the stop code VIDEO DXGKRNL FATAL ERROR. This particular issue occurs only occasionally and has a bugcheck value of 0x00000113, which indicates a violation in the Microsoft DirectX graphics kernel subsystem, as indicated by the bugcheck value. Usually, the error occurs when a corrupt driver interferes with the normal operation of the graphics card's graphics processor. If you're currently struggling with this specific issue, our article will provide you with a variety of high-quality troubleshooting tips. Below you'll find various methods that other users who encountered the exact same error have used successfully. yes

Over the past few weeks, the most important specifications and the euro prices of the Motorola Razr 50 and the Razr 50 Ultra have been leaked. Now the enormously reliable leaker @MysteryLupin was able to publish the teaser video embedded below, which

H5 refers to HTML5, the latest version of HTML. H5 is a powerful markup language that provides developers with more choices and creative space. Its emergence promotes the development of Web technology, making the interaction and effect of web pages more Excellent, as H5 technology gradually matures and becomes popular, I believe it will play an increasingly important role in the Internet world.

This article will help you quickly distinguish between H5, WEB front-end, large front-end, and WEB full stack. I hope it will be helpful to friends in need!

The Google Pixel 9 Pro and Pro XL are Google's answers to the likes of the Samsung Galaxy S24 Ultra and the Apple iPhone 15 Pro and Pro Max. Daniel Sin on YouTube(watch below) has compared the Google Pixel 9 Pro XL to the iPhone 15 Pro Max with some

Sinceitslaunchearlierthisyear,thePanasonicLumixS9hasbeenembroiledincontroversy.BetweenthecornersPanasoniccuttoarriveatsuchasmallbodyandthefusscertaincamerareviewerskickeduponYouTube,itseemedliketheLumixS9wasdoom

Despite the hype surrounding the Qualcomm Snapdragon X Elite, it has been a rather mediocre launch. In our review, we found that the most impressive part of the new Qualcomm Snapdragon X Elite X1E-78-100-powered Asus Vivobook S 15 was the seamlessnes

Detailed explanation of the video tag in HTML The video tag in HTML5 is a tag used to play videos on web pages. It can render videos using different formats, such as MP4, WebM, Ogg, and more. In this article, we will introduce the use of video tag in detail and provide specific code examples. Basic Structure The following is the basic structure of the video tag:
