I was working on a Project where I’m attempting to download a video file, audio file, and a merged version into a single complete video using ytdl-core.
But while downloading video file only it give error in console:
Error in video stream: Status code: 403 Error: Status code: 403
I expect to get: video file, audio file and merged file containing both video and audio.
Instead of the expected files, receive an empty video file with the name <video_title>_video.webm
Here’s the relevant part of code:
try {
createDirectoryIfNotExists('./Downloaded Videos');
const videoInfo = await getInfo(url);
const videoTitle = videoInfo.videoDetails.title;
const sanitizedTitle = removeSpecialCharacters(emojiStrip(videoTitle));
console.log(yellow(`nBaixando vídeo: ${videoTitle}`));
const videoFilePath = `./Downloaded Videos/${sanitizedTitle}_video.webm`;
const audioFilePath = `./Downloaded Videos/${sanitizedTitle}_audio.m4a`;
const outputFilePath = `./Downloaded Videos/${sanitizedTitle}.mp4`;
if (existsSync(outputFilePath)) {
console.error(red(`nArquivo "${outputFilePath}" já existe. Pulando.`));
return;
}
console.log(yellow(`Downloading video stream...`));
const videoFormat = chooseFormat(videoInfo.formats, { quality: 'highestvideo' });
const videoStream = downloadFromInfo(videoInfo, { format: videoFormat });
videoStream.on('error', (error) => {
console.error(red(`Error in video stream: ${error.message}`,error));
});
videoStream.pipe(createWriteStream(videoFilePath));
// await new Promise((resolve) => videoStream.on('end', resolve));
await new Promise((resolve, reject) => {
videoStream.on('end', () => {
console.log(green(`Video stream download completed.`));
resolve();
});
videoStream.on('error', reject);
});
//rest of the code for download audio file and then next..........
} catch (error) {
console.error(red(`Erro ao processar o vídeo: ${error}`));
}
The necessary imports are as follows::
import { existsSync, mkdirSync, createWriteStream, unlinkSync } from 'fs';
import ytdl from 'ytdl-core';
const { getInfo, chooseFormat, downloadFromInfo, validateURL } = ytdl;
import chalk from 'chalk';
const { yellow, red, green } = chalk;
import emojiStrip from 'emoji-strip';
import { exec } from 'child_process';
import ffmpeg from 'ffmpeg-static';
import clipboardy from 'clipboardy';
const { writeSync } = clipboardy;
Is this issue occurring because the URL in videoFormat is unreachable and returns a 403 error? How can I resolve this issue, and is there an alternative approach to downloading videos using ytdl-core? Any suggestions would be greatly appreciated!