question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Really slow downloading with CLI vs Interactive Prompt

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:24 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
StasDeepcommented, Mar 21, 2018

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)
2reactions
IeUDEcommented, Feb 16, 2019

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 . . .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Really slow downloading with CLI vs Interactive Prompt -
Really slow downloading with CLI vs Interactive Prompt.
Read more >
Very Slow Download speed in terminal [closed] - Ask Ubuntu
1 Answer. Save this answer. If you are getting fast speed in the browser and only slow speed for installing packages, the network...
Read more >
Extremely slow download speed in Windows command prompt
Extremely slow download speed in Windows command prompt · Are you using a virtual environment on Ubuntu as well? Try using a different...
Read more >
3 handy command-line internet speed tests - Opensource.com
This gives you your download and upload Internet speeds. It's fast and scriptable, so you can run it regularly and save the output...
Read more >
Console output overhead: why is writing to stdout so slow?
And for most of our use cases, this would be more than enough. However, when we are outputting a huge amount of data,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found