[Python] pytube taking weird amount of time

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    So when I run Pytube inside the script, it takes 30 seconds to finish, but when I run it in a function, it only takes 0.96 seconds to finish. Why is that? Can someone explain it in simple terms because I am new to Python?

    here it took only 0.96 seconbds to finish "inside a function"

    here it took 30s to finish in a normal line

    and do the function really get all the videos? cuz i print the random index and its give me a small number like 60 from 3000 videos here is the script


    import pytube
    import time
    StarterTick = time.time()
    print(StarterTick,type(StarterTick))
    VideoList = "list=PLKB9puQeauyBTnMmRWsB4VVfbbWR0tKCc"
    PlayList = f"https://www.youtube.com/playlist?{VideoList}"

    def getRandomVideo():
    PlayListVideos = pytube.contrib.playlist.Playlist(PlayList,)
    randomIndex = random.randint(0,len(PlayList) -1) # -1 cuz list start with 0 so if we have 5 items it would be 4 but len would return 5
    print(f"your random index is {randomIndex}")
    return PlayListVideos[randomIndex] # just return a random video from youtube list


    # this way it takes 30 s to finish PlayListVideos = pytube.contrib.playlist.Playlist(PlayList)

    print(f"here is your random Video {getRandomVideo()}")
    print("programm finished")
    print(f"THe programm took {time.time() - StarterTick:>3.2f} seconds to finish")

    Continue reading...

Compartilhe esta Página