Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Youtube video downloadet in next.js

$
0
0

Here is the api endpoint:

import ytdl from 'ytdl-core';import { NextApiRequest, NextApiResponse } from 'next';export default async (req: NextApiRequest, res: NextApiResponse) => {  const { url = '' }: { url?: string } = req.query;  const video = ytdl(url, { filter: 'audioonly' });  res.setHeader('Content-Disposition', 'attachment; filename="video.mp4"');  res.setHeader('Content-Type', 'audio/mp4');  video.pipe(res);}

here is the front end function

const handleDownload = async (url: string) => {  try {    const response = await axios.get(`/api/downloader?url=${url}`, {      responseType: 'blob'    });    const blob = new Blob([response.data], { type: 'audio/mp4' });    const downloadUrl = window.URL.createObjectURL(blob);    const link = document.createElement('a');    link.href = downloadUrl;    link.download = 'audio.mp4';    document.body.appendChild(link);    link.click();    document.body.removeChild(link);    window.URL.revokeObjectURL(downloadUrl);  } catch (error) {    console.error('Download failed:', error);    // Handle error (e.g., show error message to user)  }};

how can I handle the response in the front end? I am getting this error:

enter image description here

I think the issue is the API endpoint what did I get wrong?


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>