Related learning recommendations: javascript video tutorial
JavaScript Intercept the first frame of the video
1. Background
In the development of corporate materials, in addition to video uploading, you also need to use the first frame or perhaps several frames in the video as the video Cover display.
Problems:
Therefore, the difficulty of intercepting the first frame of the video with JS was born. However, after checking the information, I found that there are only two types of information available on the Internet. The first one iswasm ffmpeg Cooperate with the backend to intercept, the second is to intercept by JS yourself. The advantages and disadvantages are also obvious. The first one has a relatively high cost of cooperation and is not very flexible. The second one can be used under normal conditions, but there will be compatibility issues (see you later in IE) and black screen interception issues.
2. Wasm ffmpeg
The advantages and disadvantages of this method are also obvious. The cooperation cost is relatively high, and it will cause a sharp increase in web memory. However, the number of captured frames for the supported video types is Very flexible; since it involves the server, you can refer to wasm ffmpeg to capture videos.
3. JavaScript front-end interception
If you want to intercept the front-end here, you need to understand the compatibility and response events of the video and canvas tags. And it may not be so friendly to IE.
1. I won’t add too much knowledge about canvas here. Let’s mainly look at the response events of the video tag:

Execution results

According to the sequence, the first thing to be triggered turned out to be the timeupdate event. According to assumptions, the first thing to be executed should beloadedmetadata, the metadata is loaded. Regarding this, there is no clear explanation on MDN, but you can reason about it:
When currentTime is updated, the timeupdate event
# will be triggered.
##Conclusion: Although it is triggered first, the video file has not been loaded yet, and the content of the canvas itself is intercepted. Note: The timeupdate event is triggered 4-66 times per second depending on the system used. Due to the high trigger frequency, too small unit (millisecond level), event response delay and other reasons, it cannot be completely and accurately controlled.
- loadedmetadata As mentioned above, it is triggered after the metadata is loaded, but the data does not include the video file itself. Conclusion: If the video file is large and the loading time is long, the first frame that has been loaded still cannot be captured. Supplement: The URL.createObjectURL() method can basically make it invisible, but it is not safe.
- loadeddata Triggered when the current frame number (the first frame) is loaded, no problem. Conclusion: Available. Supplement: What if the first frame is a black screen and you want to use the next frame? Sorry, the number of remaining frames and the fact that they have not been loaded are not within the scope of its consideration. This event is ignored.
- canplay is triggered when the video can start playing, that is, it is triggered after the number of frames to be loaded is determined based on the number of uploaded video frames (24/25/30/60, etc.) and the playback screen is satisfied. Summary: Because there are more loading events than loadeddata (one more event), it is generally feasible. Supplement: It can be satisfied by controlling currentTime (but it cannot be as accurate as the second frame), which can be regarded as the "current playback frame".
- play It will only be triggered when playback starts, which does not meet the requirement of uploading quick screenshots.
- waiting Triggered when the next picture has been played but the next picture has not been buffered. It is suitable for inserting small advertisements.


3. Is the first frame valid?
In fact, sometimes the first frame of the captured picture is not very consistent with expectations due to poor video quality or other factors. There will always be pure black pictures, transparent pictures, white pictures and other invalid pictures. Therefore, we need to identify the validity of the image. So, how to identify the effectiveness of images?
At this time, you need to know a new attribute: Uint8ClampedArray
Uint8ClampedArray (8-bit unsigned integer fixed array ) A typed array represents an array of 8-bit unsigned integers whose values are fixed in the range 0-255; if you specify a value outside the range [0,255], it will be replaced with 0 or 255; if you specify a non-integer, then it will be set to the nearest integer. (array) contents are initialized to 0. Once created, you can reference the elements of the array using object methods, or using standard array indexing syntax (that is, using square brackets).
If you are interested in Uint8ClampedArray, you can further study Uint8ClampedArray asynchronously here.
Did you discover anything? Is 0~255 a common value? It is the hexadecimal value corresponding to the color. Okay, then, the next step is to implement what we have thought about and see if it is such a principle.
The code is implemented, so what about the results? Here I use white pictures, transparent pictures, and black pictures to compare. Is the result obtained consistent with what we imagined: First, let’s take a look at the transparent image:

As you can see, all the results in the array are 0;
White image:

Oops, all are 255, then black should be all 0, don’t worry, let’s take a look
Black picture:

There is an unexpected number, 238, which is a color value that is biased toward 255 white. Why is this? In fact, it is because The white and transparent colors are not excessive, but the black is excessive. This problem will occur when the canvas is drawn, but it can be ignored.
After knowing the color values of these three, the next judgment will be easier. Just add a condition.

Why are it 200 and 0? In fact, you can judge the reasonable range of these two values based on the actual situation. The corresponding color value of 200 is #c8c8c8, which is gray, and 0 is transparent color, so it is judged to be an invalid image here. As long as there is no value in the brr array, it means it is an invalid picture.
So what is the actual situation? Here is another actual comparison picture:

As you can see, there are values in brr, and there are a lot of them, so this is the actual picture Valid image.
4. Finally
JavaScript has finished capturing the first frame of the video. If you still want to optimize for invalid images, just use the default image display.
4. Summary
JavaScript intercepts the first frame of the video. The process is complicated and involves a large amount of data loops, which will cause a certain amount of memory growth, but it can indeed solve this problem. , and has been used in enterprise materials. A clever optimization method is used. Only if a value in the brr array is pushed, it will break directly, which can greatly optimize performance. If you have better solutions, please share them with us!
If you want to learn more about programming, please pay attention to the php training column!
The above is the detailed content of Learn how to capture the first frame of a video using javascript. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
