An Obscure Error While Downloading a YouTube Video for Offline Viewing

Trying to download a video from YouTube for offline use using youtube-dl and it keeps failing? This article will show you how I fixed it.

An Obscure Error While Downloading a YouTube Video for Offline Viewing

Lenny Rachitsky recently did a lengthy podcast interview with Ethan Smith who runs Graphite, an SEO agency.

The video is more than 1:30 hrs long so I decided I was going to watch it offline. Several attempts to download the video from YouTube failed but I managed to find a workaround.

The Download Failed on macOS and Ubuntu

My first attempt at downloading the video on macOS using youtube-dl led to the following error:

[download]   0.4% of 645.08MiB at 51.43KiB/s ETA 03:33:17[download] Got server HTTP error: Downloaded 2451832 bytes, expected 676416237 bytes. Retrying (attempt 1 of 10)...

I was too impatient to try to figure out why the download failed, so I SSHed into a spare Ubuntu Linux VPS I had running on AWS and installed the youtube-dl binary. This second attempt also failed. The error message was identical to the error I was shown on macOS.

A quick check revealed that I had version v2021.12.17 of youtube-dl installed on both macOS and Ubuntu.

At the time of my attempt, the current date was 2022.12.17 which coincided with the month and day (but not the year) of release for the version I had installed (v2021.12.17). In other words, the only difference was the year of release (2021 vs 2022).

Since the version of youtube-dl I had installed was released exactly a year ago from the current date, the quickest way to fix the issue was to download the latest version of youtube-dl, but it turns out that v2021.12.17 is indeed the latest version.

Then, I remembered that the open source project for youtube-dl had been having management issues. Collectively, these issues led the community to shift their focus to an actively maintained fork called yt-dlp.

So, I downloaded the latest binary for yt-dlp which was released on GitHub only a month ago. The download also failed. The error output from yt-dlp was identical to the output above from youtube-dl.

Slowing Down to Diagnose the Error

Based on the hint contained in the error, my next suspicion was that there was some kind of mismatch between the HTTP range request that youtube-dl was sending and what YouTube's servers were expecting.

I condensed the error down to the following string and did a quick search on Google:

Got error: Downloaded 2451832 bytes, expected 676416237 bytes.

In return, Google gave the following (non-) result:

About 0 results (0.44 seconds)

Although there were no results for my query, Google did helpfully include 4 screenshots, two of which linked to GitHub. I clicked through to the first image which led me to this closed issue on the youtube-dl project.

This comment, left in 2017, provided a valuable hint: that the issue is from YouTube and not due to a bug in the youtube-dl code. The workaround is to use a different format.

Since I didn't specify any format in all of my attempts, I decided to skim the CLI arguments supported by youtube-dl so I could specify a format explicitly.

youtube-dl --help


Video Format Options:
    -f, --format FORMAT                  Video format code, see the "FORMAT SELECTION" for all the info
    --all-formats                        Download all available video formats
    --prefer-free-formats                Prefer free video formats unless a specific one is requested
    -F, --list-formats                   List all available formats of requested videos

Armed with this information, I turn on verbose mode (with the "-v" option) and passed the "-F" option to youtube-dl so it can list the video formats supported by YouTube for the video in question:

youtube-dl -v "https://www.youtube.com/watch?v=mOY159TlYXM" -F
[debug] System config: []
[debug] User config: []
[debug] Custom config: []
[debug] Command-line args: ['-v', 'https://www.youtube.com/watch?v=mOY159TlYXM', '-F']
[debug] Encodings: locale UTF-8, fs utf-8, out utf-8, pref UTF-8
[debug] youtube-dl version 2021.12.17
[debug] Git HEAD: fba051f98
[debug] Python version 3.10.5 (CPython) - macOS-12.6-x86_64-i386-64bit
[debug] exe versions: none
[debug] Proxy map: {}
[youtube] mOY159TlYXM: Downloading webpage
[info] Available formats for mOY159TlYXM:
... (truncated due to poor formatting) ...
... (see the next screen shot) ...

It turns out that when I don't explicitly specify a format, youtube-dl defaults to downloading the last format in the list (format code: 22; file format: mp4 at a 1280x720 resolution).

It seems there's problem with how that video format is encoded/served by YouTube's servers because any attempts to download it explicitly leads to the error I mentioned above. (Highly likely that YT has checks in place to surpress encoding errors when videos are consumed normally by a human.)

Once I picked the mp4 format at a lower resolution (format code: 18, file format: mp4 at a 640x360 resolution), the download worked fine. The command I used was:

youtube-dl -v "https://www.youtube.com/watch?v=mOY159TlYXM" -f 18

HTH!