Search code examples
pythonvideoffmpegyoutubeyoutube-dl

YouTube-DL Python output file format not mp4


I have written a small piece of code in python to extract either the audio or the video from a YouTube video. Here is the code:

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])

The audio extraction works fine, but the problem is with the video extraction. When I extract the video, I am getting the output as an mkv file, not an mp4 file. Any idea about how to save the video file as mp4?


Solution

  • If the video quality is above 1080p it can't be downloaded as MP4, try downloading video of max resolution 720p or 1080p to check if it's still downloading as mkv