pytube: Really slow downloading with CLI vs Interactive Prompt

Executing the CLI takes a little over eternity

alkpc@alkPC-Asus ~/.installs $  pytube https://www.youtube.com/watch?v=9wcSVTErT2U --list
<Stream: itag="22" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.64001F" acodec="mp4a.40.2">
<Stream: itag="43" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp8.0" acodec="vorbis">
<Stream: itag="18" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.42001E" acodec="mp4a.40.2">
<Stream: itag="36" mime_type="video/3gpp" res="240p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">
<Stream: itag="17" mime_type="video/3gpp" res="144p" fps="30fps" vcodec="mp4v.20.3" acodec="mp4a.40.2">
<Stream: itag="136" mime_type="video/mp4" res="720p" fps="30fps" vcodec="avc1.4d401f">
<Stream: itag="247" mime_type="video/webm" res="720p" fps="30fps" vcodec="vp9">
<Stream: itag="135" mime_type="video/mp4" res="480p" fps="30fps" vcodec="avc1.4d401e">
<Stream: itag="244" mime_type="video/webm" res="480p" fps="30fps" vcodec="vp9">
<Stream: itag="134" mime_type="video/mp4" res="360p" fps="30fps" vcodec="avc1.4d401e">
<Stream: itag="243" mime_type="video/webm" res="360p" fps="30fps" vcodec="vp9">
<Stream: itag="133" mime_type="video/mp4" res="240p" fps="30fps" vcodec="avc1.4d4015">
<Stream: itag="242" mime_type="video/webm" res="240p" fps="30fps" vcodec="vp9">
<Stream: itag="160" mime_type="video/mp4" res="144p" fps="30fps" vcodec="avc1.4d400c">
<Stream: itag="278" mime_type="video/webm" res="144p" fps="30fps" vcodec="vp9">
<Stream: itag="140" mime_type="audio/mp4" abr="128kbps" acodec="mp4a.40.2">
<Stream: itag="171" mime_type="audio/webm" abr="128kbps" acodec="vorbis">
<Stream: itag="249" mime_type="audio/webm" abr="50kbps" acodec="opus">
<Stream: itag="250" mime_type="audio/webm" abr="70kbps" acodec="opus">
<Stream: itag="251" mime_type="audio/webm" abr="160kbps" acodec="opus">
alkpc@alkPC-Asus ~/.installs $ pytube https://www.youtube.com/watch?v=9wcSVTErT2U --itag=22

PLAYMEN & CLAYDEE ft TAMTA - Tonight.mp4 | 29292427 bytes
^Calkpc@alkPC-Asus ~/.installs $ pytube https://www.youtube.com/watch?v=9wcSVTErT2U --itag=22.4%  # --> 20 minutes

PLAYMEN & CLAYDEE ft TAMTA - Tonight.mp4 | 29292427 bytes
^C^C^C^[[Aalkpc@alkPC-Asus ~/.installs $ pytube https://www.youtube.com/watch?v=9wcSVTErT2U --itag=22  # --> ~60 minutes

vs running the interactive:

>>> from pytube import YouTube
>>> YouTube('http://youtube.com/watch?v=9bZkp7q19f0').streams.get_by_itag(22).download()

Unfortunately, no time or timestamps available, so this is reported in good faith šŸ˜• Tests are executed in a P4 3.2GHz HT / 2.5GB Ram / SSD

alkpc@alkPC-Asus ~ $ neofetch
MMMMMMMMMMMMMMMMMMMMMMMMMmds+.        alkpc@alkPC-Asus
MMm----::-://////////////oymNMd+`     ----------------
MMd      /++                -sNMd:    OS: Linux Mint 18.3 Sylvia x86_64
MMNso/`  dMM    `.::-. .-::.` .hMN:   Kernel: 4.10.0-42-generic
ddddMMh  dMM   :hNMNMNhNMNMNh: `NMm   Uptime: 1 hour, 37 mins
    NMm  dMM  .NMN/-+MMM+-/NMN` dMM   Packages: 2755
    NMm  dMM  -MMm  `MMM   dMM. dMM   Shell: bash 4.3.48
    NMm  dMM  -MMm  `MMM   dMM. dMM   Resolution: 1280x1024
    NMm  dMM  .mmd  `mmm   yMM. dMM   DE: Cinnamon 3.6.7
    NMm  dMM`  ..`   ...   ydm. dMM   WM: Mutter (Muffin)
    hMM- +MMd/-------...-:sdds  dMM   WM Theme: New-Minty (Mint-Y-Dark-Polo)
    -NMm- :hNMNNNmdddddddddy/`  dMM   Theme: Mint-Y-Dark-Polo [GTK2/3]
     -dMNs-``-::::-------.``    dMM   Icons: Surfn-Numix-Polo [GTK2/3]
      `/dMNmy+/:-------------:/yMMM   Terminal: gnome-terminal
         ./ydNMMMMMMMMMMMMMMMMMMMMM   CPU: Intel Pentium 4 3.20GHz (2) @ 3.200GHz
            .MMMMMMMMMMMMMMMMMMM      GPU: NVIDIA GeForce 8400 GS Rev. 3
                                      Memory: 1708MiB / 2501MiB

yt-video-9wcSVTErT2U-1514569445.json.tar.gz I can provide the partial (and the full file, if necessary)

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 24 (3 by maintainers)

Commits related to this issue

Most upvoted comments

If anyone is trying to increase the speed, I’ve written a multiprocessing chunk downloading function.

import multiprocessing as mp
from math import ceil

import requests
from pytube import YouTube

CHUNK_SIZE = 3 * 2**20  # bytes

def download_video(video_url, itag, filename):
    stream = YouTube(video_url).streams.get_by_itag(itag)
    url = stream.url
    filesize = stream.filesize

    ranges = [[url, i * CHUNK_SIZE, (i+1) * CHUNK_SIZE - 1] for i in range(ceil(filesize / CHUNK_SIZE))]
    ranges[-1][2] = None  # Last range must be to the end of file, so it will be marked as None.

    pool = mp.Pool(min(len(ranges), 64))
    chunks = pool.map(download_chunk, ranges)

    with open(filename, 'wb') as outfile:
        for chunk in chunks:
            outfile.write(chunk)


def download_chunk(args):
    url, start, finish = args
    range_string = '{}-'.format(start)

    if finish is not None:
        range_string += str(finish)

    response = requests.get(url, headers={'Range': 'bytes=' + range_string})
    return response.content

download_video(video_url, 160, filename)

I do not know if my problem is related to the one reported here, but why does PyTube not use my full bandwidth while downloading a youtube video?

It uses maybe at-most 1/4 of the maximum it could use of my bandwidth . . .

Same problem here. I had to remove: on_progress_callback I think it should be fixed or, at least, to have a switch --no-progress

https://github.com/nficano/pytube/blob/9b2345574430537d2d8b917ae399a7488e042248/pytube/cli.py#L162

Same issue here, and removing the on_progress_callback parameter seems to resolve the issue… well, at least it downloads faster although i still find it somewhat slow…

I can confirm that the same problem is occurring for me.

this code in the interactive console is immediate.

yt = YouTube(video_url) available_streams = yt.streams.filter(resolution=ā€œ360pā€, subtype=ā€˜mp4’).asc() available_streams.first().download(ā€˜./Documentaries’)

but extremely slow when running in my .py file…