{"id":9892,"date":"2021-03-20T16:39:02","date_gmt":"2021-03-20T08:39:02","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9892"},"modified":"2021-03-20T16:39:02","modified_gmt":"2021-03-20T08:39:02","slug":"ffmpeg-wasm-audio-video-merge","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/03\/ffmpeg-wasm-audio-video-merge\/","title":{"rendered":"\u501f\u52a9ffmpeg.wasm\u7eaf\u524d\u7aef\u5b9e\u73b0\u591a\u97f3\u9891\u548c\u89c6\u9891\u7684\u5408\u6210"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9892\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9892<\/a><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><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/panda-cover.png\" width=\"480\" height=\"337\" alt=\"\u718a\u732b \u5408\u4f53\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202103-ffmpeg-wasm.png';<\/script><\/p>\n<h3>\u4e00\u3001\u5173\u4e8e\u524d\u7aef\u89c6\u9891\u751f\u6210<\/h3>\n<p>\u524d\u7aef\u89c6\u9891\u751f\u6210\u65b9\u6848\u5f88\u591a\uff1a<\/p>\n<ul>\n<li>canvas.captureStream()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9Canvas\u56fe\u50cf\u8f6c\u6210WebM\u6216MP4\u89c6\u9891\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u672c\u8d28\u4e0a\u662f\u5f55\u5c4f\uff0c\u4e14\u6ca1\u6709\u58f0\u97f3\u3002<\/li>\n<li>Chrome\u548cAndroid\u4e0b\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u56fe\u7247\u5e8f\u5217\u8f6cWebM\uff0c\u6211\u4e4b\u524d\u662f\u501f\u52a9\u7684whammy.js\uff08<a href=\"https:\/\/github.com\/antimatter15\/whammy\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/antimatter15\/whammy<\/a>\uff09\u5e2e\u5fd9\u5b9e\u73b0\u7684\uff0c\u51e0\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u4e86\u3002\u540c\u6837\u6ca1\u6709\u58f0\u97f3\u3002<\/li>\n<li>\u4f7f\u7528WebRTC\u6280\u672f\u4e2d\u7684RecordRTC\u6280\u672f\uff0c\u53ef\u4ee5\u5f55\u5236\u97f3\u89c6\u9891\u3001\u5f55\u5236\u5168\u5c4f\u7f51\u9875\u7b49\uff0c\u9879\u76ee\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/muaz-khan\/RecordRTC\" rel=\"noopener\" target=\"_blank\">https:\/\/github.com\/muaz-khan\/RecordRTC<\/a><br \/>\n\u5f55\u5236\u8f93\u5165\u97f3\u9891\u3001\u4ee5\u53ca\u8f93\u5165\u89c6\u9891\u6bd4\u8f83\u65b9\u4fbf\uff0c\u4f46\u662f\u5982\u679c\u662f\u5f55\u5236\u8f93\u51fa\u97f3\u9891\u548c\u89c6\u9891\u5728Web\u7aef\u5c31\u6709\u4e9b\u9650\u5236\uff0c\u6b64\u65f6\uff0c\u5b89\u88c5\u76f8\u5173\u7684Chrome\u63d2\u4ef6\uff0c\u53ef\u4ee5\u8ba9\u529f\u80fd\u66f4\u5f3a\u5927\uff0c\u5f55\u5236\u7f51\u9875\u65f6\u5019\uff0c\u7f51\u9875\u5185\u64ad\u653e\u58f0\u97f3\u53ef\u4ee5\u4e00\u8d77\u5f55\u4e0b\u6765\u3002\u4e0d\u8fc7\u8fd9\u4e2a\u5728Unix\u670d\u52a1\u5668\u4e0a\u4e0d\u592a\u53ef\u884c\uff0c\u56e0\u4e3a\u6ca1\u6709\u58f0\u5361\u3002<\/li>\n<\/ul>\n<p>\u4e0a\u9762\u7684\u6211\u90fd\u73a9\u8fc7\uff0c\u4e0d\u8fc7\u90fd\u6ca1\u6cd5\u6ee1\u8db3\u6211\u8fd9\u8fb9\u7684\u9700\u6c42\uff0c\u6211\u8fd9\u8fb9\u6709\u8d44\u6e90\u5e8f\u5217\uff0c\u6709\u72ec\u7acb\u7684\u97f3\u9891\uff0c\u7136\u540e\u6700\u7ec8\u8981\u751f\u6210\u4e00\u4e2a\u53ef\u4ee5\u5728\u6296\u97f3\u3001\u5feb\u624b\u4e0a\u4f20\u64ad\u7684\u89c6\u9891\u3002<\/p>\n<p>\u7528Web\u7f51\u9875\u6a21\u62df\u89c6\u9891\u6211\u73a9\u7684\u5f88\u6e9c\uff0c\u4f46\u662f\u76f4\u63a5\u53d8\u6210\u89c6\u9891\uff0c\u53ef\u5c31\u89e6\u53ca\u4e86\u6211\u7684\u6280\u672f\u76f2\u533a\u4e86\uff0c\u4e0a\u9762\u8fd9\u4e9b\u65b9\u6848\u6298\u817e\u4e86\u4e00\u5708\uff0c\u90fd\u4e0d\u592a\u7ed9\u529b\uff0c\u672c\u8d28\u4e0a\u90fd\u662f\u5f55\u5c4f\u4e3a\u4e3b\uff0c\u58f0\u97f3\u878d\u5408\u4e5f\u662f\u5927\u95ee\u9898\u3002<\/p>\n<p>\u4e8e\u662f\uff0c\u6211\u7684\u76ee\u5149\u8f6c\u5411\u4e86 FFmpeg \u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/2021-03-20_120437.png\" width=\"139\" height=\"48\" alt=\"FFmpeg\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e8c\u3001\u5173\u4e8effmpeg.wasm<\/h3>\n<p>FFmpeg \u662f\u6574\u4e2a\u8f6f\u4ef6\u754c\u975e\u5e38\u8457\u540d\u7684\u97f3\u89c6\u9891\u7b49\u6d41\u5a92\u4f53\u5904\u7406\u5de5\u5177\uff0c\u975e\u5e38\u5f3a\u5927\u3002<\/p>\n<p>ffmpeg.wasm\u5219\u662f FFmpeg \u5728\u6d4f\u89c8\u5668\u4e2d\u8fd0\u884c\u7684\u7248\u672c\u3002<\/p>\n<p>FFmpeg \u662fC\/C++\u7f16\u5199\u7684\uff0c\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u8fd0\u884c\u5462\uff1f\u56e0\u4e3aWebAssembly\uff0c\u8ba9\u5f88\u591a\u4f20\u7edf\u8bed\u8a00\u7684\u5de5\u5177\u4e5f\u80fd\u5728\u6d4f\u89c8\u5668\u4e2d\u8fd0\u884c\u3002\u517c\u5bb9\u6027\u8fd8\u662f\u4e0d\u9519\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/2021-03-20_120750.png\" width=\"631\" height=\"380\" alt=\"WebAssembly\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u5f00\u59cb<\/h4>\n<p><a href=\"https:\/\/github.com\/ffmpegwasm\/ffmpeg.wasm\" rel=\"noopener\" target=\"_blank\">ffmpeg.wasm\u8fd9\u4e2a\u9879\u76ee<\/a>\u63d0\u4f9b\u7684\u5b98\u65b9\u6848\u4f8b\u662f\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre>&lt;script src=\"https:\/\/unpkg.com\/@ffmpeg\/ffmpeg@0.9.5\/dist\/ffmpeg.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\n  const { createFFmpeg } = FFmpeg;\r\n  ...\r\n&lt;\/script&gt;<\/pre>\n<p>\u770b\u4e0a\u53bb\u662f\u4e2a\u7b80\u5355\u7684JS\u8c03\u7528\uff0c\u5b9e\u9645\u4e0a\uff0c\u771f\u8981\u5728\u672c\u5730\u8fd9\u4e48\u73a9\uff0c\u8bf7\u51c6\u5907\u8fce\u63a5\u8111\u58f3\u75bc\u7684\u6d17\u793c\u5427\uff0c\u56e0\u4e3a\u5f00\u53d1\u6548\u7387\u4f1a\u5f88\u4f4e\u3002\u800c\u5f00\u53d1\u6548\u7387\u4f4e\u7684\u539f\u56e0\u662f\u8d44\u6e90\u52a0\u8f7d\u7684\u95ee\u9898\uff0c\u8fd9\u4e2a\u770b\u8d77\u6765\u5e73\u5e73\u65e0\u5947\u7684ffmpeg.min.js\u4f1a\u53bb\u52a0\u8f7d\u540c\u6e90\u76ee\u5f55\u4e0b\u53e6\u59162\u4e2aJS\uff0c\u5230\u8fd9\u91cc\u4e5f\u90fd\u662f\u5e38\u89c4\u64cd\u4f5c\uff0c\u6ca1\u4ec0\u4e48\u7279\u522b\u7684\uff0c\u5173\u952e\u662f\uff0c\u8fd9\u4e2a\u5f02\u6b65\u52a0\u8f7d\u7684\u6838\u5fc3JS\u6709\u597d\u51e0\u5341M\u3002<\/p>\n<p>unpkg.com\u867d\u7136\u4e5f\u6709CDN\uff0c\u4f46\u662f\u6bd5\u7adf\u90fd\u662f\u5916\u7f51\uff0c\u901f\u5ea6\u582a\u5fe7\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u6211\u662f\u5168\u90e8\u90fd\u4e0b\u8f7d\u5230\u4e86\u672c\u5730\u6587\u4ef6\u5939\u4e2d\u4f7f\u7528\u7684\u3002\u51714\u4e2a\u6587\u4ef6\uff0c\u5206\u522b\u662f\uff1a<\/p>\n<ul>\n<li>ffmpeg.min.js<\/li>\n<li>ffmpeg-core.js<\/li>\n<li>ffmpeg-core.worker.js<\/li>\n<li>ffmpeg-core.wasm \uff0822.4M\uff09<\/li>\n<\/ul>\n<p>\u7136\u540e\u5f00\u59cb\u4f7f\u7528\u7684\u4ee3\u7801\u5c31\u4f1a\u662f\u8fd9\u6837\uff08\u8d44\u6e90\u6362\u6210\u672c\u5730\u5730\u5740\uff09\uff1a<\/p>\n<pre>&lt;script src=\".\/ffmpeg.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\nconst { createFFmpeg, fetchFile } = FFmpeg;\r\nconst ffmpeg = createFFmpeg({\r\n    corePath: \".\/ffmpeg-core.js\",\r\n    log: true,\r\n});\r\n<span style=\"color:green;\">\/\/ ...<\/span>\r\n&lt;\/script&gt;<\/pre>\n<p>\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u8fdb\u5165\u6b63\u5f0f\u7684\u903b\u8f91\u5f00\u53d1\u4e86\u3002<\/p>\n<p>\u4e00\u53e3\u5403\u4e0d\u4e86\u4e2a\u5927\u80d6\u5b50\uff0c\u5148\u4ece\u7b80\u5355\u7684\u4e00\u4e2aaudio\u97f3\u9891+\u4e00\u4e2avideo\u7684\u89c6\u9891\u5408\u6210\u8bf4\u8d77<\/p>\n<h3>\u4e09\u3001\u5355\u4e2a\u97f3\u89c6\u9891\u7684\u5408\u6210<\/h3>\n<p>\u5728\u9875\u9762\u4e0a\u521b\u5efa\u4e00\u4e2a<code>&lt;video&gt;<\/code>\u5143\u7d20\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre>&lt;video id=\"player\" controls height=\"344\" width=\"412\"&gt;&lt;\/video&gt;<\/pre>\n<p>\u6709\u4e00\u4e2a\u89c6\u9891\u7d20\u6750\uff0c\u540d\u4e3abj.mp4\uff0c\u80cc\u666f.mp4\u7684\u610f\u601d\uff0c\u8fd9\u662f\u4e2a\u65e0\u58f0\u89c6\u9891\uff0c\u5982\u4e0b\u6240\u793a\uff08\u4e0d\u52a8\u70b9\u51fb\u64ad\u653e\uff09\uff0c\u89c6\u9891\u53d6\u81ea\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/10\/svg-feturbulence\/\">SVG feTurbulence\u6ee4\u955c\u6df1\u5165\u4ecb\u7ecd<\/a>\u201d\u8fd9\u7bc7\u6587\u7ae0\u3002<\/p>\n<p><video src='\/\/image.zhangxinxu.com\/video\/blog\/202010\/bj.mp4' type=\"video\/mp4\" width=\"412\" height=\"344\" preload=\"auto\" autoplay autobuffer loop x-webkit-airplay=\"allow\" x5-video-player-type=\"h5\" x5-video-player-fullscreen=\"true\" x5-video-orientation=\"portraint\" webkit-playsinline=\"true\" playsinline=\"true\" \u200b\u200b\u200b\u200bposter=\"\/\/image.zhangxinxu.com\/image\/blog\/201806\/poster.png\"><\/video><\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a\u97f3\u9891\u7d20\u6750\uff0c\u540d\u4e3arecord.mp3\uff0c\u662f\u4e4b\u524d\u5f04\u768412\u70b9\u5403\u996d\u62a5\u65f6\u7684\u97f3\u9891\uff0c\u53ef\u4ee5\u70b9\u51fb\u4e0b\u9762\u7684\u97f3\u4e50\u64ad\u653e\u5668\u8bd5\u542c\u3002<\/p>\n<p><audio src=\"\/study\/202103\/record.mp3\" controls><\/audio><\/p>\n<p>\u4e8e\u662f\uff0c\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u5c31\u53ef\u4ee5\u8ba9video\u5143\u7d20\u663e\u793a\u5408\u6210\u540e\u7684\u89c6\u9891\u4e86\u3002<\/p>\n<pre>&lt;script src=\".\/ffmpeg.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\nconst { createFFmpeg, fetchFile } = FFmpeg;\r\nconst ffmpeg = createFFmpeg({\r\n    corePath: \".\/ffmpeg-core.js\",\r\n    log: true,\r\n});\r\n(async () =&gt; {\r\n    await ffmpeg.load();\r\n    \r\n    const dataInputVideo = await fetchFile('bj.mp4');\r\n    const dataInputAudio = await fetchFile('record.mp3');\r\n\r\n    ffmpeg.FS('writeFile', 'bj.mp4', dataInputVideo);\r\n    ffmpeg.FS('writeFile', 'record.mp3', dataInputAudio);\r\n    \r\n    <span style=\"color:green;\">\/\/ ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -strict experimental -map 0:v:0 -map 1:a:0 output.mp4<\/span>\r\n    await ffmpeg.run('-i', 'bj.mp4', '-i', 'record.mp3', '-c:v', 'copy', '-c:a', 'aac', '-strict', 'experimental', '-map', '0:v:0', '-map', '1:a:0', 'output.mp4');\r\n    \r\n    const data = ffmpeg.FS('readFile', 'output.mp4');\r\n    const video = document.getElementById('player');\r\n    video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video\/mp4' }));\r\n})();\r\n&lt;\/script&gt;<\/pre>\n<p>\u5355\u4e2a\u89c6\u9891\u548c\u97f3\u9891\u5408\u6210\u8fd8\u6bd4\u8f83\u7b80\u5355\uff0c\u53c2\u8003<a href=\"https:\/\/ffmpegwasm.github.io\/\" rel=\"noopener\" target=\"_blank\">ffmpeg.wasm\u5b98\u65b9<\/a>\u6848\u4f8b\u7684\u6e90\u7801\uff08\u89c1\u4e0b\u56fe\uff09\uff0c\u4ee5\u53ca\u8c37\u6b4c\u641c\u7d22\u51fa\u6765\u7684\u97f3\u89c6\u9891\u5408\u6210\u8bed\u6cd5\u5c31\u53ef\u4ee5\u6478\u7d22\u51fa\u6765\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/2021-03-20_143225.png\" width=\"480\" height=\"431\" alt=\"\u5b98\u65b9\u6e90\u7801\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p><span class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4f53\u9a8c\u7cdf\u7cd5\u7684\u5783\u573e\u76d7\u7248\u7f51\u7ad9\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u539f\u6587\u83b7\u5f97\u5f88\u597d\u7684\u4f53\u9a8c\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=9892\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u5c31\u662f\u5408\u6210\u540e\u7684\u89c6\u9891\uff0c\u5927\u5bb6\u53ef\u4ee5\u70b9\u51fb\u64ad\u653e\uff0c\u542c\u542c\u770b\u6709\u6ca1\u6709\u58f0\u97f3\uff1a<\/p>\n<p><video src='\/\/image.zhangxinxu.com\/video\/blog\/202103\/combin-audio.mp4' type=\"video\/mp4\" width=\"412\" height=\"344\" preload=\"auto\" controls x-webkit-airplay=\"allow\" x5-video-player-type=\"h5\" x5-video-player-fullscreen=\"true\" x5-video-orientation=\"portraint\" webkit-playsinline=\"true\" playsinline=\"true\" \u200b\u200b\u200b\u200bposter=\"\/\/image.zhangxinxu.com\/image\/blog\/201806\/poster.png\"><\/video><\/p>\n<p>\u4f46\u662f\u4e0a\u9762\u7684\u5c1d\u8bd5\u6ca1\u4ec0\u4e48\u9524\u5b50\u7528\uff0c\u56e0\u4e3a\u6211\u8fd9\u8fb9\u6700\u7ec8\u7684\u9700\u6c42\u662f\u5e0c\u671b\u6700\u7ec8\u7684\u89c6\u9891\u53ea\u8981\u5305\u542b\u591a\u4e2a\u97f3\u9891\u5408\u6210\u7684\uff0c\u800c\u4e14\u97f3\u9891\u7684\u4f4d\u7f6e\u90fd\u662f\u6307\u5b9a\u4f4d\u7f6e\uff0c\u5e76\u6ca1\u6709\u4e0a\u9762\u6848\u4f8b\u6f14\u793a\u7684\u8fd9\u4e48\u7b80\u5355\u3002<\/p>\n<p>\u6240\u4ee5\u8fd8\u5f97\u7ee7\u7eed\u653b\u514b\u591a\u97f3\u9891\u5408\u6210\u7684\u96be\u9898\u3002<\/p>\n<h3>\u56db\u3001\u591a\u4e2a\u97f3\u9891\u5728\u6307\u5b9a\u4f4d\u7f6e\u548c\u89c6\u9891\u5408\u6210<\/h3>\n<p>\u600e\u4e48\u529e\uff1f\u6beb\u65e0\u5934\u7eea\uff0c\u5148\u4e70\u672c\u4e66\u5b66\u4e60\u4e0b\uff0c\u7f13\u89e3\u4e0b\u7126\u8651\u3002<\/p>\n<p>\u4e1a\u754c\u5173\u4e8effmpeg\u7684\u4e66\u8fd8\u771f\u5c11\uff0c\u6ca1\u529e\u6cd5\uff0c\u6ca1\u5f97\u9009\uff0c\u5c31\u4ed6\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/ffmpeg-book.jpg\" width=\"345\" height=\"460\" alt=\"FFmpeg\u4e66\u7c4d\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u8fd9\u672c\u4e66\u5feb\u901f\u770b\u4e86\u4e00\u904d\uff0c\u53d1\u73b0\u4e0d\u662f\u5165\u95e8\u5230\u7cbe\u901a\uff0c\u800c\u4e14\u76f4\u63a5\u5c31\u8981\u7cbe\u901a\uff0c\u4e0d\u9002\u5408\u6211\u8fd9\u6837\u7684\u97f3\u89c6\u9891\u5904\u7406\u9886\u57df\u7684\u5c0f\u767d\u3002 \u200b\u200b\u200b\u200b\u6700\u7ec8\uff0c\u6211\u7684\u7126\u8651\u5e76\u6ca1\u6709\u5f97\u5230\u7f13\u89e3\uff0c\u53cd\u800c\u66f4\u52a0\u7126\u8651\u4e86\u3002<\/p>\n<p>\u5509\uff0c\u6ca1\u529e\u6cd5\uff0c\u6211\u53c8\u796d\u51fa\u8c37\u6b4c\u641c\u7d22\u5927\u6cd5\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/ga-bar.png\" width=\"514\" height=\"42\" alt=\"\u8c37\u6b4c\u641c\u7d22\u7684\u75d5\u8ff9\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u867d\u7136\u6211\u6ca1\u6cd5\u4ece0\u5f00\u59cb\u521b\u9020\uff0c\u4f46\u662f\u6211\u53ef\u4ee5\u7ad9\u5728\u5de8\u4eba\u7684\u80a9\u4e0a\u3002<\/p>\n<p>\u7136\u540e\u5728\u8fd9\u91cc\u627e\u5230\u4e86\u4e00\u4efd\u770b\u8d77\u6765\u6bd4\u8f83\u9760\u8c31\u7684\u6587\u6863\u3002\u201c<a href=\"https:\/\/segmentfault.com\/q\/1010000021299139\" rel=\"noopener nofollow\" target=\"_blank\">ffmpeg\uff1a\u5728\u89c6\u9891\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u97f3\u9891<\/a>\u201d<\/p>\n<p>\u5176\u4e2d\u7684\u4ee3\u7801\u662f\u8fd9\u4e2a\uff1a<\/p>\n<pre>ffmpeg -i 1.mp4 -i 1.3gp -i 2.3gp -i 1.mp3\r\n  -filter_complex \"[2]adelay=10000|10000[s2];[3:a][1:a][s2]amix=3[a]\"\r\n  -map 0:v -map \"[a]\" -c:v copy result.mp4<\/pre>\n<p>\u524d\u9762\u7684-i\uff0c\u540e\u9762\u7684-map\uff0c-c:v\u5565\u7684\u8fd8\u597d\uff0c\u90fd\u662f\u5e38\u7528\u7684\u57fa\u672c\u6307\u4ee4\uff0c\u773c\u775b\u5df2\u7ecf\u770b\u5230\u751f\u8001\u8327\u4e86\uff0c\u81ea\u7136\u5927\u81f4\u77e5\u9053\u4ec0\u4e48\u610f\u601d\uff0c\u5c31\u7b97\u4e0d\u77e5\u9053\uff0c\u53ea\u8981\u77e5\u9053\u8fd9\u4e48\u5199\u6709\u6548\u679c\u5c31\u884c\u4e86\u3002<\/p>\n<p>\u5173\u952e\u662f<code>-filter_complex<\/code>\u540e\u9762\u90a3\u4e00\u6233\u5b57\u7b26\u4e32\uff0c\u5230\u5e95\u662f\u4e2a\u4ec0\u4e48\u73a9\u610f\uff0c\u5b8c\u5168\u770b\u4e0d\u61c2\u3002<\/p>\n<p>\u770b\u6765\u770b\u4e0d\u61c2\u7684\u8fd8\u4e0d\u6b62\u6211\u4e00\u4e2a\uff0c\u54c8\u54c8\u54c8\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/08f.png\" width=\"576\" height=\"431\" alt=\"\u770b\u4e0d\u61c2\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u770b\u4e0d\u61c2\u561b\uff0c\u5f88\u7b80\u5355\uff0c\u641e\u61c2\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u8fd9\u4e2a\u65f6\u5019\uff0c\u6211\u53c8\u7ffb\u5f00\u4e86\u90a3\u672c\u201c\u4ece\u7cbe\u901a\u5230\u66f4\u7cbe\u901a\u201d\u7684\u90a3\u672cFFmpeg\u4e66\uff0c\u82b1\u4e00\u4e2a\u5c0f\u65f6\u5feb\u901f\u8fc7\u4e00\u904d\u8fd8\u662f\u6709\u7528\u7684\uff0c\u8bb0\u4f4f\u4e86\u6709\u4e00\u5904\u6709\u4ecb\u7ecd-filter_complex\uff0c\u6211\u7ffb\u5f00\uff0c\u770b\u5230\u4e86\u8fd9\u4e48\u4e00\u53e5\u8bdd\uff0c\u6211\u6807\u6ce8\u4e0b\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/book-photo.jpg\" width=\"637\" height=\"294\" alt=\"\u4e66\u4e2d\u89e3\u91ca\u6807\u6ce8\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u54e6\uff0c\u540e\u9762\u6846\u6846\u662f\u4e34\u65f6\u6807\u8bb0\u540d\uff0c\u662f\u81ea\u5df1\u5b9a\u4e49\u7684\uff0c\u524d\u9762\u7684\u7684\u662f\u8f93\u5165\u6807\u8bb0\uff0c\u5e94\u8be5\u6709\u7279\u5b9a\u7684\u683c\u5f0f\u6216\u8005\u7ea6\u5b9a\u3002<\/p>\n<p>\u4e0b\u9762\u5c31\u662f\u641e\u61c2\u4e24\u4e2a[]\u4e2d\u95f4\u7684\u6ee4\u955c\u53c2\u6570\u662f\u4ec0\u4e48\u610f\u601d\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u53c2\u6570\u561b\u90a3\u5c31\u662f\u5b98\u65b9\u6587\u6863\u641c\u4e00\u641c\u5c31\u597d\u4e86\uff1a<a href=\"http:\/\/ffmpeg.org\/ffmpeg-filters.html\" rel=\"noopener\" target=\"_blank\">http:\/\/ffmpeg.org\/ffmpeg-filters.html<\/a> <\/p>\n<p>Ctrl + F\u4e00\u641c\u5c31\u5339\u914d\u4e0a\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/adely.png\" width=\"438\" height=\"585\" alt=\"adelay\u53c2\u6570\u542b\u4e49\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>adelay\u8868\u793aaudio\u7684\u5ef6\u65f6\uff0c\u7ba1\u9053\u7b26\u5199\u6cd5\u8868\u793a\u4e0d\u540c\u58f0\u9053\u7684\u5ef6\u65f6\u3002\u5982\u679c\u540c\u65f6\u5ef6\u65f6\uff0c\u7ba1\u9053\u7b26\u524d\u540e\u65f6\u95f4\u8bbe\u4e3a\u4e00\u6837\u7684\u6570\u503c\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/amix.png\" width=\"504\" height=\"348\" alt=\"amix\u6548\u679c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>amix\u8868\u793a\u5408\u5e76\uff0c\u591a\u4e2a\u97f3\u9891\u5408\u6210\u4e00\u4e2a\u3002<\/p>\n<p>\u54c7\u54e6~~~~~~~~~~~~<\/p>\n<p>\u4e16\u754c\u4e00\u4e0b\u5b50\u8c41\u7136\u5f00\u6717\uff0c\u77e5\u9053\u600e\u4e48\u56de\u4e8b\u4e86\uff0c\u4e8b\u60c5\u5c31\u5df2\u7ecf\u6210\u529f\u4e86\u4e00\u5927\u534a\u4e86\uff0c\u4e8e\u662f\u4f9d\u846b\u82a6\u753b\u74e2\uff0c\u6309\u7167\u81ea\u5df1\u7406\u89e3\u5199\u4e86\u8d77\u6765\u3002<\/p>\n<p>\u627e\u4e86\u4e2a\u957f\u957f\u7684\u89c6\u9891\uff0c\u4e3a\u89c4\u907f\u7248\u6743\u98ce\u9669\uff0c\u6211\u81ea\u5df1\u51fa\u5883\u4e86\uff0c\u7136\u540e\u627e\u4e864\u6bb5\u673a\u5668\u751f\u6210\u7684mp3\u8bed\u97f3\uff0c\u5206\u522b\u662f1.mp3\uff0c2.mp3\uff0c3.mp3\uff0c4.mp3\uff0c\u7136\u540e\u4ee3\u7801\u64b8\u8d77\u6765\u3002<\/p>\n<pre>(async () => {\r\n    await ffmpeg.load();\r\n\r\n    const dataInputVideo = await fetchFile('zhangxinxu.mp4');\r\n    const dataInputAudio1 = await fetchFile('1.mp3');\r\n    const dataInputAudio2 = await fetchFile('2.mp3');\r\n    const dataInputAudio3 = await fetchFile('3.mp3');\r\n    const dataInputAudio4 = await fetchFile('4.mp3');\r\n\r\n    ffmpeg.FS('writeFile', 'zhangxinxu.mp4', dataInputVideo);\r\n    ffmpeg.FS('writeFile', '1.mp3', dataInputAudio1);\r\n    ffmpeg.FS('writeFile', '2.mp3', dataInputAudio2);\r\n    ffmpeg.FS('writeFile', '3.mp3', dataInputAudio3);\r\n    ffmpeg.FS('writeFile', '4.mp3', dataInputAudio4);\r\n\r\n    await ffmpeg.run('-i', 'zhangxinxu.mp4', '-i', '1.mp3', '-i', '2.mp3', '-i', '3.mp3', '-i', '4.mp3',     \r\n    '-filter_complex', '[1]adelay=2000|2000[aout1];[2]adelay=10000|10000[aout2];[3]adelay=15000|15000[aout3];[4]adelay=20000|20000[aout4];[aout1][aout2][aout3][aout4]amix=4[aout]',\r\n    '-c:v', 'copy', '-c:a', 'aac', '-strict', 'experimental', '-map', '0:v:0', '-map', '[aout]', 'output.mp4');\r\n    \r\n    const data = ffmpeg.FS('readFile', 'output.mp4');\r\n    const video = document.getElementById('player');\r\n    video.src = URL.createObjectURL(new Blob([data.buffer], { type: 'video\/mp4' }));\r\n})();<\/pre>\n<p>\u7136\u540e\uff0c\u9875\u9762\u4e00\u5237\u65b0\uff0c\u6069\uff0c\u5c45\u7136\u2026\u2026\u5c45\u7136\u2026\u2026\u89c6\u9891\u753b\u9762\u51fa\u6765\u4e86\uff0c\u4e00\u8bd5\u542c\uff0c\u5c45\u7136\u97f3\u9891\u90fdTN\u7684\u5408\u5e76\u6210\u529f\u4e86\uff0c\u5747\u5206\u522b\u57282s\uff0c10s\uff0c15s\u548c20s\u5904\u5408\u6210\u5230\u4e86\u89c6\u9891\u4e2d\u3002<\/p>\n<p>\u6211\u6709\u4e9b\u4e0d\u6562\u76f8\u4fe1\uff0c\u4ee5\u81f3\u4e8e\u9677\u5165\u4e86\u6c89\u601d\uff0c\u4e3a\u4ec0\u4e48\u6211\u53ef\u4ee5\u4e00\u6c14\u5475\u6210\uff1f\u601d\u6765\u60f3\u53bb\uff0c\u53ef\u80fd\u662f\u56e0\u4e3a\u6700\u8fd1\u751f\u75c5\u4e00\u573a\u3001\u5916\u52a0\u624b\u673a\u6389\u8fdb\u9c7c\u7f38\u8fb9\u7816\u5934\u3001\u517b\u4e86\u51e0\u5e74\u7684\u5c0f\u9f9f\u9f9f\u7a81\u7136\u66b4\u6bd9\u7b49\u4e8b\u60c5\u7684\u53d1\u751f\uff0c\u6bd5\u7adf\uff0c\u4eba\u54c1\u662f\u5b88\u6052\u7684\u3002<\/p>\n<p>\u770b\u6765\uff0c\u4eba\u751f\u8fc7\u5f80\u6240\u53d7\u8fc7\u7684\u6240\u6709\u82e6\u96be\u90fd\u662f\u6709\u610f\u4e49\u7684\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202103\/biaoqing-c.jpg\" width=\"200\" height=\"137\" alt=\"\u4eab\u53d7\u7684\u8868\u60c5\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u5bf9\u4e86\uff0c\u6700\u7ec8\u5408\u6210\u7684\u89c6\u9891\u5982\u4e0b\uff0c\u5927\u5bb6\u53ef\u4ee5\u68c0\u9605\u4e0b\uff0c\u70b9\u51fb\u64ad\u653e\u3001\u603b\u517122\u79d2\uff0c\u51715M+\uff0c1080P\u3002<\/p>\n<p><video src='\/\/image.zhangxinxu.com\/video\/blog\/202103\/mul-audio-combe.mp4' type=\"video\/mp4\" width=\"480\" height=\"270\" preload=\"auto\" controls x-webkit-airplay=\"allow\" x5-video-player-type=\"h5\" x5-video-player-fullscreen=\"true\" x5-video-orientation=\"portraint\" webkit-playsinline=\"true\" playsinline=\"true\" \u200b\u200b\u200b\u200bposter=\"\/\/image.zhangxinxu.com\/image\/blog\/201806\/poster.png\"><\/video><\/p>\n<h3>\u4e94\u3001\u70b9\u8bc4\u548c\u7ed3\u8bed<\/h3>\n<p>ffmpeg.wasm\u7684\u6838\u5fc3\u6587\u4ef6\u7684\u4f53\u79ef\u5b9e\u5728\u662f\u592a\u5927\u4e86\uff0c\u5c31\u7b97Gzip\u4e0b\uff0c\u8981\u5c11\u8bf47M\u591a\uff0c\u8fd9\u4e2aJS\u53c8\u4e0d\u662f\u89c6\u9891\uff0c\u53ef\u4ee5\u53d8\u4e0b\u8f7d\u8fb9\u62e8\uff0c\u8fd9JS\u4e0b\u8f7d\u4e0d\u6210\u529f\uff0c\u529f\u80fd\u5c31\u8dd1\u4e0d\u8d77\u6765\u3002<\/p>\n<p>\u6211\u81ea\u5df1\u7528\u5c40\u57df\u7f51\u6d4b\u8bd5\u4e86\u4e0b\uff0c\u540c\u4e00\u4e2a\u8def\u7531\u4e0b\uff0cA\u7535\u8111\u8bbf\u95eeB\u7535\u8111\u4e0a\u7684ffmpeg.wasm\uff0c\u5c45\u7136\u7b49\u4e86\u67096~7\u79d2\uff0c\u8d44\u6e90\u624d\u51c6\u5907\u597d\u3002<\/p>\n<p>\u90a3\u8981\u662f\u5728\u7ebf\uff1f\u5567\u5567\uff0c\u8fd9\u7528\u6237\u4f53\u9a8c\u4e0d\u6562\u60f3\u8c61\u3002<\/p>\n<p>\u8981\u60f3\u5728\u7528\u6237\u4fa7\u4f7f\u7528\uff0c\u4f30\u8ba1\u8981\u4e48\u8ba9\u7528\u6237\u5b89\u88c5\u4e0bffmpeg.wasm\u63d2\u4ef6\uff0c\u8981\u4e48\u8981\u628aloading\u52a0\u8f7d\u505a\u51fa\u82b1\u6765\uff01<\/p>\n<p>\u95ee\u9898\u8fd8\u4e0d\u6b62\u8fd9\u4e9b\uff0c\u9664\u4e86\u52a0\u8f7d\u7684\u95ee\u9898\uff0c\u8fd8\u6709\u4e2a\u5934\u75bc\u7684\u95ee\u9898\u662f\u90e8\u5206\u7528\u6237\u7684\u8bbe\u5907wasm\u8dd1\u4e0d\u8d77\u6765\uff0c\u4f8b\u5982\u6211\u5bb6\u91ccPC\u4e3b\u673a\uff0c\u662f32\u4f4d\u7cfb\u7edf\uff0cChrome\u548cFirefox\u5747\u8dd1\u4e0d\u8d77\u6765\uff0c\u5206\u522b\u62a5\uff1a<\/p>\n<blockquote><p>WebAssembly.Memory(): could not allocate memory<\/p><\/blockquote>\n<p>\u548c<\/p>\n<blockquote><p>Uncaught (in promise) out of memory<\/p><\/blockquote>\n<p>\u7684\u9519\u8bef\u3002<\/p>\n<p>\u516c\u53f8\u7684PC\u7535\u8111\uff0c\u81ea\u5df1\u7684Mac Pro\u7b49\u90fd\u662f\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\u7684\u3002<\/p>\n<p>\u8c8c\u4f3c\u662f\u7cfb\u7edf\u5e26\u6765\u7684\u5185\u5b58\u5206\u914d\u95ee\u9898\u3002<\/p>\n<p>\u76f8\u4fe1\u65e2\u7136\u6211\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u95ee\u9898\uff0c\u7528\u6237\u4e5f\u53ef\u80fd\u9047\u5230\u3002<\/p>\n<p>\u56e0\u6b64\uff0cffmpeg.wasm\u76ee\u524d\u53ea\u80fd\u4f5c\u4e3aVIP\u529f\u80fd\u5c0f\u90e8\u5206\u7528\u6237\u4f7f\u7528\u3001\u6216\u8005\u7ed9\u81ea\u5df1\u4eba\u4f7f\u7528\uff0c\u5927\u89c4\u6a21\u5bf9\u5916\u8fd8\u4e0d\u6210\u719f\u3002<\/p>\n<h4>\u89c6\u9891\u4ece\u54ea\u91cc\u6765\uff1f<\/h4>\n<p>\u901a\u5e38\u89c6\u9891\u90fd\u662f\u901a\u8fc7canvas\u8fd9\u4e2a\u5a92\u4ecb\u751f\u6210\u7684\uff0c\u65e0\u8bba\u662fwebRTC\uff0c\u8fd8\u662fhtml\u622a\u56fe\uff0c\u6216\u8005\u662fcanvas\u52a8\u753b\u6355\u83b7\uff0c\u9650\u4e8e\u7bc7\u5e45\uff0c\u8fd9\u91cc\u4e0d\u5c55\u5f00\u4ecb\u7ecd\uff0c\u4ee5\u540e\u65f6\u673a\u5408\u9002\uff0c\u6211\u4e13\u95e8\u6f14\u793a\u4e0b\u3002<\/p>\n<h4>Demo\u5728\u54ea\u91cc\uff1f<\/h4>\n<p>Demo\u6709\uff0c\u4f46\u662f\u88ab\u6211\u81ea\u5df1\u85cf\u8d77\u6765\u4e86\uff0c\u6d41\u91cf\u4f24\u4e0d\u8d77\u554a\uff0cffmpeg.wasm\u6838\u5fc3\u6587\u4ef6\u592a\u5927\u4e86\uff0c\u8fd8\u6709\u89c6\u9891\u4e5f\u90fd\u662f\u5927\u6587\u4ef6\uff0c\u670d\u52a1\u5668\u5c312M\u7684\u5e26\u5bbd\u51fa\u53e3\uff0c\u6e9c\u4e86\u6e9c\u4e86\u2026\u2026<\/p>\n<p>\u5509\uff0c\u4ec0\u4e48\u65f6\u5019\u80fd\u670d\u52a1\u5668\u5e26\u5bbd\u81ea\u7531\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u5927\u5bb6\u53ef\u4ee5\u591a\u591a\u8f6c\u53d1\u8f6c\u53d1\uff0c\u8bf4\u4e0d\u5b9a\u4f1a\u54ea\u4f4d\u571f\u8c6a\u7238\u7238\u770b\u5230\uff0c\u628a\u6211\u7684\u7f51\u7ad9\u6536\u8d2d\uff0c\u6216\u8005\u6295\u8d44\u4e00\u7b14\u5462\uff1f<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f913.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/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=9892\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9892<\/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\/202103\/panda-cover.png\" width=\"160\" height=\"112\" alt=\"\u718a\u732b \u5408\u4f53\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u4e0d\u501f\u52a9C\/C++\uff0c\u7eafJS\u6d4f\u89c8\u5668\u4e2dffmpeg.wasm\u5b9e\u73b0\u6307\u5b9a\u957f\u5ea6\u3001\u4f4d\u7f6e\u548c\u6570\u91cf\u7684\u97f3\u9891\u548c\u89c6\u9891\u5408\u5e76\u6210\u65b0\u7684\u89c6\u9891\uff0c\u6b64\u9886\u57df\u76ee\u524d\u4e3a\u6570\u4e0d\u591a\u4f18\u8d28\u6587\u7ae0\uff0c\u5efa\u8bae\u6536\u85cf\u5907\u5fd8\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,53,1741,1738,1743,340,1745,1739,1742,1744,1129,1740,1092],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9892"}],"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=9892"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9892\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}