Below is my code to download a YouTube video and then converting it into an audio file. But while renaming it, I am getting an error. Please explain my problem and please provide me a solution.
import os
import tkinter as tk
from tkinter import ttk
from pytube import YouTube
from tkinter.filedialog import askdirectory
from tkinter.messagebox import askyesno, showinfo
This function gives me problem while I am using it to rename my file.
def rename_file(file):
def return_val():
print(new_name.get())
top.destroy()
return(new_name.get())
print(file)
top = tk.Toplevel(root)
top.title("Rename File")
top.iconbitmap('icon.ico')
top.geometry('400x130')
tk.Label(top, text='Enter new name for the file: ', font='comicsansms 12').grid(row=0, column=0)
new_name=tk.StringVar()
new_name.set(file)
name_entry = ttk.Entry(top, textvariable=new_name, font='comicsansns 12')
name_entry.focus_force()
name_entry.grid(row=0, column=1, pady=5)
ttk.Button(top, text='Rename!', command=return_val, width=8).grid(row=3, column=1, pady=18, ipady=9)
This is the main function to download a video from YouTube.
def download_audio():
try:
global link_var
global directory
link = link_var.get()
yt = YouTube(link)
title_lable = ttk.Label(root, text=f"Title: {yt.title}", font='lucida 12')
title_lable.grid(row=3, column=0, pady=9, padx=4)
confirmation = askyesno('Check Title', "Does the title match the video you wanna download?")
if confirmation:
video = yt.streams.filter(only_audio=True).first()
video_file = video.download(output_path=directory)
base, ext = os.path.splitext(video_file)
audio_file = base + '.mp3'
rename = askyesno('Rename File', 'Do you want to rename this file?')
if rename:
Before this function ends,
obj = rename_file(audio_file.split('\\')[-1])
it moves forward to next line.
audio_file = os.path.join(directory, obj)
print(audio_file)
os.rename(video_file, audio_file)
showinfo("Downloaded", "Your Audio file is downloaded")
link_var.set("")
title_lable.config(text="")
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "f:\Eriq\PYTHON\YT to Mp3 Converter\yttomp3.py", line 53, in download_audio
audio_file = os.path.join(directory, obj)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\ntpath.py", line 115, in join
genericpath._check_arg_types('join', path, *paths)
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\genericpath.py", line 149, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'NoneType'