python-3.xyt-dlp

How yt-dlp provides faster search results


Below is a search code

def search(query, max_results=5):
    # Parameters for calling yt-dlp
    command = [
            'yt-dlp',
            'ytsearch{}:{}'.format(max_results, query),
            '--dump-json', 
            '--default-search', 'ytsearch',
            '--no-playlist', '--no-check-certificate', '--geo-bypass'
        ]
    try:
        # Get the output and analyze it
        output = subprocess.check_output(command).decode('utf-8')
        videos = [json.loads(line) for line in output.splitlines()]
        # Simplify the results for displaying to the user
        simplified_results = []
        for video in videos:
            simplified_results.append({
                "title": video.get("title", "N/A"),
                "url": video.get("webpage_url", "N/A"),
                "origin_url": video.get("original_url", "N/A"),
                "duration": str(datetime.timedelta(seconds=video.get("duration", 0))),
                "uploader": video.get("uploader", "N/A")
            })

        return simplified_results

    except subprocess.CalledProcessError:
        return []

This is the command I use to call yt-dlp, and then get the output and convert it into data, but yt-dlp is too slow whether it is called on the console or through subprocess. Below are the parameters I use

command = [
            'yt-dlp',
            'ytsearch{}:{}'.format(max_results, query),
            '--dump-json', 
            '--default-search', 'ytsearch',
            '--no-playlist', '--no-check-certificate', '--geo-bypass'
        ]
yt-dlp ytsearch{max_results}:{query} --dump-json --default-search ytsearch --no-playlist --geo-bypass

I don't need to download the video, just get his webpage url, but it takes 11 seconds for just five results
I want to get results faster, is there any good way?


Solution

  • command = [
                "yt-dlp",
                "ytsearch{}:{}".format(max_results, query),
                "--dump-json", 
                "--default-search", "ytsearch",
                "--no-playlist", "--no-check-certificate", "--geo-bypass",
                "--flat-playlist", "--skip-download", "--quiet", "--ignore-errors"
            ]
    

    Add --flat-playlist, --skip-download, --quiet, --ignore-errors to speed up, Reduced from 11 seconds to over 1.6 seconds