I am working on a program which downloads YouTube videos and converts them to an audio file of any format, either by giving a link or a search term and I have a problem with collecting the name of the downloaded file, since it can be downloaded in various video formats (mainly mkv, mp4, webm).
The only thing I get is that the name matches the name of the video from the page but I still lack the output format. So far I have that part of the code like this:
from bs4 import BeautifulSoup
import urllib.request, urllib.parse
import subprocess
import ffmpy
import requests
def name_generator(term):
url = term
webpage_url = "http://www.youtube.com/results?search_query=" + url.replace(" ", "+")
webpage_url = urllib.parse.quote(webpage_url, safe='/:?+=', encoding="utf-8", errors="strict")
webpage_request = urllib.request.urlopen(webpage_url)
status_code = webpage_request.getcode()
if status_code == 200:
list_of_names = []
html = BeautifulSoup(webpage_request, "html.parser")
videos = html.find_all('div', {'class': 'yt-lockup-content'})
for i, video in enumerate(videos):
if len(list_of_names) < 5:
name = video.find('a')['title']
formatted_text = "{0}".format(name)
list_of_names.append(formatted_text)
return list_of_names
url = input("Introduce la URL: ")
comando = "youtube-dl {} -o %(title)s.%(ext)s".format(url)
subprocess.call(comando, shell=True)
question = input("Quieres convertir el audio?[S/n]: ").lower()
if question == "s":
request = input("En que formato lo quires?: ")
nombre = name_generator(url)[0]
try:
ff=ffmpy.FFmpeg(
inputs={"{}.mp4".format(nombre): None},
outputs={"{}.{}".format(nombre, request): None}
)
ff.run()
except:
ff=ffmpy.FFmpeg(
inputs={"{}.webm".format(nombre): None},
outputs={"{}.{}".format(nombre, request): None}
)
ff.run()
else:
print("Gracias por usar el programa")
I just need the program to handle the entry of the videos in the 3 main formats that I mentioned earlier and I would have finished it. If someone made a cable I would be grateful.