pythonyoutubepytube

How can I quickly get titles of videos in YouTube playlist using pytube?


I need to get 1, a list of video links of a playlist and 2, list of video names of a playlist. This is what I am doing.

from pytube import YouTube, Playlist

playlist_link = "https://www.youtube.com/playlist?list=PLJKfZ_cKGyLdYqdzGLCJPbsi9UGCcEc5e"

video_links = Playlist(playlist_link).video_urls

video_titles = []
for link in video_links:
    video_titles.append(YouTube(link).title)

While this works, getting all the titles takes forever because each link has to be converted to a YouTube object, is there a faster way to do this?


Solution

  • I looked at your code and it takes around 22 seconds to execute.

    from time import time
    from pytube import YouTube, Playlist
    
    playlist_link = "https://www.youtube.com/playlist?list=PLJKfZ_cKGyLdYqdzGLCJPbsi9UGCcEc5e"
    
    video_links = Playlist(playlist_link).video_urls
    
    video_titles = []
    start = time()
    for link in video_links:
        video_titles.append(YouTube(link).title)
    
    print(f'Time taken: {time() - start}')
    # output
    Time taken: 21.815414667129517
    

    You can reduce the execution time by adding multithreading. The following code takes about 3 seconds to execute.

    from time import time
    from pytube import YouTube, Playlist
    from concurrent.futures import ThreadPoolExecutor, as_completed
    
    playlist_link = "https://www.youtube.com/playlist?list=PLJKfZ_cKGyLdYqdzGLCJPbsi9UGCcEc5e"
    video_links = Playlist(playlist_link).video_urls
    start = time()
    
    
    def get_video_title(link):
        title = YouTube(link).title
        return title
    
    
    processes = []
    with ThreadPoolExecutor(max_workers=10) as executor:
        for url in video_links:
            processes.append(executor.submit(get_video_title, url))
    
    video_titles = []
    for task in as_completed(processes):
        video_titles.append(task.result())
        print(task.result())
    
    
    print(f'Time taken: {time() - start}')
    # output
    Time taken: 2.7463150024414062