How can I list video resolutions in youtube-dl while using it as a python module?

Saumyakanta Sahoo picture Saumyakanta Sahoo · Feb 16, 2016 · Viewed 10k times · Source

Well, I can get the video formats directly by using this in terminal -

$ youtube-dl -F "some youtube url"

Output :

[youtube] Setting language
[youtube] P9pzm5b6FFY: Downloading webpage
[youtube] P9pzm5b6FFY: Downloading video info webpage
[youtube] P9pzm5b6FFY: Extracting video information
[info] Available formats for P9pzm5b6FFY:
format code extension resolution  note 
140         m4a       audio only  DASH audio , audio@128k (worst)
160         mp4       144p        DASH video , video only
133         mp4       240p        DASH video , video only
134         mp4       360p        DASH video , video only
135         mp4       480p        DASH video , video only
136         mp4       720p        DASH video , video only
17          3gp       176x144     
36          3gp       320x240     
5           flv       400x240     
43          webm      640x360     
18          mp4       640x360     
22          mp4       1280x720    (best)

but I want to use the same option while using youtube-dl as a module in python .

Right now I have to guess and specify the options for downloading as :

import youtube_dl

options = {
'format': 'bestaudio/best', # choice of quality
'extractaudio' : True,      # only keep the audio
'audioformat' : "mp3",      # convert to mp3 
'outtmpl': '%(id)s',        # name the file the ID of the video
'noplaylist' : True,        # only download single song, not playlist
}

with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download(url)

I'm unable to know which format is available or not. but if I can get to list the available formats , then I can set those options accordingly .

Is there any way to use that "-F" switch inside python ?

Answer

jaimeMF picture jaimeMF · Feb 16, 2016

If you use the listformats option to print the table to standard output, it won't download the video. For example:

import youtube_dl

options = {
    'format': 'bestaudio/best',  # choice of quality
    'extractaudio': True,        # only keep the audio
    'audioformat': "mp3",        # convert to mp3
    'outtmpl': '%(id)s',         # name the file the ID of the video
    'noplaylist': True,          # only download single song, not playlist
    'listformats': True,         # print a list of the formats to stdout and exit
}

with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download(['http://www.youtube.com/watch?v=BaW_jenozKc'])