0
votes

Python noob :(

I created a script that downloads any youtube video.

Then the script inserts a subtitle into the video and that works fine.

However, I have a problem, I can't make the script always download the video in mp4. Most of the time it is downloaded in mkv and i can't understand why that happens and how can I change the code in order to always download the video in the format I prefer.

The function in charge to download the video is this one:

def video_download():
    #global resolution
    try:
        print(linkvideo)
        ydl_opts = {
            'format': 'bestvideo[ext=mp4]+bestaudio/best',
            'outtmpl': video_id+"."+'%(ext)s'
            #'bestvideo': 'mp4',
            #'bestaudio': 'm4a',
            #'ext': 'mp4'
        }
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([linkvideo])
            result = ydl.extract_info("{}".format(linkvideo))
            title = result.get("id", None)
            videoext = result.get("ext", None)
            resolution = result.get("resolution", None)
    except:
        print("falhou")
        pass
    global ficheiro
    ficheiro2 = str(title)+"."+"mkv"
    ficheiro =""
    for i in ficheiro2:
        if (i == '"' or i =="'" or i =="/"):
            pass
        else:
            ficheiro += i

Can any one help me? Thanks in advance.

I got it, thanks to everyone who tried to help me.

The problem is this: when we try to download any video with the opts i've selected, youtube-dl will download the best video/audio combination in the same file, so I've added the following in my code:

ytdl = YoutubeDL()
    result = ytdl.extract_info(linkvideo, download=False)
    formats = result['formats']
    formato = ""
    formatoaudio =  ""
    extaudio = ""
    for format in formats:
        if format['format_note'] == "1080p" and format['ext'] == "mp4":
            print(format)
            formato = (format['format_id'])
            break
        else:
            print("no 1080p mp4?!")

    if formato == "":
        for format in formats:
            if format['format_note'] == "720p" and format['ext'] == "mp4":
                print(format)
                formato = (format['format_id'])
                break
            else:
                print("no 720p mp4")

Then I did the same to the sound file, checking if there is any m4a or webm sound file. To finish I'm joining both files using FFmpeg.

Maybe this is not the best solution, but I'm still learning. :)

Thank you all

1
Issue here is that some videos do not have certain formats available. So you might want to first check the formats available with youtube-dl -F url, this will give you the formats available for that video. Although, you can also just download those with not mp4 versions and convert it with ffmpeg to your desire format (youtube-dl will do it for you with the right options). - A. J.
Thank you for your help, but how can i run that command? using subprocess? Thanks - Joao
I think you could perfectly do it by using the library as you are doing right now. However, take a look at this link there you can read the option (listformats) to pass to get available formats. Actually, you can as @unkn0wn95 suggested specify the extension and it would download it on that format, see here. - A. J.

1 Answers

0
votes

Try this:

ficheiro2 = str(title)+"."+"mp4"