Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resume downloading consequent videos when youtubeDL fails to download a video

I'm trying to develop a python script to download a bunch of youtube videos which are inputted from a csv file using the youtube-dl module. The script stops as the download fails for a single video. I need the script to continue downloading the next videos ignoring the videos that cannot be downloaded. Can some one please help me on how to handle this in python?

like image 606
Divya Avatar asked Jan 06 '23 17:01

Divya


2 Answers

If you are using the youtube-dl command you can use the --ignore-errors flag.

If you are using the youtube_dl module from python you have to use the ignoreerrors parameter:

from __future__ import unicode_literals
import youtube_dl

ydl_opts = {'ignoreerrors': True}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['<URL>'])
like image 128
jaimeMF Avatar answered May 11 '23 18:05

jaimeMF


I realize this is an old question but the problem cropped up for me and I found a simple solution.

I think the problem here is that youtube_dl is catching any errors that come up and then reporting them by way of it's own internal message output routines, so the exception is 'consumed' before it gets to the calling code. This is probably appropriate for the command line utility but creates a hurdle when using it in native python code. Fortunately the developers of youtube_dl provide us a way to intercept error messages and handle them without the need to jack around with the youtube_dl internals. Unfortunately they're pretty light on both explanations and examples, but if you check the 'developer instructions' of the readme there's an example of a custom logger class.

With a little modification, we can use this class to store error messages (or warnings, or whatever) and then check for them in lieu of using a try/except block:

class MyLogger(object):

def __init__(self):
    self._message_queue = []

def debug(self, msg):
    print('[DEBUG]' + msg)

def warning(self, msg):
    pass

def error(self, msg):
    self._message_queue.append(msg)
    print(msg)

def get_message(self):
    return None if not len(self._message_queue) else \
           self._message_queue.pop()

Import the logger class, instantiate it, and pass it as an argument in your options string for youtube_dl.

import mylogger
...
loggr = mylogger.MyLogger()
...
ydl_opts = { 'logger': loggr }

Now, when it's time to take an action based on the presence of an error, you can check the message queue in your logger object to see if there's something to do:

possible_error = loggr.get_message()
if possible_error is None:
    #do normal code
else:
    #check the content of the error string and do something with it.

Hope this helps someone.

like image 27
nsand Avatar answered May 11 '23 17:05

nsand