distubejs/ytdl-core

File downloads without sound

Closed this issue · 1 comments

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

  • @distube/ytdl-core version: 4.14.4
  • Node.js version: v22.0.0
  • Operating system: Windows 11 PRO

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