fent / node-ytdl-core

YouTube video downloader in javascript.
MIT License
4.48k stars 790 forks source link

recursion Error Too many redirects #564

Closed lkareeml closed 4 years ago

lkareeml commented 4 years ago

When using ytdl inside a function and trying to do recursion and download an array of videos It works fine for some amount of videos and then it throw error Note : It doesn't matter which video in the array, because when I re Run The Code It starts Downloading again normally

A Code example :

const ... = require( '...');    //All the requires
const yotub = "https://www.youtube.com/watch?v=";
const vidFH = {filter:"videoonly",quality:"highestvideo"};
const audFH = {filter:"audioonly",quality:"highestaudio"};
let i = 0;  //recursion counter 
const arr = ['Video1', 'Video2',  ... (More than 20 Videos to download to get the error)]
function doit(i) {
  ytdl(yotub + arr[i], vidFH).pipe(fs.createWriteStream('EP' + i + '.m4v'))
    .on('finish', () => {
      ytdl(yotub + arr[i], audFH ).pipe(fs.createWriteStream('EP' + i + '.m4a'))
        .on('finish', () => {
          console.log("\n Downloading Episode " + i + " is DONE!!");
          if (i < arr.length) {
            i = i + 1;
            doit(i);
          }
        });
    });
}
doit(i);

Code Notes: For Video1 , Video2 you can write YouTube video text , The text after "?v=" for example "C0DPdy98e4c" Where (yotub + arr[i]) == the full YouTube link

For me the error came after 31 videos, after Running again it came after 10 videos

The Error :

events.js:187
      throw er; // Unhandled 'error' event
      ^
Error: Too many redirects
    at ClientRequest.<anonymous> (E:\china wonderful life\node_modules\miniget\dist\index.js:113:42)
    at Object.onceWrapper (events.js:300:26)
    at ClientRequest.emit (events.js:210:5)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:585:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:111:17)
    at HTTPParser.execute (<anonymous>)
    at TLSSocket.socketOnData (_http_client.js:454:22)
    at TLSSocket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:326:12)
    at readableAddChunk (_stream_readable.js:301:11)
Emitted 'error' event on PassThrough instance at:
    at errorOrDestroy (internal/streams/destroy.js:128:12)
    at PassThrough.onerror (_stream_readable.js:783:9)
    at PassThrough.emit (events.js:210:5)
    at PassThrough.<anonymous> (E:\china wonderful life\node_modules\ytdl-core\lib\index.js:133:14)
    at PassThrough.emit (events.js:210:5)
    at ClientRequest.<anonymous> (E:\china wonderful life\node_modules\miniget\dist\index.js:113:28)
    at Object.onceWrapper (events.js:300:26)
    [... lines matching original stack trace ...]
    at HTTPParser.parserOnHeadersComplete (_http_common.js:111:17)

Hope I documented it well first issue to write lol

fent commented 4 years ago

might be a dupe of https://github.com/fent/node-ytdl-core/issues/212

Note : It doesn't matter which video in the array, because when I re Run The Code It starts Downloading again normally

does the error happen on the same video in the array each time? if so, what's the video?

TimeForANinja commented 4 years ago

unable to reproduce, but since i'm already here: a) you can skip that yotub variable and just pass the id to ytdl, should even improve performance b) you should change if (i < arr.length) to if (i <= arr.length) c) your global variable let i = 0 only gets used for the first doit(i) call, the int gets passed by value (not reference like you wrote in your comment) and i = i + 1; only updates the local version - means your global i will forever stay 0 and you could just use doit(0) the first time and doit(i+1) later on

lkareeml commented 4 years ago

might be a dupe of #212

Note : It doesn't matter which video in the array, because when I re Run The Code It starts Downloading again normally

does the error happen on the same video in the array each time? if so, what's the video?

for the error #212 I read it and no information was clarified with the issue

no, It doesn't happen in the same video, I downloaded the full series just by this script, only when it repeats many times it just happens

lkareeml commented 4 years ago

unable to reproduce, but since i'm already here: a) you can skip that yotub variable and just pass the id to ytdl, should even improve performance

