具体实现代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <script src="https://unpkg.com/obs-websocket-js@5.0.5/dist/obs-ws.min.js"></script> </head> <body> <button id="btn">打开</button> <script> const makeInit = () => { const address = `ws://127.0.0.1:4455`; console.log('OBSWebSocket连接中', address) return new Promise((resolve, reject) => { const obs = new OBSWebSocket(); obs .connect(address) .then((res) => { console.log(`链接成功`, res); resolve(obs); }) .catch((err) => { console.warn(err); }); }); }; const sleep = (time = 1) => { return new Promise((resolve) => { setTimeout((_) => resolve(), time * 1000); }); }; // 获取视频; const getVideo = async (content = {}) => { console.log(`开始执行`); return new Promise(async (resolve, reject) => { console.log(`获取参数`, content); const { length = 10, direction, text, status, mp4, id, wait = 2, queryKey = '', } = content; // 横屏 const directionConfig = +direction === 1 ? { width: 1280, height: 720 } : { width: 720, height: 1280 }; const obs = await makeInit(); const playLink = `https://www.baidu.com`; const sceneName = "场景"; const inputName = '浏览器'; console.log(`全景播放地址`, playLink); console.log(`设置屏幕宽高`, directionConfig); const setting = { baseWidth: directionConfig.width, baseHeight: directionConfig.height, outputWidth: directionConfig.width, outputHeight: directionConfig.height, } const result = await obs.call('SetVideoSettings', setting); console.log(`全局设置`, setting); // 新增一个场景! try { await obs.call('CreateScene', { 'sceneName': sceneName }); } catch (e) {} // 切换到这个场景 // await obs.call('SetCurrentPreviewScene', { 'sceneName': sceneName }); const browser = { sceneName: sceneName, inputName: inputName, inputKind: 'browser_source', inputSettings: { url: playLink, ...directionConfig, }, }; console.log(`新增浏览器`, directionConfig); try { await obs.call('CreateInput', browser); } catch (e) { alert(e.message); console.log('没有创建场景'); return ; } console.log(`浏览器打开成功`); await sleep(wait); console.log(`等${wait}秒再执行...`); await obs.call(`StartRecord`).then((_) => { console.log(`📍开始录制视频`); const time = 1000 * parseInt(length); console.log(`共录制时长`, time); setTimeout((_) => { console.log(`现在开始录制`, time); obs.call(`StopRecord`).then(async ({ outputPath }) => { console.log(`结束录制视频`, outputPath); await obs.call('RemoveInput', { inputName }); console.log(`删除浏览器`); await sleep(5); console.log(`返回录制视频地址`, outputPath); resolve(outputPath); }); }, time); }); }); }; const callBack = (mp4, content = {}) => { const { id } = content; console.log(`生成成功的mp4`, mp4); }; document.getElementById('btn').addEventListener('click', getVideo); </script> </body> </html> |
功能比较简单, 可以控制 obs 打开百度录制 10 秒视频后自动关闭!