惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
有赞技术团队
有赞技术团队
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
雷峰网
雷峰网
T
Tor Project blog
博客园_首页
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
J
Java Code Geeks
AWS News Blog
AWS News Blog
Security Latest
Security Latest
Spread Privacy
Spread Privacy
T
Threatpost
博客园 - 三生石上(FineUI控件)
I
Intezer
G
Google Developers Blog
Scott Helme
Scott Helme
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Hacker News
The Hacker News
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
NISL@THU
NISL@THU
A
Arctic Wolf
F
Full Disclosure
P
Proofpoint News Feed
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threat Research - Cisco Blogs
B
Blog
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
Know Your Adversary
Know Your Adversary

WishMeLz - JS

Electron 主进程起一个可用的 HTTPS 静态服务器 - WishMeLz 某x面板每月自动重置流量功能 - WishMeLz ANSI 字体在前端展示 Jenkins日志 - WishMeLz 金额大写 - WishMeLz markdown 编辑器 md编辑器 - WishMeLz 零宽非连接符 [ZWNJ] U+200C - WishMeLz SSE(Server-Sent Events) - WishMeLz 随机渐变背景 - WishMeLz 文件处理 - WishMeLz
获取视频某一秒的截图 - WishMeLz
Wish · 2024-07-04 · via WishMeLz - JS
        
        const handleGetVideoThumb = async function (url, options = {}) {
            if (typeof url !== "string") {
                console.error("URL must be a string");
                return;
            }

            // 默认参数
            const defaults = {
                seekTime: 1,
                onLoading: () => {},
                onLoaded: () => {},
                onFinish: (thumbData) => {},
                onError: (error) => console.error(error),
            };
            const params = { ...defaults, ...options };

            // 基于视频元素绘制缩略图,而非解码视频
            const video = document.createElement("video");
            // 静音
            video.muted = true;

            // 绘制缩略图的 canvas 画布元素
            const canvas = document.createElement("canvas");
            const context = canvas.getContext("2d", {
                willReadFrequently: true,
            });

            // 绘制缩略图的标志量
            let isTimeUpdated = false;

            // 设置视频源并返回 blob URL
            const setVideoSource = async (url) => {
                if (/^blob:|base64,/i.test(url)) {
                    return url;
                } else {
                    try {
                        const response = await fetch(url);
                        const blob = await response.blob();
                        params.onLoaded();
                        return URL.createObjectURL(blob);
                    } catch (error) {
                        params.onError(error);
                    }
                }
            };

            // 获取视频尺寸并开始绘制
            const onLoadedMetadata = () => {
                canvas.width = video.videoWidth;
                canvas.height = video.videoHeight;
                draw();
            };

            // 触发绘制监控
            const onTimeUpdate = () => {
                isTimeUpdated = true;
            };

            // 绘制方法
            const draw = () => {
                const thumbData = [];
                video.currentTime = params.seekTime;

                const onSeeked = () => {
                    if (isTimeUpdated) {
                        context.clearRect(0, 0, canvas.width, canvas.height);
                        context.drawImage(video, 0, 0, canvas.width, canvas.height);
                        canvas.toBlob((blob) => {
                            thumbData.push(URL.createObjectURL(blob));
                            params.onFinish(thumbData);
                        }, "image/jpeg");
                        video.removeEventListener("seeked", onSeeked);
                    }
                };

                video.addEventListener("seeked", onSeeked);
            };

            // 设置事件监听
            video.addEventListener("loadedmetadata", onLoadedMetadata);
            video.addEventListener("timeupdate", onTimeUpdate);

            // 获取视频数据
            params.onLoading();
            video.src = await setVideoSource(url);
        };

        // 示例调用
        handleGetVideoThumb(
            "https://xxxxx.mp4",
            {
                seekTime: 7, // 截取第7s视频
                onFinish: (data) => {
                    console.log(data); // 图片数据
                },
                onError: (error) => {
                    console.error("Error occurred:", error);
                },
            }
        );