-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Evaluate format selector before download in Python #9973
Comments
The info dict will only have a |
The "bestvideo" and "bestaudio" are typically the defaults, and Youtube pushes that information into the info.json in the format_id field. All the filesize_approx, vcodec, acodec, resolution, ext, format_note etc fields at the root of the json object describe this combination of formats. All the filesize(_approx)s are in the .formats branch. There's about 20 of them and you could loop through building a dictionary with the format_id and .filesize or .filesize_approx. The .formats branch is what builds the table you see with the -F option passed to yt-dlp. |
From what you’ve said and my own experimentation, I’ve figured out the following:
So with this knowledge, I can do the following: def get_filesize(format_id, formats):
for f in formats:
if f['format_id'] == format_id:
return f.get('filesize', f.get('filesize_approx'))
with YoutubeDL({'format': 'bestvideo*+bestaudio'}) as ydl:
info = ydl.extract_info(some_url, download=False)
video_format_id, audio_format_id = info['format_id'].split('+')
video_size = get_filesize(video_format_id, info['formats'])
audio_size = get_filesize(audio_format_id, info['formats'])
# Or just look them up in info['requested_formats']
# ... Do logic based on video_size and audio_size ...
with YoutubeDL('format': 'bestvideo*,bestaudio') as ydl:
ydl.download([some_url]) Notice how I need to create another I was hoping for a way to evaluate any format selector given
There’s also still my question about why |
with this pattern: # extract info only
with YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(URL, download=False)
# do things with info dict here
# process and download
with YoutubeDL(ydl_opts) as ydl:
processed_info = ydl.process_ie_result(info)
can you give an example |
For anyone with the same questions as me, I just realized the following: It is easy to manually evaluate any format selector because the formats are already sorted from def get_best_mp4_video(formats):
for f in reversed(formats):
if f.get('vcodec') != 'none' and f.get('ext') == 'mp4':
return f['format_id'] I have observed
yt-dlp -O "%(filesize)s, %(filesize_approx)s" -f bv,ba https://youtu.be/BaW_jenozKc
I didn’t even know that
Great, this works for my current particular case where I know which format I want to download beforehand. What would I do if I wanted to look at all the formats and then choose one? |
select the format(s) by adding the |
DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE
Checklist
Please make sure the question is worded well enough to be understood
From within a Python script, I would like to determine which format a specific format selector will select before starting the download. Concretely, I would like to check
filesize
andfilesize_approx
ofbestvideo*
andbestaudio
before downloading them. Is there a good way to do this?There already is the issue [Question] How to extract filesize before download?, but that only discusses template strings. It also brings up the key
requested_formats
, which doesn’t seem to work here:I’ve also already noticed that
info
has the keysfilesize
andfilesize_approx
, but which size do they refer to? The video, the audio, the merged format? (I didn’t even request a merge…) And why does it have both, with different values?Provide verbose output that clearly demonstrates the problem
yt-dlp -vU <your command line>
)'verbose': True
toYoutubeDL
params instead[debug] Command-line config
) and insert it belowComplete Verbose Output
No response
The text was updated successfully, but these errors were encountered: