I'm trying to create a video/playlist to mp3 converter, but I don't know how to handle python for now. I would need a little help.
When I insert just a video link it downloads and converts normally, but when I insert a playlist link it doesn't download because the command goes to the download command for a single video.
I would like to know a way to use (if - else) or something similar to identify that it is a playlist and vice versa.
The code I'm using is this:
from pytube import YouTube, Playlist
from tkinter import *
from moviepy import *
import os
import re
if os.name == "nt":
folder = f"{os.getenv('USERPROFILE')}\\Downloads"
else:
folder = f"{os.getenv('HOME')}/Downloads"
def playlist_audio():
get_link = link_field.get()
p = Playlist (get_link)
p._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*a)")
for video in p.videos:
mp3_audio = video.streams.filter(only_audio=True).first().download(folder)
audioconvert(mp3_audio)
#
screen.title('OK!')
def video_audio():
get_link = link_field.get()
screen.title('Downloading..')
mp3_audio = YouTube(get_link).streams.filter(only_audio=True).first().download(folder)
audioconvert(mp3_audio)
#
screen.title('OK!')
def audioconvert(mp3_audio):
base, ext = os.path.splitext(mp3_audio)
new_file = base + '.mp3'
os.rename(mp3_audio , new_file)
screen = Tk()
title = screen.title('Python Youtube Converter Test')
Canvas = Canvas(screen, width=400, height=200)
Canvas.pack()
link_field = Entry(screen, width=50)
link_label = Label(screen, text="Insert the url of the song or playlist:")
Canvas.create_window(200, 90, window=link_field)
Canvas.create_window(200, 70, window=link_label)
#
download_btn = Button(screen, text="Download", bg='green', padx='20', pady='5',font=('Arial', 12), fg='#fff', command=lambda:[video_audio(),playlist_audio()])
#
Canvas.create_window(200, 140, window=download_btn)
screen.mainloop()
When I insert the playlist link, the process ends up not happening (download and convert) because the command goes to the "video_audio" definition and not to the "playlist_audio" which ends up generating an error. I wanted to know a way when clicking on download, the program identify what type of url it is, if it's just a video or a playlist and thus execute the correct command.