YouTube-DL Python 输出文件格式不是 mp4

YouTube-DL Python output file format not mp4

我在 python 中编写了一小段代码来从 YouTube 视频中提取音频或视频。这是代码:

from __future__ import unicode_literals
import youtube_dl

link = input("Enter the video link:")

while True:
    choice = input("Enter a for audio file, v for video file:")
    if choice == "a" or choice == "v":
        break

ydl_opts = {}

if choice == "a":
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    info_dict = ydl.extract_info(link, download=False)
    video_title = info_dict.get('title', None)

if choice == "a":
    path = f'D:\DwnldsYT\{video_title}.mp3'
if choice == "v":
    path = f'D:\DwnldsYT\{video_title}.mp4'

ydl_opts.update({'outtmpl':path})

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([link])

音频提取工作正常,但问题出在视频提取上。当我提取视频时,我得到的输出是 mkv 文件,而不是 mp4 文件。知道如何将视频文件保存为 mp4 吗?

如果视频质量高于 1080p,则无法下载为 MP4,请尝试下载最大分辨率为 720p 或 1080p 的视频,以检查它是否仍在下载为 mkv