Python Youtube-dl - keep terminal / tkinter window open after executing

624 views Asked by At

I'm using tkinter and youtube-dl under python

def dl(lk,fl): # link / file location
    opts=['-o', fl+'/%(title)s-%(id)s.%(ext)s', '--playlist-end', '20', '--extract-audio', '--audio-   format', 'mp3', '--audio-quality', '9', '--write-thumbnail', lk]
    youtube_dl.main(opts)
    return "Successful download"

This is the basic function which using the library, nothing extraordinary.

I call it from tkinter, so when I run it, the download start and finish. After, it close my tkinter windows.

Here, the function which call the first function in a tkinter class.

def dlv(self):
    self.url = self.iurl.get()
    if not self.url:
        self.msg = "Error : no url"
    elif not urlregex.match(self.url):
        self.msg = "Error : invalid url"
    else:
        self.msg = dl(self.url,filel)
    self.Com()

I try to stop by calling some functions as input or raw input. I also looked for option from the youtube dl librarie.

Nothing has work well.

Thanks

1

There are 1 answers

0
Himal On BEST ANSWER

I'm not familiar with youtube_dl. but, i think it calls sys.exit at the end.Try subprocess.Popen instead.

Something like,

import subprocess

def dl(lk, fl):
    opts = ['-o', fl + '/%(title)s-%(id)s.%(ext)s', '--playlist-end', '20',
            '--extract-audio', '--audio-   format', 'mp3', '--audio-quality',
            '9', '--write-thumbnail', lk]

    args = ['python', '-m', 'youtube_dl']
    args.extend(opts)

    p = subprocess.Popen(args) # You can use subprocess.PIPE to redirect stdout & stderr. Read the doc for more info.
    p.communicate() # Wait for process to terminate.

    if p.returncode == 0: # You might wanna check for correct successful return code.
        return 'Successful download'
    else:
        return 'Download Failed'

Note that i didn't test this code.also this will hang the main tkinter window.you might wanna call this in a separate thread/process.