-1

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.

sssssKull
  • 15
  • 6
  • I think you question needs more clarity, otherwise is gonna be downvoted and not resolved. – FN_ Oct 17 '22 at 19:48
  • 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 kind of url it is, if it's just a video or a playlist. – sssssKull Oct 17 '22 at 20:02
  • How about using `try - except` in order to test `video_audio` and than `playlinst_audio`? Are you able to determine which to choose based on the URL? How does the URL looks like? – FN_ Oct 18 '22 at 13:12

1 Answers1

0

Since the URL examples are missing, I am gonna assume that it's not possible to determine whether to use playlist_audio or video_audio based on the URL.

I would suggest using "like" EAFP principle.

Please note, the code below is an example. The purpose is to demonstrate the principle, not provide the exact copy&paste solution. Also, it's not a good practise to use base exceptions and also I am not familiar with the Button object and it's configuration.


def convert():
  try:
      video_audio()
      return True
  except:
      print("Video conversion failed, probably playlist URL")
  
  try:
      playlist_audio()
      return True
  except:
      print("Unknown error. Even playilist ULR conversion failed")

download_btn = Button(screen, text="Download", bg='green', padx='20', pady='5',font=('Arial', 12), fg='#fff', command=convert())

FN_
  • 715
  • 9
  • 27
  • Glad it helped. If my answer was helpful, please mark is as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in. – FN_ Oct 19 '22 at 08:31