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

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Privacy International News Feed
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
NISL@THU
NISL@THU
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
博客园 - Franky
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
The Hacker News
The Hacker News
K
Kaspersky official blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tailwind CSS Blog
Project Zero
Project Zero
T
Tor Project blog
B
Blog RSS Feed
Recorded Future
Recorded Future
Scott Helme
Scott Helme
美团技术团队
V
V2EX
V
Visual Studio Blog
L
Lohrmann on Cybersecurity
P
Proofpoint News Feed
D
DataBreaches.Net
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
L
LangChain Blog
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
P
Privacy & Cybersecurity Law Blog

WishMeLz - Vue

Electron 主进程起一个可用的 HTTPS 静态服务器 - WishMeLz van-nav-bar 页面宽度改动 placeholder占位元素未自动更新 - WishMeLz Vant UI van-search 键盘没有显示「搜索」按钮的解决方案 - WishMeLz Lodop、C-Lodop使用笔记 - WishMeLz el-upload beforeUpload 使用async无法拦截 ANSI 字体在前端展示 Jenkins日志 - WishMeLz elementui 动态生成的 el-form-item rules校验失效 vue中v-html - WishMeLz SSE(Server-Sent Events) - WishMeLz
获取视频某一秒的截图 - WishMeLz
Wish · 2024-07-04 · via WishMeLz - Vue
        
        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);
                },
            }
        );