Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Explanation of code
#1
Here's an example of the code. I'd like to know what ctx is here. It's not passed anywhere to a function and i can't use get('formats')[::-1] without this function. How does it work and called?

import yt_dlp

URLS = ['https://www.youtube.com/watch?v=BaW_jenozKc']

def format_selector(ctx):
    formats = ctx.get('formats')[::-1]

   .....


ydl_opts = {
    'format': format_selector,
}

with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    ydl.download(URLS)
buran write Feb-26-2024, 12:09 PM:
Please, use python tags when post code
Reply
#2
Obviously the format_selector() function is called either by YoutubeDL(ydl_opts) or by ydl.download(URLS). The ctx argument is created internally by the yt_dlp module. You can find the type of this object and see where it is called by replacing the function by

def format_selector(ctx):
    raise RuntimeError(ctx)
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Thank you. So if i understood correctly, YoutubeDL looks at ydl_opts, sees format in 'format': format_selector and is sending all of the format related things to format_selector function even though function is called without arguments?

This is specific to yt_dlp, right? I can't do this with my own functions, i need to send arguments or use *args.
Reply
#4
(Feb-26-2024, 02:41 PM)ejKDE Wrote: even though function is called without arguments?
You never call the function format_selector yourself, it's called internally from YoutubeDL.download and it expects that it takes ctx argument

From the docstring for YoutubeDL class

Quote: format: Video format code. see "FORMAT SELECTION" for more details.
You can also pass a function. The function takes 'ctx' as
argument and returns the formats to download
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Ok, thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A better explanation of the last post Led_Zeppelin 9 2,405 Sep-20-2022, 05:08 PM
Last Post: deanhystad
  Operator meaning explanation Sherine 3 2,053 Jul-31-2021, 11:05 AM
Last Post: Sherine
  Explanation of except ... as : Fernando_7obink 2 1,949 Feb-13-2021, 04:45 AM
Last Post: deanhystad
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 2,993 Jan-28-2021, 05:05 PM
Last Post: buran
  .remove() from a list - request for explanation InputOutput007 3 2,257 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  Explanation of the left side of this statement please rascalsailor 3 2,525 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  Need explanation of one line of code Fliberty 6 3,505 Feb-18-2020, 12:50 AM
Last Post: Fliberty
  explanation of code hikerguy62 2 2,265 Aug-01-2019, 01:37 PM
Last Post: hikerguy62
  While loop explanation rdgbl 1 2,310 Dec-18-2018, 01:03 AM
Last Post: stullis
  Lamda function explanation mrcool4 4 3,580 Jul-04-2018, 10:44 AM
Last Post: mrcool4

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020