I created a youtube downloader that converts videos into .mp3. Moreover, I added the possiblity to name the artist and the song and state a directory into which the file is downloaded.
Everything works fine but the file is always saved into Python's current working directory and not into the selected path file_loc
.
I also noticed removing this part (which is intended to name the file) from the downloader
-function causes Python to output the file in the correct directory.
global label_artist, label_song
name = label_artist.get() + " - " + label_song.get()
print(name)
Can someone please help me?
import pytube
from pytube import YouTube
from tkinter import *
from tkinter import filedialog
import os
#-- creating window for input
# window is named "root"
root = Tk()
root.geometry("500x400") # sets size of window
root.title("Youtube Videokonvertierer") # sets title window
root.configure(bg = "white smoke") # sets background color of window
Label(root, text = "Youtube Videokonvertierer", font = "arial 20 bold").pack()
#-- defining labels and entry fields for customized name of video
label_artist = StringVar()
label_song = StringVar()
artist_enter = Entry(root, width = 30, textvariable = label_artist).place(x = 32, y = 140)
song_enter = Entry(root, width = 30, textvariable = label_song ).place(x = 270, y = 140)
Label(root, text = "Künstler").place(x = 32, y = 119)
Label(root, text = "Lied" ).place(x = 270, y = 119)
#-- creating field to select download location
# checks if "file_loc" is assigned, if so it shows the path otherwise "Bitte Speicherort auswählen" is shown
try:
text_dir = file_loc
except:
text_dir = "Bitte Speicherort auswählen"
# create label that shows either "file_loc" or "Bitte Speicherort auswählen"
label_dir = Label(root, text = text_dir)
label_dir.place(x = 32, y = 185)
# function is later executed via button, enables to select file location
def browse_file():
global file_loc # saves location of file as global variable (to make it accessible in further code)
file_loc = filedialog.askdirectory() # chooses file location
label_dir.configure(text = file_loc) # updates the label with the selected path (https://stackoverflow.com/questions/28303346/how-to-make-a-tkinter-label-update)
# button to select file path, automatically triggers Label() in browse_file function and updates it with the new path
Button(root, text = "Speicherort wählen...", font = "arial 15 bold", bg = "light steel blue", padx = 2, command = browse_file).place(x = 180, y = 210)
#-- creating field to enter youtube link
link = StringVar() # creates variable to hold link
Label(root, text = "Link hier einfügen:", font = "arial 15 bold").place(x = 160, y = 60) # labels field to enter link
link_enter = Entry(root, width = 70, textvariable = link).place(x = 32, y = 90) # creates entry field to input link
#-- download from youtube
def downloader():
url = YouTube( str(link.get()) )
video = url.streams.filter(only_audio = True).first()
print("Downloading...")
downloaded_file = video.download(file_loc) # downloads video from youtube
base, ext = os.path.splitext(downloaded_file) # splits file name and file extension
global label_artist, label_song
name = label_artist.get() + " - " + label_song.get()
print(name)
new_file = name + '.mp3' # pastes file name and ".mp3" together (converts video file into .mp3 file)
os.rename(downloaded_file, new_file) # renames file with ".mp3" label
print("Done")
os.system(f"start {os.path.realpath( file_loc )}") # opens file location after video was downloaded and saved as mp3
# inside of os.system() is path outputted
Button(root, text = "DOWNLOAD", font = "arial 15 bold", bg = "deep sky blue", padx = 2, command = downloader).place(x = 180, y = 270)
root.mainloop()
Problem is that you create new_file
without file_loc
... and because os.rename
can move file to other folder if you use different folders in source and destination path.
Code video.download(file_loc)
downloads file to expected folder file_loc
but when you run os.rename(downloaded_file, new_file)
then new_name
is without file_loc
and rename
moves it to current working directory.
You should create
new_file = os.path.join(file_loc, name + '.mp3')
You can see this problem if you use
print(downloaded_file)
print(new_file)
downloaded_file
will have /full/path/to/file_loc/movie.mp4
but new_file
will have only artist - song.mp3