algolia / youtube-captions-scraper

Fetch youtube user submitted or fallback to auto-generated captions
249 stars 67 forks source link

How to avoid error when there are no captions in YouTube videos? #15

Open andrii1 opened 2 years ago

andrii1 commented 2 years ago

Some videos don't have autogenerated captions from YouTube. Then this code throws error. Is there a way to avoid error or to have some if statement etc. Sorry, I'm not experienced in js.

ErrorCould not find captions for video: ejSm7B2gyk8 at null.null (/opt/ee/node_modules/youtube-captions-scraper/dist/index.js:21:48) at Generator.next (null:null:null) at null.step (/opt/ee/node_modules/youtube-captions-scraper/dist/index.js:103:191) at null.null (/opt/ee/node_modules/youtube-captions-scraper/dist/index.js:103:361) at process.processTicksAndRejections (internal/process/task_queues.js:95:5)

Joery-M commented 2 years ago

The best you can do for now is go into the index.js node_modules/youtube-captions-scraper/dist/index.js and replace 'throw new Error(Could not find captions for video: ${videoID});' with return undefined (Row 21)

You can do the same on row 38, if you also don't want the code to exit when it can't find captions in the right language

Note: if you edit the node_modules directly, your changes will get discarded when you run npm i or anything that checks all modules.

I've made a custom version of the module that has an extra export called hasSubtitles. it will return true if subtitles exist. It is a lot faster than the normal getSubtitles since it isn't getting all subtitles, but it doesn't give you the subtitles youtube-captions-scraper_Edited.zip

Don't put this in node_module, it's not an official module.

I would submit this as a pr but I don't know enough about git to do this.

Edit: If you're using the edited file, don't forget to run npm i in the folder where you put it

andrii1 commented 2 years ago

Thank you! Any advice maybe how to not change anything here, but just in my code - so I avoid an error when there are no captions?

andrii1 commented 2 years ago

Ok, seems it can be solved with adding this: .catch(error => { //function if there is an error });

Joery-M commented 2 years ago

Like usual, I'm overthinking it. That's a lot better.