我想并行运行五个生成命令。我将五个hls流网址传递给循环,这些streamlink命令应将视频记录5秒钟,然后终止这些进程。
我试图通过几种方式使其异步...但是我不知道如何为每个流分别等待5秒钟。
我正在Windows 10上运行它。
这是我尝试的最后一件事:
import { spawn } from "child_process";
import crypto from "crypto"
const hls_streams = [
'https://stream1.url',
'https://stream2.url',
'https://stream3.url',
'https://stream4.url',
'https://stream5.url',
]
for (let i = 0; i < hls_streams.length; i++) {
const filename = crypto.randomBytes(16).toString("hex");
const child = spawn('streamlink', [`${urls[i]}`, "best", "-f", "-o", `/temp/${filename}.mp4`]);
await new Promise(r => setTimeout(r, 5000));
child.kill()
}
五个网址的正确执行应仅持续5秒钟...
您可以使用一个循环来创建一个子数组,然后等待,然后使用另一个循环来结束它们。要基于已存在的数组创建数组,map()
可能会更加方便:
import { spawn } from "child_process";
import crypto from "crypto";
const hls_streams = [
'https://stream1.url',
'https://stream2.url',
'https://stream3.url',
'https://stream4.url',
'https://stream5.url',
];
let children = hls_streams.map(url => {
const filename = crypto.randomBytes(16).toString("hex");
const child = spawn('streamlink', [`${url}`, "best", "-f", "-o", `/temp/${filename}.mp4`]);
return child;
});
await new Promise(r => setTimeout(r, 5000));
for(let child of children) {
child.kill();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句