{"id":12166,"date":"2026-04-17T15:22:57","date_gmt":"2026-04-17T07:22:57","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12166"},"modified":"2026-04-17T15:22:57","modified_gmt":"2026-04-17T07:22:57","slug":"mediabunny-video-water-clip-merge","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2026\/04\/mediabunny-video-water-clip-merge\/","title":{"rendered":"\u501f\u52a9mediabunny\u7eafJS\u5b9e\u73b0\u89c6\u9891\u6c34\u5370\u3001\u526a\u88c1\u3001\u5408\u6210\u7b49\u529f\u80fd"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12166\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=12166<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0cAI\u6293\u53d6\u4fdd\u7559\u539f\u6587\u5730\u5740\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<h3>\u4e00\u3001\u63a8\u8350\u4f7f\u7528mediabunny<\/h3>\n<p>\u5927\u7ea62\u5e74\u524d\uff0c\u6211\u66f4\u65b0\u4e86\u5927\u91cf\u7684\u97f3\u89c6\u9891\u5904\u7406\u7684\u6587\u7ae0\uff0c\u4e0d\u8fc7\u91cc\u9762\u7684\u6280\u672f\u5b9e\u73b0\u5927\u90fd\u4f7f\u7528\u539f\u751f\u4ee3\u7801\u548cWebCodecs API\u624b\u6413\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u56e0\u4e3a\u90a3\u4e2a\u65f6\u5019\uff0cWebCodecs API\u521a\u51fa\u6765\uff0c\u6280\u672f\u8fd8\u4e0d\u6210\u719f\u3002<\/p>\n<p>\u81ea\u7136\u800c\u7136\uff0c\u5c31\u9646\u7eed\u51fa\u73b0\u4e86\u4e0d\u5c11\u57fa\u4e8eWebCodecs API\u5c01\u88c5\u7684\u97f3\u89c6\u9891\u5904\u7406\u6846\u67b6\u3002<\/p>\n<p>\u7ecf\u8fc7\u8fd9\u4e9b\u5e74\u7684\u53d1\u5c55\uff0c\u6709\u4e00\u4e2a\u5a92\u4f53\u5de5\u5177\u5305\u5f02\u519b\u7a81\u8d77\uff0c\u90a3\u5c31\u662fmediabunny\uff01<\/p>\n<p>\u9879\u76ee\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/Vanilagy\/mediabunny\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/Vanilagy\/mediabunny<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202604\/2026-4-16_193804.png\" width=\"296\" height=\"94\" class=\"size-medium\" alt=\"mediabubby logo\" \/> <\/p>\n<p>2\u4e2a\u6708\u524d\uff0c\u6211\u5728\u5fae\u535a\u4ecb\u7ecd\u8fc7\u7684<a href=\"https:\/\/www.zhangxinxu.com\/sp\/mp4webm\/\" rel=\"noopener\" target=\"_blank\" style=\"color:#f30;\">MP4\/MOV\u89c6\u9891\u8f6cWebM\u683c\u5f0f\u5728\u7ebf\u5de5\u5177<\/a>\u5c31\u4f7f\u7528\u4e86\u6b64\u9879\u76ee\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202604\/2026-4-16_194405.webp\" width=\"345\" height=\"182\" alt=\"\u89c6\u9891\u8f6c\u683c\u5f0f\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>mediabunny\u7684\u80fd\u529b\u4e0d\u4ec5\u4ec5\u5728\u4e8e\u89c6\u9891\u683c\u5f0f\u8f6c\u6362\u4e0e\u538b\u7f29\uff0c\u6dfb\u52a0\u6c34\u5370\u3001\u65f6\u957f\u526a\u88c1\u7b49\u90fd\u4e0d\u5728\u8bdd\u4e0b\uff0c\u672c\u6587\u5c31\u901a\u8fc7\u6211\u8dd1\u901a\u7684demo\u7ed9\u5927\u5bb6\u770b\u4e0b\u8fd9\u7c7b\u9700\u6c42\u8be5\u5982\u4f55\u5b9e\u73b0\u3002<\/p>\n<h3>\u4e8c\u3001\u7ed9\u89c6\u9891\u6dfb\u52a0\u6c34\u5370<\/h3>\n<p>\u8bdd\u4e0d\u591a\u8bf4\uff0c\u5148\u76f4\u63a5\u4e0a\u624b\u4f53\u9a8c\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202604\/js-video-water-demo.php\" rel=\"noopener\" target=\"_blank\">\u7eaf\u524d\u7aef\u5b9e\u73b0\u89c6\u9891\u6dfb\u52a0\u6c34\u5370\u6548\u679cdemo<\/a><\/p>\n<p>\u9009\u62e9\u89c6\u9891\u548c\u9700\u8981\u7684\u6c34\u5370\u56fe\u7247\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u6700\u7ec8\u7684\u6548\u679c\u4e86\uff0c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202604\/2026-4-16_201714.webp\" width=\"419\" height=\"749\" alt=\"\u6c34\u5370\u5408\u6210\u6548\u679c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5176\u4e2d\uff0c\u6700\u5173\u952e\u7684\u5408\u6210\u5c31\u662f\u4e0b\u9762\u8fd9\u90e8\u5206\u4ee3\u7801\uff1a<\/p>\n<pre>let ctx = null;\r\nconst conversion = await Conversion.init({\r\n    input,\r\n    output,\r\n    video: {\r\n        process: (sample) =&gt; {\r\n            if (!ctx) {\r\n                \/\/ \u521b\u5efacanvas\r\n                const canvas = new OffscreenCanvas(\r\n                    sample.displayWidth,\r\n                    sample.displayHeight\r\n                );\r\n                ctx = canvas.getContext('2d');\r\n            }\r\n\r\n            ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);\r\n            sample.draw(ctx, 0, 0);\r\n            ctx.drawImage(watermark, 80, 80 * watermark.naturalHeight \/ watermark.naturalWidth);\r\n\r\n            return ctx.canvas;\r\n        }\r\n    }\r\n});<\/pre>\n<p>\u4f7f\u7528Conversion.init\u89e3\u7801\u89c6\u9891\u7684\u6bcf\u4e00\u5e27(sample)\uff0c\u7136\u540e\u4f7f\u7528canvas\u5c06\u753b\u9762\u548c\u6c34\u5370\u56fe\u91cd\u65b0\u7ed8\u5236\uff0c\u518d\u8fd4\u56de\u5f53\u524dcanvas\u5373\u53ef\u3002<\/p>\n<p>\u5176\u5b9e\u4e0d\u4ec5\u662f\u6c34\u5370\u5408\u6210\uff0c\u4efb\u4f55\u753b\u9762\u7279\u6548\uff0c\u5b57\u5e55\u6dfb\u52a0\uff0c\u906e\u7f69\uff0c\u5c3a\u5bf8\u8bbe\u7f6e\u90fd\u53ef\u4ee5\u4f7f\u7528\u6b64\u65b9\u6cd5\u5b9e\u73b0\uff0c\u539f\u7406\u90fd\u662f\u4e00\u6837\u7684\uff0c\u90fd\u662f\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<h3>\u4e09\u3001Video\u89c6\u9891\u524d\u540e\u526a\u88c1<\/h3>\n<p>\u540c\u6837\u7684\uff0c\u5148\u770b\u5b9e\u73b0\u6548\u679c\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202604\/js-video-clip-demo.php\" rel=\"noopener\" target=\"_blank\">\u7eaf\u524d\u7aef\u5b9e\u73b0\u89c6\u9891\u9996\u5c3e\u526a\u88c1demo<\/a><\/p>\n<p>\u9009\u62e9\u4efb\u610f\u7684\u89c6\u9891\uff0c\u7136\u540e\u62d6\u9009\u6ed1\u7aff\u9009\u62e9\u9700\u8981\u7684\u89c6\u9891\u7247\u6bb5\uff0c\u70b9\u51fb\u7ea2\u8272\u7684\u526a\u88c1\u6309\u94ae\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u88ab\u526a\u88c1\u540e\u7684\u89c6\u9891\u4e86\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202604\/2026-4-16_202408.webp\" width=\"482\" height=\"499\" alt=\"\u526a\u88c1demo\u793a\u610f\" class=\"alignnone size-medium\" style=\"border:1px solid #ddd\" \/><\/p>\n<p>\u5b9e\u9645\u751f\u4ea7\u73af\u5883\uff0c\u62d6\u62fd\u7684\u5e94\u8be5\u662f\u7f29\u5217\u56fe\u7684\u5de6\u53f3\u4e24\u4e2a\u5c0f\u7fc5\u8180\uff0c\u8fd9\u91cc\u4e3a\u4e86\u7b80\u5316\u4f7f\u7528\uff0c\u4f7f\u7528\u4e86LuLu UI\u7684\u53cc\u6ed1\u5757\u6a21\u62df\u3002<\/p>\n<p>\u5176\u6838\u5fc3\u5b9e\u73b0\u4ee3\u7801\u6781\u4e3a\u7b80\u5355\uff1a<\/p>\n<pre>const input = new Input({\r\n    formats: ALL_FORMATS,\r\n    source: new BlobSource(videoFile),\r\n});\r\nconst output = new Output({\r\n    format: new Mp4OutputFormat(), \/\/ The format of the file\r\n    target: new BufferTarget(),\r\n});\r\n\r\nconst eleRange = range.querySelector('input');  \r\nconst conversion = await Conversion.init({\r\n    input,\r\n    output,\r\n    trim: {\r\n        start: eleRange.from,\r\n        end: eleRange.to,\r\n    },\r\n});\r\n\r\nawait conversion.execute();<\/pre>\n<p>\u4f7f\u7528<code>trim<\/code>\u53c2\u6570\uff0c\u6307\u5b9a\u8d77\u6b62\u65f6\u95f4\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u5b8c\u6574\u4ee3\u7801\u53ef\u4ee5\u8bbf\u95ee\u6f14\u793a\u9875\u9762\u3002<\/p>\n<h3>\u56db\u3001\u591a\u97f3\u9891\u548c\u753b\u9762\u7684\u89c6\u9891\u5408\u6210<\/h3>\n<p>\u4e00\u4f8b\u80dc\u5343\u8a00\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202604\/js-video-merge-demo.php\" rel=\"noopener\" target=\"_blank\">\u7eaf\u524d\u7aef\u5b9e\u73b0\u753b\u9762\u52a0\u97f3\u9891\u7684\u89c6\u9891\u5408\u6210demo<\/a><\/p>\n<p>\u9ed8\u8ba4\u63d0\u4f9b\u4e86\u5b57\u5e55\u3001\u80cc\u666f\u56fe\u3001\u53f0\u8bcd\uff0c\u80cc\u666f\u97f3\u4e50\u53ef\u9009\uff0c\u7528\u6237\u53ef\u4ee5\u81ea\u5df1\u4e0a\u4f20\uff0c\u53ef\u4ee5\u5408\u6210\u6700\u7ec8\u7684\u89c6\u9891\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202604\/2026-4-17_143054.webp\" width=\"463\" height=\"480\" alt=\"\u89c6\u9891\u5408\u6210demo\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6211\u67e5\u4e86\u4e0bAPI\uff0cmediabunny\u4e2d\u4f3c\u4e4e\u7f3a\u5c11AudioBuffer\u5904\u7406\u65b9\u6cd5\uff0c\u5f53\u7136\uff0c\u4e5f\u53ef\u80fd\u6709\uff0c\u6211\u81ea\u5df1\u6ca1\u627e\u5230\u3002<\/p>\n<p>\u8fd9\u91cc\u7684AudioBuffer\u526a\u88c1\u548c\u5408\u5e76\u7528\u7684\u662f\u6211\u81ea\u5df1\u4e4b\u524d\u624b\u6413\u7684\u65b9\u6cd5\u3002<\/p>\n<ul>\n<li><a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2023\/10\/js-audio-audiobuffer-concat-merge\/\">\u7eafJS\u5b9e\u73b0\u591a\u4e2a\u97f3\u9891\u7684\u62fc\u63a5\u6216\u8005\u5408\u5e76<\/a><\/li>\n<li><a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/07\/js-audio-clip-copy-upload\/\">JS\u7eaf\u524d\u7aef\u5b9e\u73b0audio\u97f3\u9891\u526a\u88c1\u526a\u5207\u590d\u5236\u64ad\u653e\u4e0e\u4e0a\u4f20<\/a><\/li>\n<\/ul>\n<p>\u76f8\u5173\u6e90\u7801\u5728\u9875\u9762\u5de6\u4fa7\uff08\u79fb\u52a8\u7aef\u5728\u4e0b\u65b9\uff09\u6709\u5b8c\u6574\u5c55\u793a\uff0c\u57fa\u672c\u4e0a\u76f8\u5173\u7684\u89c6\u9891\u5408\u6210\u90fd\u53ef\u4ee5\u5b9e\u73b0\u4e86\u3002<\/p>\n<p>\u8fd9\u91cc\u63d0\u4f9b\u4e0b\u6838\u5fc3\u5b9e\u73b0\u90e8\u5206\uff1a<\/p>\n<pre>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u89c6\u9891\u5408\u6210\u8f93\u51fa\r\nconst output = new Output({\r\n    format: new Mp4OutputFormat(), \r\n    target: new BufferTarget(),\r\n});\r\n\r\n\/\/ \u6dfb\u52a0\u89c6\u9891\u8f68\u9053\uff0c\u753b\u9762\u6e90\u81eacanvas\r\nconst videoSource = new CanvasSource(canvas, {\r\n    codec: 'avc',\r\n    bitrate: QUALITY_HIGH,\r\n});\r\noutput.addVideoTrack(videoSource);\r\n\r\n\/\/ \u6dfb\u52a0\u97f3\u8f68\r\nconst audioSource = new AudioBufferSource({\r\n    codec: 'aac',\r\n    bitrate: QUALITY_HIGH,\r\n});\r\noutput.addAudioTrack(audioSource);\r\n\r\nawait output.start();\r\n\r\n\/\/ \u83b7\u53d6\u97f3\u9891\u6587\u4ef6\r\nconst duration = audioFile.duration;\r\n\r\n\/\/ \u6bcf\u79d230\u5e27\r\nfor (let frame = 0; frame &lt; 30 * duration; frame++) {\r\n    draw(frame);\r\n    await videoSource.add(frame \/ 30, 1 \/ 30);\r\n}\r\n\r\n\/\/ \u83b7\u53d6\u97f3\u9891\u7684 audioBuffer\u2026\u2026\uff08\u4ee3\u7801\u7565\uff09\uff0c\u7136\u540e\u6dfb\u52a0\r\nawait audioSource.add(audioBuffer);\r\n\r\nawait output.finalize();\r\n<\/pre>\n<h3>\u4e94\u3001\u5e7f\u544a\u65f6\u95f4<\/h3>\n<p>\u63a8\u8350\u4e0b\u6211\u51e0\u5e74\u524d\u5728\u6398\u91d1\u4e0a\u66f4\u65b0\u7684\u4eba\u6587\u7c7b\u8bfe\u7a0b<a href=\"https:\/\/s.juejin.cn\/ds\/aBrMyLZcGws\/\" rel=\"noopener\" target=\"_blank\">\u300a\u6280\u672f\u5199\u4f5c\u6307\u5357\u300b<\/a>\u3002<\/p>\n<p><a href=\"https:\/\/s.juejin.cn\/ds\/aBrMyLZcGws\/\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202604\/2026-4-17_151119.webp\" width=\"528\" height=\"203\" alt=\"\u6280\u672f\u5199\u4f5c\u6307\u5357\" class=\"alignnone size-medium\" \/><\/a><\/p>\n<p>\u65e2\u662f\u5173\u4e8e\u5199\u4f5c\uff0c\u4e5f\u662f\u5173\u6ce8\u4e2a\u4eba\u6210\u957f\uff01<\/p>\n<p>OK\uff0c\u5c31\u8bf4\u8fd9\u4e48\u591a\uff0c\u5982\u679c\u4f60\u89c9\u5f97\u672c\u6587\u5185\u5bb9\u5bf9\u4f60\u7684\u5de5\u4f5c\u4e0e\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff0c\u6b22\u8fce\u8f6c\u53d1\uff0c\u70b9\u8d5e\uff01<\/p>\n<p>\ud83d\ude09\ud83d\ude0a\ud83d\ude07<br \/>\n\ud83e\udd70\ud83d\ude0d\ud83d\ude18<\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12166\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=12166<\/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\/202604\/2026-4-17_152137.webp\" width=\"160\" height=\"103\" alt=\"\u89c6\u9891\u5408\u6210\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>mediabunny\u662f\u4e00\u4e2a\u975e\u5e38\u6210\u719f\u7684\u97f3\u89c6\u9891\u5904\u7406\u5de5\u5177\u5e93\uff0c\u76ee\u524d\u662f\u4f17\u591a\u57fa\u4e8eWebCodecs API\u7684\u9879\u76ee\u4e2d\u51fa\u7c7b\u62d4\u8403\u7684\u90a3\u4e00\u4e2a\uff0c\u672c\u6587\u5c31\u5c06\u901a\u8fc7\u591a\u4e2a\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528mediabunny\u5b9e\u73b0\u89c6\u9891\u7684\u6c34\u5370\u3001\u526a\u88c1\u4ee5\u53ca\u5408\u6210\u7b49\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":[489,1638,53,2113,340,2114,1740,1750],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12166"}],"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=12166"}],"version-history":[{"count":9,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12166\/revisions"}],"predecessor-version":[{"id":12193,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12166\/revisions\/12193"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=12166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=12166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=12166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}