That's right and I didn't realize that it will improve performance, Thank you

b) you should change if (i < arr.length) to if (i <= arr.length)

To cover the full array! Thank you again!

c) your global variable let i = 0 only gets used for the first doit(i) call, the int gets passed by value (not reference like you wrote in your comment) and i = i + 1; only updates the local version - means your global i will forever stay 0 and you could just use doit(0) the first time and doit(i+1) later on

This is really a new information to me, it was really a stupid from me to do a global variable

I Really Appreciate your tips, Thank you so much for tips and advice, I will do so in further projects for the code error to be produced This is the full code

const readline   = require('readline');
const fs         = require('fs');
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg     = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(ffmpegPath);
const ytdl       = require( 'ytdl-core');

const vidFHOption = {filter:"videoonly",quality:"highestvideo"};
const audFHOption = {filter:"audioonly",quality:"highestaudio"};

const arr = [
            "mZXwg1t8jq8","rLKMoaqBX-0","a8AGreEQ2Cc","vLpjzCGjces",
            "Bh7YKvLBZ4U","n7Vv7r7zS-c","EnkRJ5JSMN0","l5eQLAKq7Qg",
            "Qhio7I8L6bs","1wd6bcoYaNk","_QIjU1KCjuU","nfOe_g5va2U",
            "E3Mb5EccHqM","qva0bWuKtwQ","q7vJYSWJ7Pc","TH2ezcduskk",
            "rK5sEP0ZFUE","XxI4VwewmPg","JB8d39D71Gs","HkupQXkx8hk",
            "o-vgQFtjBZQ","6SbgmNVrdM8","eIcOujrFyII","06GNCzKWF3M",
            "rYjJz177bb0","mtcmYQ4rUYM","wy_0sj56lPI","5yZ6pUCneWs",
            "3fnnpKHhld8","UswF0NTZbPM","PvxK95bp74M","JRFnDMUHdrw",
            "cvmfkuuslQs","KC-mhZz7n6o","RVTEHWUts2w","1pt6lAeuzFI",
            "Ve7e94SM8mo","glKnCp5hl-A","5j1stlT9Mp0","DuSJmGiP1sA",
            "67MLCmZ_pOc","VDhKqt0Sx58","qz_xALU-xtw","zPfykU9mm8A",
            "pXZo0gcrQvs","XQWaXmVwaIg"
]

function doit(i) {
  console.log("START Downloading Episode " + i + "\n");
  ytdl("https://www.youtube.com/watch?v=" + arr[i], vidFHOption).on('progress', (chunkLength, downloaded, total) => {
      const percent = downloaded / total;
      readline.cursorTo(process.stdout, 0);
      process.stdout.write(`${(percent * 100).toFixed(2)}% downloaded `);
      process.stdout.write(`(${(downloaded / 1024 / 1024).toFixed(2)}MB of ${(total / 1024 / 1024).toFixed(2)}MB)`);
  }).pipe(fs.createWriteStream('EP' + i + '.m4v')).on('finish', () => {
      ytdl("https://www.youtube.com/watch?v=" + arr[i], audFHOption).on('progress', (chunkLength, downloaded, total) => {
          const percent = downloaded / total;
          readline.cursorTo(process.stdout, 0);
          process.stdout.write(`${(percent * 100).toFixed(2)}% downloaded `);
          process.stdout.write(`(${(downloaded / 1024 / 1024).toFixed(2)}MB of ${(total / 1024 / 1024).toFixed(2)}MB)`);
      }).pipe(fs.createWriteStream('EP' + i + '.m4a')).on('finish', () => {
          console.log("\n Downloading Episode " + i + " is DONE!!");
          if (i <= arr.length) 
              doit(i + 1);
      });
  });
}
doit(0);
TimeForANinja commented 4 years ago

ran into the same problem, looks like some generic network problems to me; just retrying the download always fixed it (at least on my machine)

fent commented 4 years ago

going to mark this as a dupe of https://github.com/fent/node-ytdl-core/issues/212 to keep things organized