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

推荐订阅源

V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Latest news
Latest news
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
B
Blog
T
Threat Research - Cisco Blogs
罗磊的独立博客
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
P
Palo Alto Networks Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
T
Tor Project blog
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
Recorded Future
Recorded Future
D
DataBreaches.Net
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
B
Blog RSS Feed
Scott Helme
Scott Helme
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
A
Arctic Wolf
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
N
Netflix TechBlog - Medium
L
LangChain Blog
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
M
MIT News - Artificial intelligence
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
W
WeLiveSecurity

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);
                },
            }
        );