Remove the name of the file in Python3

0

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.

    
asked by Raplh Wigum 13.11.2018 в 12:34
source

1 answer

0

Since you are delegating in youtube-dl the download of the video, you can take and pass to that command the options --extract-audio and --audio-format , and in this way you avoid the problem that you are posing, while avoiding the dependency of the library ffmpy (although you will need to have installed ffmpeg anyway, because youtube-dl uses it).

That is, you could do something like this:

# Omitida la parte en que se busca la url del vídeo o se le pide al usuario
# Se supone que en la variable 'url' ya la tenemos
convertir = input("Quedarse sólo con el audio? [S/n]: ").lower()
extra = ""
if convertir == "s" or convertir == "":
    formato = input("Formato del audio? [mp3]: ").lower()
    if formato == "":
        formato = "mp3"
    extra = "--extract-audio --audio-format {}".format(formato)

comando = "youtube-dl.py {} -o %\(title\)s.%\(ext\)s {}".format(url, extra)
subprocess.call(comando, shell=True)
print("Gracias por usar el programa")

The formats you can request from youtube-dl are "aac", "flac", "mp3", "m4a", "opus", "vorbis", or "wav".

    
answered by 13.11.2018 / 14:06
source