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.

[BUG] KeyError: 'content-length'

See original GitHub issue

Describe the bug The download function in 10.4.1 results in a 'content-length' KeyError.

To Reproduce Here’s my code:

from pytube import YouTube

url = input('URL: ')

yt = YouTube(url)
yt.streams.filter(file_extension='mp4').first().download()

Expected behavior I expect it to download the video.

Output

URL: https://www.youtube.com/watch?v=EbNr015DZbM
Traceback (most recent call last):
  File "C:\Users\cmlsc\Desktop\youtube_download.py", line 7, in <module>
    yt.streams.filter(file_extension='mp4').first().download()
  File "D:\Python38\lib\site-packages\pytube\streams.py", line 251, in download
    bytes_remaining = self.filesize
  File "D:\Python38\lib\site-packages\pytube\streams.py", line 158, in filesize
    self._filesize = request.filesize(self.url)
  File "D:\Python38\lib\site-packages\pytube\request.py", line 127, in filesize
    return int(head(url)["content-length"])
KeyError: 'content-length'

System information Please provide the following information: Python Version: 3.8.6 Pytube Version: 10.4.1 Install Command: D:\Python38\python.exe -m pip install git+https://github.com/pytube/pytube --upgrade

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

2reactions
ManasKhoslacommented, Jan 31, 2022

Rather than using “pyt_obj.filesize”, try using “pyt_obj._filesize”. That fixed the “KeyError: ‘content-length’” for me.

I think the issue is in pytube.requests and pytube.streams as both have conflicting keys here

streams.py self._filesize: Optional[int] = int(stream.get('contentLength', 0))

requests.py `@lru_cache() def filesize(url): “”"Fetch size in bytes of file at given URL

:param str url: The URL to get the size of
:returns: int: size in bytes of remote file
"""
return int(head(url)["content-length"])`
1reaction
Robin-Haupt-1commented, Jan 22, 2022

I am also getting the same issue only with the following video, which isn’t a recent video or a livestream:

https://www.youtube.com/watch?v=qT4a73ctcmw

Traceback (most recent call last):
  File "/hdd/Software Engineering/PyCharm/AHC-Archive-Youtube-Videos/download_youtube/import_youtube_video_into_obsidian_main_vault.py", line 214, in <module>
    y._get(x)
  File "/hdd/Software Engineering/PyCharm/AHC-Archive-Youtube-Videos/download_youtube/import_youtube_video_into_obsidian_main_vault.py", line 154, in _get
    saved_video = ffmpeg.input(video_stream.download(output_path=self.temp_folder))
  File "/hdd/Software Engineering/2022/22-01-02 Python 3.10 Shared/venv/lib/python3.10/site-packages/pytube/streams.py", line 247, in download
    bytes_remaining = self.filesize
  File "/hdd/Software Engineering/2022/22-01-02 Python 3.10 Shared/venv/lib/python3.10/site-packages/pytube/streams.py", line 146, in filesize
    self._filesize = request.filesize(self.url)
  File "/hdd/Software Engineering/2022/22-01-02 Python 3.10 Shared/venv/lib/python3.10/site-packages/pytube/request.py", line 200, in filesize
    return int(head(url)["content-length"])
KeyError: 'content-length'

Read more comments on GitHub >

github_iconTop Results From Across the Web

"KeyError: 'content-length'" when using PyTube on specific ...
I am getting the error. KeyError: 'content-length'. while downloading videos. Specifically for this one, whenever I try to download this ...
Read more >
KeyError: 'content-length' : JT-10823 - YouTrack
Using youtrack-python-api-3.0.2-47 I found the following error: Transfer attachment of H-8: CN0151_02_1024.gif. Traceback (most recent call last):
Read more >
pytube filesize() keyerror content-length - You.com - You.com
The bug Iam getting the KeyError: 'content-length' while downloading videos, specifically for this one, when ever I try to download this video in...
Read more >
Bountysource
[BUG] KeyError: 'content-length' ... Help and Information. Frequently Asked Questions · Bugs and Feature Requests · Fees ...
Read more >
Could not load model from S3 due to 'ContentLength'
Error : <class 'KeyError'>: 'ContentLength' UserWarning: Agent could not be loaded with the provided configuration.
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