Rafalpero
Rafalpero

Reputation: 97

How to change directory youtube_dl in python

I'm new into programming, and i'm making a simple mp3 downloader from youtube. And theres my question i cant realize how can i change the filedialog.askdirectory() directory to a downloaded music. Where i can define the directory, please check below:

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import youtube_dl

import sys

root=Tk()
root.resizable(0,0)


def save():
    messagebox.showinfo('Wybór', 'Wybierz folder docelowy')
    directory=filedialog.askdirectory()
        ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([e1.get()])


    messagebox.showinfo('Zakonczone','Pobieranie zakonczone')



def exit():
    sys.exit()


root.title('Youtube downloader')
l1=Label(root,text='Wpisz adres url filmu Youtube',font=20)
l1.pack()
e1=Entry(root,width=50)
e1.pack()
b1=Button(root,text='Pobierz',width=7,height=2,command=save)
b1.pack(side=LEFT)
b2=Button(root,text='Wyjdź',command=exit,width=7,height=2)
b2.pack(side=RIGHT)




root.mainloop()

Upvotes: 1

Views: 1759

Answers (1)

arshovon
arshovon

Reputation: 13661

To download the video in a specified folder use outtmpl in the configuration.

I have created a folder named downloaded_music and updated your code as following:

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import youtube_dl

import sys

root=Tk()
root.resizable(0,0)

def save():
    #messagebox.showinfo('Wybór', 'Wybierz folder docelowy')
    #messagebox.showinfo('Folder Selection', 'Select download folder')
    #directory=filedialog.askdirectory()
    directory = 'downloaded_music'
    ydl_opts = {
        'outtmpl': 'downloaded_music/%(title)s-%(id)s.%(ext)s',
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([e1.get()])
    #messagebox.showinfo('Zakonczone','Pobieranie zakonczone')
    messagebox.showinfo('Success','Download completed')

def exit():
    sys.exit()

if __name__ == '__main__':
    root.title('Youtube downloader')
    #l1=Label(root,text='Wpisz adres url filmu Youtube',font=20)
    l1=Label(root,text='Download MP3 from Youtube video',font=20)
    l1.pack()
    e1=Entry(root,width=50)
    e1.pack()
    #b1=Button(root,text='Pobierz',width=7,height=2,command=save)
    b1=Button(root,text='Download',width=7,height=2,command=save)
    b1.pack(side=LEFT)
    #b2=Button(root,text='Wyjdź',command=exit,width=7,height=2)
    b2=Button(root,text='Exit',command=exit,width=7,height=2)
    b2.pack(side=RIGHT)
    root.mainloop()

Now the downloaded files are stored in the downloaded_music folder.

Upvotes: 1

Related Questions