Merging Resources and Responding in Streaming Mode
In this example, three video clips are merged into one video, and the merged video is played on a client based on the order in which the video clips are merged. This example demonstrates how to use an edge function to fetch multiple remote resources, read and merge the resources in streaming mode, and respond to a client request by using the merged resource in streaming mode.
console.error(`Merging video streams error: ${err.message}`);
}finally{
// Close the stream after all fragments are processed
const writer = destination.getWriter();
writer.close();
writer.releaseLock();
}
}
asyncfunctionhandleRequest(request){
// The URLs of the video clips.
const urls =[
'https://vod.example.com/stream-01.mov',
'https://vod.example.comm/stream-02.mov',
'https://vod.example.com/stream-03.mov',
];
// Creating a Transformation Flow
const{ readable, writable }=newTransformStream();
// Get and merge video clips in sequence
sequentialCombine(urls, writable);
// Returns the merged video stream response
returnnewResponse(readable,{
headers:{
'content-type':'video/mp4',
}
});
}
// Listening for fetch events
addEventListener('fetch', event =>{
event.respondWith(handleRequest(event.request));
});
Sample Preview
In the address bar of the browser, enter a URL that matches a trigger rule of the edge function to preview the merged video. View the response header and verify that the video is transferred in chunked mode.