{"id":11103,"date":"2024-01-15T23:55:26","date_gmt":"2024-01-15T15:55:26","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11103"},"modified":"2024-01-16T00:00:30","modified_gmt":"2024-01-15T16:00:30","slug":"js-get-video-thumb-poster","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2024\/01\/js-get-video-thumb-poster\/","title":{"rendered":"\u4f7f\u7528JS\u5feb\u901f\u83b7\u53d6video\u89c6\u9891\u4efb\u610f\u4f4d\u7f6e\u7684\u7f29\u7565\u56fe"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11103\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11103<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202401-video-thumb.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202401\/video-thumb-cover.png\" width=\"422\" height=\"264\" alt=\"\u5c01\u9762\u56fe\uff0c\u5760\u843d\u7684\u5929\u4f7f\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u5148\u770b\u6548\u679c<\/h3>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202401\/js-get-mp4-video-image-demo.php\" rel=\"noopener\" target=\"_blank\">JS\u6bcf\u9694\u4e00\u79d2\u63d0\u53d6\u89c6\u9891\u7f29\u7565\u56fedemo<\/a><\/p>\n<p>\u4f8b\u5982\uff0c\u9009\u62e9\u672c\u5730\u89c6\u9891\u540e\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202401\/2024-01-15_232201.png\" width=\"562\" height=\"492\" alt=\"\u9009\u62e9\u672c\u5730\u89c6\u9891\u7f29\u7565\u56fe\u751f\u6210\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u8f93\u5165\u5728\u7ebf\u7684\u89c6\u9891\u6587\u4ef6\u5730\u5740\u540e\uff0c\u6ce8\u610f\uff0c\u8be5\u5730\u5740\u8981\u5141\u8bb8\u8de8\u57df\uff0c\u4e5f\u53ef\u4ee5\u770b\u5230\u7f29\u7565\u56fe\u51fa\u73b0\u4e86\uff0c\u524d\u540e\u4e5f\u5c31\u51e0\u79d2\u949f\u7684\u65f6\u95f4\uff0c\u8fd8\u662f\u633a\u5feb\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202401\/2024-01-15_233020.png\" width=\"614\" height=\"372\" alt=\"\u5728\u7ebf\u5730\u5740\u7f29\u7565\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e8c\u3001\u5982\u4f55\u5b9e\u73b0\uff1f<\/h3>\n<p>\u65b9\u6cd5\u4e00\uff0c\u5bf9\u89c6\u9891\u5e27\u8fdb\u884c\u5b8c\u5168\u89e3\u7801\uff0c\u7136\u540e\u8fdb\u884c\u63d0\u53d6\uff0c\u76f8\u5173\u5b9e\u73b0\u53ef\u4ee5\u53c2\u8003<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2023\/11\/mp4box-js-webcodecs-mp4-canvas\/\">\u201cmp4box.js\u52a0WebCodecs \u89e3\u7801MP4\u89c6\u9891\u5e27\u5e76\u6e32\u67d3<\/a>\u201d\u4e00\u6587\u3002<\/p>\n<p>\u4e0d\u8fc7\u6b64\u65b9\u6cd5\u5bf9\u8d44\u6e90\u5360\u7528\u8f83\u5927\uff0c\u800c\u7f29\u7565\u56fe\u4ec5\u4ec5\u662f\u89c6\u9891\u7684\u90e8\u5206\u5e27\u4fe1\u606f\uff0c\u6709\u4e9b\u9ad8\u5c04\u70ae\u6253\u868a\u5b50\uff0c\u4e0d\u5212\u7b97\u3002<\/p>\n<p>\u65b9\u6cd5\u4e8c\uff0c\u5229\u7528video\u5143\u7d20\u8fdb\u884c\u7ed8\u5236\u3002<\/p>\n<p>\u5177\u4f53\u63cf\u8ff0\u4e3a\uff1a<\/p>\n<p>\u5e0c\u671b\u7ed8\u5236\u54ea\u4e00\u5e27\u89c6\u9891\uff0c\u5c31\u8ba9\u89c6\u9891\u8df3\u5230\u8fd9\u4e00\u5e27\u7684\u753b\u9762\uff0c\u7136\u540e\u4f7f\u7528canvas\u76f4\u63a5\u7ed8\u5236\u8be5video\u5143\u7d20\u3002<\/p>\n<h4>\u5b9e\u73b0\u7ec6\u8282<\/h4>\n<p>\u4e0d\u8fc7\u5177\u4f53\u5b9e\u73b0\u7684\u65f6\u5019\u8fd8\u662f\u6709\u4e0d\u5c11\u7ec6\u8282\u9700\u8981\u6ce8\u610f\u7684\u3002<\/p>\n<ol>\n<li>\u89c6\u9891\u7684src\u5730\u5740\u5fc5\u987b\u662f\u672c\u5730\u5730\u5740\uff0c\u800c\u4e0d\u80fd\u662f\u5728\u7ebf\u5730\u5740\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u6709\u7ed8\u5236\u5f02\u5e38\u7684\u95ee\u9898\uff0c\u4f8b\u5982\uff0c\u4f60\u8981\u7ed8\u5236\u6700\u540e1\u79d2\u7684\u7f29\u7565\u56fe\uff0c\u5728\u7ebf\u89c6\u9891\u5f80\u5f80\u5f53\u524d\u753b\u9762\u8d44\u6e90\u662f\u672a\u52a0\u8f7d\u7684\uff0c\u4f60\u518d\u53bb\u7ed8\u5236\uff0c\u5c31\u4f1a\u6709\u5f88\u957f\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u753b\u9762\u5c31\u53ef\u80fd\u662f\u9ed1\u8272\u7684\u3002<\/li>\n<li>video.currentTime = xxx\u53ef\u4ee5\u8ba9\u89c6\u9891\u5b9a\u4f4d\u5230\u5177\u4f53\u7684\u65f6\u95f4\uff0c\u4f46\u662f\u6b64\u8fc7\u7a0b\u5e76\u4e0d\u662f\u5373\u65f6\u7684\uff0c\u9700\u8981\u4f7f\u7528 timeupdate \u4e8b\u4ef6\u8fdb\u884c\u76d1\u63a7\uff0c\u4f8b\u5982\uff1a\n<pre>video.addEventListener('timeupdate', () => {\r\n   \/\/ \u7ed8\u5236\u5f53\u524dvideo\r\n});\r\nvideo.currentTime = 1;<\/pre>\n<\/li>\n<\/ol>\n<h4>\u5b8c\u6574\u4ee3\u7801<\/h4>\n<p>\u4e3a\u4e86\u65b9\u4fbf\u5927\u5bb6\u7684\u4f7f\u7528\u4e0e\u5b66\u4e60\uff0c\u5bf9\u4e8e\u7f29\u7565\u56fe\u7684\u63d0\u53d6\uff0c\u6211\u76f4\u63a5\u5f04\u4e86\u4e2a\u65b9\u6cd5\uff0c\u4ee51\u79d2\u4e3a\u95f4\u9694\uff0c\u6279\u91cf\u8fd4\u56de\u89c6\u9891\u7684\u7f29\u7565\u56fe\uff0c\u5927\u5bb6\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\uff0c\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>const handleGetVideoThumb = function (url, options = {}) {\r\n    if (typeof url != 'string') {\r\n        return;    \r\n    }\r\n    \/\/ \u9ed8\u8ba4\u53c2\u6570\r\n    const defaults = {\r\n        onLoading: () =&gt; {},\r\n        onLoaded: () =&gt; {},\r\n        onFinish: (arr) =&gt; {}\r\n    };\r\n    \r\n    const params = Object.assign({}, defaults, options);\r\n    \r\n    \/\/ \u57fa\u4e8e\u89c6\u9891\u5143\u7d20\u7ed8\u5236\u7f29\u7565\u56fe\uff0c\u800c\u975e\u89e3\u7801\u89c6\u9891\r\n    const video = document.createElement('video');\r\n    \/\/ \u9759\u97f3\r\n    video.muted = true;\r\n    \r\n    \/\/ \u7ed8\u5236\u7f29\u7565\u56fe\u7684canvas\u753b\u5e03\u5143\u7d20\r\n    const canvas = document.createElement('canvas');\r\n    const context = canvas.getContext('2d', {\r\n        willReadFrequently: true\r\n    });\r\n    \r\n    \/\/ \u7ed8\u5236\u7f29\u7565\u56fe\u7684\u6807\u5fd7\u91cf\r\n    let isTimeUpdated = false;\r\n    \/\/ \u51e0\u4e2a\u89c6\u9891\u4e8b\u4ef6\r\n    \/\/ 1. \u83b7\u53d6\u89c6\u9891\u5c3a\u5bf8\r\n    video.addEventListener('loadedmetadata', () =&gt; {\r\n        canvas.width = video.videoWidth;\r\n        canvas.height = video.videoHeight;\r\n        \r\n        \/\/ \u5f00\u59cb\u6267\u884c\u7ed8\u5236\r\n        draw();\r\n    });\r\n    \/\/ 2. \u89e6\u53d1\u7ed8\u5236\u76d1\u63a7\r\n    video.addEventListener('timeupdate', () =&gt; {\r\n        isTimeUpdated = true;\r\n    });\r\n    \r\n    \/\/ \u83b7\u53d6\u89c6\u9891\u6570\u636e\r\n    params.onLoading();\r\n    \/\/ \u8bf7\u6c42\u89c6\u9891\u5730\u5740\uff0c\u5982\u679c\u662f\u672c\u5730\u6587\u4ef6\uff0c\u76f4\u63a5\u6267\u884c\r\n    if (\/^blob:|base64,\/i.test(url)) {\r\n        video.src = url;    \r\n    } else {\r\n        fetch(url).then(res =&gt; res.blob()).then(blob =&gt; {\r\n            params.onLoaded();\r\n            \/\/ \u8d4b\u4e88\u89c6\u9891\r\n            video.src = URL.createObjectURL(blob);\r\n        });\r\n    }\r\n    \r\n    \/\/ \u7ed8\u5236\u65b9\u6cd5\r\n    const draw = () =&gt; {\r\n        const arrThumb = [];\r\n        const duration = video.duration;\r\n        let seekTime = 0.1;\r\n\r\n        const loop = () =&gt; {\r\n            if (isTimeUpdated) {                \r\n                context.clearRect(0, 0, canvas.width, canvas.height);\r\n                context.drawImage(video, 0, 0, canvas.width, canvas.height);\r\n\r\n                canvas.toBlob(blob =&gt; {\r\n                    arrThumb.push(URL.createObjectURL(blob));\r\n\r\n                    seekTime += 1;\r\n\r\n                    if (seekTime &gt; duration) {\r\n                        params.onFinish(arrThumb);\r\n\r\n                        return;\r\n                    }\r\n\r\n                    step();\r\n                }, 'image\/jpeg');\r\n\r\n                return;\r\n            }\r\n            \/\/ \u76d1\u63a7\u72b6\u6001\r\n            requestAnimationFrame(loop);\r\n        }\r\n        \r\n        \/\/ \u9010\u6b65\u7ed8\u5236\uff0c\u56e0\u4e3acurrentTime\u4fee\u6539\u751f\u6548\u662f\u5f02\u6b65\u7684\r\n        const step = () =&gt; {\r\n            isTimeUpdated = false;\r\n            video.currentTime = seekTime;\r\n            \r\n            loop();\r\n        }\r\n\r\n        step();\r\n    }\r\n};<\/pre>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre>handleGetVideoThumb(url, {\r\n  onLoading: () =&gt; {},\r\n  onLoaded: () =&gt; {},\r\n  onFinish: (arr) =&gt; {}\r\n})<\/pre>\n<p>\u5176\u4e2d\uff0c\u6700\u4e3b\u8981\u7684\u53ef\u9009\u53c2\u6570\u5c31\u662f onFinish \u65b9\u6cd5\uff0c\u5176\u652f\u6301\u4e00\u4e2a\u53c2\u6570\uff0c\u4e3a\u6570\u7ec4\uff0c\u91cc\u9762\u5c31\u662f1:1\u548c\u539f\u59cb\u89c6\u9891\u5c3a\u5bf8\u4e00\u6837\u7684\u7f29\u7565\u56fe\u7684 blob \u5730\u5740\uff0c\u4ee5\u79d2\u4e3a\u95f4\u9694\u3002<\/p>\n<p>\u66f4\u5177\u4f53\u7684\u4f7f\u7528\u53ef\u4ee5\u53c2\u8003<a href=\"https:\/\/www.zhangxinxu.com\/study\/202401\/js-get-mp4-video-image-demo.php\" rel=\"noopener\" target=\"_blank\">demo\u9875\u9762<\/a>\u7684\u4ee3\u7801\u793a\u610f\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u83b7\u5f97\u67d0\u4e00\u5177\u4f53\u65f6\u95f4\u7684\u7f29\u7565\u56fe\uff0c\u53ef\u4ee5\u53c2\u8003\u672c\u6587\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u7a0d\u52a0\u6539\u9020\u5c31\u53ef\u4ee5\u3002<\/p>\n<p>\u4f8b\u5982\u5c06 seekTime \u8bbe\u7f6e\u4e3a\u4f60\u9700\u8981\u7684\u65f6\u95f4\uff0c\u53ea\u7ed8\u5236\u4e00\u6b21\u5c31\u6267\u884cparams.onFinish\u65b9\u6cd5\u3002<\/p>\n<h3>\u4e09\u3001\u6709\u611f\u800c\u53d1<\/h3>\n<p>\u5728\u4e4b\u524d\uff0c\u6211\u4e00\u76f4\u4ee5\u4e3a<code>&lt;video&gt;<\/code>\u5143\u7d20\u548c<code>&lt;audio&gt;<\/code>\u5143\u7d20\u4e00\u6837\uff0c\u662f\u4e2a\u6bd4\u8f83\u9e21\u808b\u7684\u7279\u6027\uff0c\u4e0d\u80fd\u5e94\u5bf9\u590d\u6742\u7684\u9700\u6c42\u3002<\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\uff0c\u6211\u7684\u8ba4\u8bc6\u6d45\u8584\u4e86\uff0cvideo\u8fd8\u662f\u53ef\u4ee5\u7684\uff0c\u4e0d\u7136\u4e0d\u4f1a\u53ea\u6709Web Audio API\u800c\u6ca1\u6709Web Video API\u3002<\/p>\n<p>\u7422\u78e8\u4e86\u4e0b\uff0c\u8bf4\u4e0d\u5b9a\u8bd5\u8bd5\u7ed8\u5236\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u53ef\u80fd\u4f1a\u6709\u70b9\u5ef6\u65f6\u504f\u5dee\uff0c\u5e94\u8be5\u597d\u89e3\u51b3\uff0c\u518d\u7814\u7a76\u7814\u7a76\u5427\u3002<\/p>\n<p>\u597d\u5427\uff0c\u5dee\u4e0d\u591a\u5c31\u8bf4\u8fd9\u4e48\u591a\u5427\u3002<\/p>\n<p>\u5e74\u5e95\u4e86\uff0c\u8d76\u4e66\u7a3f\uff0c\u5c31\u4e0d\u591a\u5570\u55e6\u4e86\u3002<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff0c\u6b22\u8fce<button onclick=\"shareWeixin.click()\" class=\"entry-share-btn\">\u5206\u4eab<\/button>\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202401\/2024-01-15_234823.png\" width=\"120\" height=\"99\" alt=\"\u7d2b\u971e\u4ed9\u5b50\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11103\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11103<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202401\/video-thumb-cover.png\" width=\"160\" height=\"100\" alt=\"\u5c01\u9762\u56fe\uff0c\u5760\u843d\u7684\u5929\u4f7f\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u5176\u5b9e\u83b7\u53d6\u89c6\u9891\u7684\u7f29\u7565\u56fe\u5e76\u4e0d\u9700\u8981\u89c6\u9891\u89e3\u7801\u5c31\u80fd\u5b9e\u73b0\uff0c\u65b9\u6cd5\u5c31\u662f\u5176\u8c8c\u4e0d\u626c\u7684HTML video\u5143\u7d20\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[872,1064,1148,340,1919,236],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11103"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=11103"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11103\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}