I have the following code in a route handler
import { NextRequest, NextResponse } from "next/server";
import ytdl from "ytdl-core";
export async function POST(request: NextRequest){
try {
const {url_video, formatArchive, quality} = await request.json()
const response = NextResponse.next()
const title = (await ytdl.getInfo(url_video)).player_response.videoDetails
if(!url_video){
return NextResponse.json({error: "Url não inserida"})
}
if(!quality){
return NextResponse.json({error: "Formato do arquivo não inserido"})
}
if(formatArchive == "video"){
const video = ytdl(url_video, {filter: format => format.qualityLabel === quality})
response.headers.set(`Content-Disposition`, `attachment; filename=${title}.mp4`);
response.headers.set('Content-Type', 'video/mp4');
video.pipe(response)
return
}
const video = ytdl(url_video, {filter: format => format.audioBitrate === quality})
response.headers.set(`Content-Disposition`, `attachment; filename=${title}.mp3`);
response.headers.set('Content-Type', 'audio/mpeg');
video.pipe(response)
} catch (error) {
return NextResponse.json({error: `Erro no download ${error}`}, {status: 500})
}
}
This code even somehow downloads the file, however, it doesn't download in the right size, and it happens that when I listen to the file, it appears that the file is corrupted, because a 120kbps mp3 file it downloads in 1kb.
In the video.pipe part, it gives this error:
This code even somehow downloads the file, however, it doesn't download in the right size, and it happens that when I listen to the file, it appears that the file is corrupted, because a 120kbps mp3 file it downloads in 1kb, why would it be? because this is a typing error, I don't know if that's why, but even with this error, the code runs perfectly.