I am trying to pipe the output of youtube-dl-exec to the front end but I cant seem to figure out how.
export default async function convertAndDownload(req: Request, res: Response) {
try {
const { video, meta, filename } = req.body;
res.header("Content-Disposition", `attachment; filename="${filename}"`);
const videoUrl = `https://youtube.com/watch?v=${video.id}`;
const cmd = youtubedl.exec(videoUrl, {
extractAudio: true,
audioFormat: "mp3",
audioQuality: "10",
output: "-",
postprocessorArgs: [`"ffmpeg: -metadata title='${meta.title}'"`],
addHeader: ["referer:youtube.com", "user-agent:googlebot"],
});
cmd.stdout.pipe(res, { end: true });
} catch (err) {
res.status(500).json({
success: false,
msg: "There was an error during processing.",
});
}
}
This actually works but the output is not mp3 it is webm so I’m assuming I’m just getting the stream and not the processed file. If I remove the pipe and just directly download it via youtubedl without exec like :
const cmd = youtubedl(videoUrl, {
extractAudio: true,
audioFormat: "mp3",
audioQuality: "10",
output: "output.mp3",
postprocessorArgs: [`"ffmpeg: -metadata title='${meta.title}'"`],
addHeader: ["referer:youtube.com", "user-agent:googlebot"],
});
Then it works fine. So how can I correctly pass the fully processed file stream in mp3 format back to the front end in the res? Help would be appreciated I’m sure I’m missing something simple but not sure what. Thanks