distubejs / ytdl-core

YouTube video downloader in javascript.
MIT License
206 stars 43 forks source link

File downloads without sound #89

Open adize2000 opened 1 month ago

adize2000 commented 1 month ago

Describe the bug

In my download.js file below i define an API to save a YT file locally, however the file has no sound each time:

async function handler(req, res) { if (req.method === 'GET') { const videoId = req.query.videoId; //Video ID of the video to download

try {
    const fs = require('fs');
    const ytdl = require('@distube/ytdl-core');
    //const ytdl = require('ytdl-core');
    const path = require('path');
    const currentProjectPath = __dirname;

    console.log("Saving: "+videoId);

    ytdl("https://www.youtube.com/watch?v="+videoId).pipe(
      fs.createWriteStream(currentProjectPath+"../../../../../downloads/"+videoId+'.mp4'));

    res.writeHead(200, {"Content-Type": "text/plain"});
    res.end(JSON.stringify({}));
} catch (error) {
  console.error('Error handling request:', error);
  res.status(500).json({ error: 'An error occurred' }); // Send generic error response
}

} else { res.status(405).json({ error: 'Method not allowed' }); // Handle unsupported methods } }

export default handler;

Debug File

Saving: 5O...cI (node:22532) [DEP0040] DeprecationWarning: The punycode module is deprecated. Please use a userland alternative instead. (Use node --trace-deprecation ... to show where the warning was created) GET /api/download?videoId=5O...cI 200 in 234ms

Environment

phucngo2 commented 4 weeks ago

Youtube separates the audio and video streams for efficient media streaming.

If you want a high quality video with audio, you should use FFmpeg to combine them: https://github.com/fent/node-ytdl-core/blob/master/example/ffmpeg.js