Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit youtube-dl on video length when downloading a youtube stream

Im trying to write a small script that records a live stream from youtube. when using youtube-dl.download function it just keeps on downloading until the stream ends, but i want to stop downloading after 20min or so. the problem is when i try killing it after a while the output is corrupted (im using mp4 format), and i tried fixing it with ffmpeg but a "moov atom not found" error occurs.

How can i make youtube-dl (or any other tool) record a fixed length of a live stream?

Here's a portion of the code:

class recordingThread (threading.Thread):
def __init__(self, threadID, output_location, name, yt_stream, start_time):
    threading.Thread.__init__(self)

    self.threadID = threadID
    self.output = os.path.abspath(output_location)
    self.name = name
    self.start_time = start_time

    self.ydl_opts = {'quiet': True,
                     'format': 'mp4',
                     'outtmpl': name+ '%(ext)s',
                     'sleep_interval': 2
                     }
    self.yt_stream = yt_stream.strip('\'"')

def run(self):
    print "Starting %s Thread Recorder - %s" % (self.name, self.start_time)

    with youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
        self.download_prc = ydl.download([self.yt_stream])

Thanks.

like image 891
M B Avatar asked Nov 09 '22 11:11

M B


1 Answers

here is how I rm file > 20min:

        dictMeta = ydl.extract_info(
        "https://www.youtube.com/watch?v={sID}".format(sID=sID),
        download=False)

    # Duration > Max duation ?
    iDuration = dictMeta["duration"]
    if iDuration > iMaxDuration:
        print(
            "[Log] Too long to download, %ds > %ds" %
            (iDuration, iMaxDuration))
        return ""
like image 154
Simon Avatar answered Nov 14 '22 22:11

Simon