async function combine(readableVideos, destination) {
for (const readable of readableVideos) {
await readable.pipeTo(destination, {
preventClose: true
});
}
const writer = destination.getWriter();
writer.close();
writer.releaseLock();
}
async function handleRequest(request) {
const urls = [
'https://laputa-1257579200.cos.ap-guangzhou.myqcloud.com/stream-01.mov',
'https://laputa-1257579200.cos.ap-guangzhou.myqcloud.com/stream-02.mov',
'https://laputa-1257579200.cos.ap-guangzhou.myqcloud.com/stream-03.mov',
];
const requests = urls.map(url => fetch(url));
const responses = await Promise.all(requests);
const readableVideos = responses.map(res => res.body);
const { readable, writable } = new TransformStream();
combine(readableVideos, writable);
return new Response(readable, {
headers: {
'content-type': 'video/mp4',
}
});
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
Was this page helpful?