1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] yt-dlp returns non-existent format_id when extracting formats via Python

Discussão em 'Python' iniciado por Stack, Setembro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I'm a Python beginner. I am using yt-dlp via Python to select the best video format with a target resolution.
    However, I sometimes get a format_id that does not actually exist in the -F output. My 'yt-dlp' is up to date.

    My script:

    import os
    import subprocess
    import glob
    import sys
    import yt_dlp

    def download_best_video (url, target_height):
    ydl_opts = {
    'quiet': True,
    'forcejson': True,
    'dump': True
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    info_dict = ydl.extract_info(url, download=False)

    formats = info_dict.get('formats', [])

    # get maximum bit rate format
    max_bitrate = 0
    max_bitrate_format_id = None
    for format in formats:
    height = format.get('height')
    bitrate = format.get('tbr')
    if height == target_height and bitrate > max_bitrate:
    max_bitrate = bitrate
    max_bitrate_format_id = format.get('format_id')

    if max_bitrate_format_id == None:
    return

    # download
    script_path = os.path.abspath(__file__)
    script_dir = os.path.dirname(script_path)

    print(script_dir)

    args = ["yt-dlp", "--ignore-no-formats-error", "-f", f"{max_bitrate_format_id}+ba[ext=m4a]",
    "--write-sub", "--embed-subs", "--sub-lang", "ja", "--embed-metadata", "--paths", script_dir, url]

    print("cmd:", " ".join(args))

    subprocess.run(args)

    # delete subtitle file
    vtt_files = glob.glob(os.path.join(script_dir, "*.vtt"))
    for vtt_file in vtt_files:
    os.remove(vtt_file)

    heights = [720, 1080, 1440, 1920, 2160, 2560, 3840]
    print(f"available heights: {heights}")
    target_height_str = input("height = ")

    target_height = None
    try:
    target_height = int(target_height_str)
    except ValueError:
    sys.exit()

    if (not(heights.__contains__(target_height))):
    sys.exit()

    url_Texts = input("URL > ")
    urls = url_Texts.split()

    for url in urls:
    try:
    download_best_video(url, target_height)
    except Exception as e:
    continue


    Output:

    PS C:\Users\user> & C:/Users/user/AppData/Local/Programs/Python/Python313/python.exe "d:/Download/Download Best Video.py"
    available heights: [720, 1080, 1440, 1920, 2160, 2560, 3840]
    height = 1440
    URL > https://www.youtube.com/watch?v=wJwUjuKr_54
    d:\Download
    cmd: yt-dlp --ignore-no-formats-error -f 623+ba[ext=m4a] --write-sub --embed-subs --sub-lang ja --embed-metadata --paths d:\Download https://www.youtube.com/watch?v=wJwUjuKr_54
    [youtube] Extracting URL: https://www.youtube.com/watch?v=wJwUjuKr_54
    [youtube] wJwUjuKr_54: Downloading webpage
    [youtube] wJwUjuKr_54: Downloading tv simply player API JSON
    [youtube] wJwUjuKr_54: Downloading tv client config
    [youtube] wJwUjuKr_54: Downloading tv player API JSON
    [info] wJwUjuKr_54: Downloading subtitles: ja
    WARNING: Requested format is not available
    Deleting existing file d:\Download\LE SSERAFIM(르세라핌) 'CRAZY' (4K) | STUDIO CHOOM ORIGINAL [wJwUjuKr_54].ja.vtt
    [info] Writing video subtitles to: d:\Download\LE SSERAFIM(르세라핌) 'CRAZY' (4K) | STUDIO CHOOM ORIGINAL [wJwUjuKr_54].ja.vtt
    [download] Destination: d:\Download\LE SSERAFIM(르세라핌) 'CRAZY' (4K) | STUDIO CHOOM ORIGINAL [wJwUjuKr_54].ja.vtt
    [download] 100% of 3.45KiB in 00:00:00 at 15.56KiB/s
    ERROR: [youtube] wJwUjuKr_54: No video formats found!


    Format list of the video (I have only included the formats for 1440p.):

    C:\Users\user>yt-dlp -F https://www.youtube.com/watch?v=wJwUjuKr_54
    [youtube] Extracting URL: https://www.youtube.com/watch?v=wJwUjuKr_54
    [youtube] wJwUjuKr_54: Downloading webpage
    [youtube] wJwUjuKr_54: Downloading tv simply player API JSON
    [youtube] wJwUjuKr_54: Downloading tv client config
    [youtube] wJwUjuKr_54: Downloading tv player API JSON
    [info] Available formats for wJwUjuKr_54:
    ID EXT RESOLUTION FPS CH │ FILESIZE TBR PROTO │ VCODEC VBR ACODEC ABR ASR MORE INFO
    ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    (omit)
    308 webm 2560x1440 60 │ 275.48MiB 12195k https │ vp9 12195k video only 1440p60, webm_dash
    400 mp4 2560x1440 60 │ 145.44MiB 6439k https │ av01.0.12M.08 6439k video only 1440p60, mp4_dash
    (omit)


    The command

    yt-dlp --ignore-no-formats-error -f 623+ba[ext=m4a] --write-sub --embed-subs --sub-lang ja --embed-metadata --paths d:\Download https://www.youtube.com/watch?v=wJwUjuKr_54


    was generated by my script, but format 623 does not exist in the -F output. Why is this happening? This script used to work fine until recently.

    By the way, I am aware that I could use a command like:

    yt-dlp -f "bv[height=1440]" URL


    However, I don’t use this approach, because for some reason it does not always select the highest quality format. That is why I wrote my own script to choose the format with the maximum bitrate at the desired height.

    D:\Download>yt-dlp -f bv[height=1440]+ba[ext=m4a] https://www.youtube.com/watch?v=wJwUjuKr_54
    [youtube] Extracting URL: https://www.youtube.com/watch?v=wJwUjuKr_54
    [youtube] wJwUjuKr_54: Downloading webpage
    [youtube] wJwUjuKr_54: Downloading tv simply player API JSON
    [youtube] wJwUjuKr_54: Downloading tv client config
    [youtube] wJwUjuKr_54: Downloading tv player API JSON
    [info] wJwUjuKr_54: Downloading 1 format(s): 400+140
    [download] Destination: LE SSERAFIM(르세라핌) 'CRAZY' (4K) | STUDIO CHOOM ORIGINAL [wJwUjuKr_54].f400.mp4
    [download] 100% of 145.44MiB in 00:00:17 at 8.16MiB/s
    [download] Destination: LE SSERAFIM(르세라핌) 'CRAZY' (4K) | STUDIO CHOOM ORIGINAL [wJwUjuKr_54].f140.m4a
    [download] 100% of 2.93MiB in 00:00:00 at 6.25MiB/s


    For example, at 1440p the best quality should be format 308, but yt-dlp instead selects 400.

    Why does 'yt-dlp' return a format_id that does not actually exist in the yt-dlp -F output? And how can I make sure to only select real, downloadable formats?

    Continue reading...

Compartilhe esta Página