Lab: Downloading a Video
Due: Wednesday, September 30 at 11:59pm (one week after it is assigned) Worth: 2 points
This is one of this week’s two short labs; the other is File Encodings. Both are graded on what you produce rather than on a test badge. For this lab, you will submit a downloaded video.
Starter code: github.com/rtealwitter/lab-youtube-download
Python makes it easy to download and run programs, called scripts, that other people have written. In this lab, you will run a script for downloading videos from YouTube. You will write your own script in the next project.
Finding the working repo
There are many GitHub repos with code for downloading YouTube videos. Here are two:
Your first task is to determine which repo has working code by checking its automated tests. Each repo runs its test suite on every change through GitHub Actions and shows the result as a badge:
Open both links and find the test badge on each. One repo’s tests are passing and the other’s are failing. Clone the one with passing tests, then step into the folder:
$ git clone <url>
$ cd <repo_dir>Running the code
Recall that ls lists the files in the current folder. If the clone worked, you will see something like this:
$ ls
bundle LICENSE README.md yt-dlp.cmd
Changelog.md Maintainers.md supportedsites.md yt-dlp.sh
CONTRIBUTING.md Makefile test
CONTRIBUTORS public.key THIRD_PARTY_LICENSES.txt
devscripts pyproject.toml yt_dlpYour exact output will differ a little in colors, columns, and sort order, but the same files should be there. Thousands of people have contributed to this project. Later in the course, you will learn how to contribute to a project like it and add that work to your GitHub profile.
Python projects commonly use a file named __main__.py as an entry point. It usually sits in a subfolder named after the project, which here is yt_dlp. Run it with:
$ python3 yt_dlp/__main__.py
Usage: yt-dlp [OPTIONS] URL [URL...]
yt-dlp: error: You must provide at least one URL.
Type yt-dlp --help to see a list of all options.yt_dlp/__main__.py is a relative path: the file’s name together with the subfolder it lives in. Drop the subfolder and Python cannot find the file:
$ python3 __main__.py
python3: can't open file '__main__.py': [Errno 2] No such file or directoryThis is the FileNotFoundError from the reading, caused here by omitting one folder from the path. Even when called correctly, the program displays an error message rather than opening a window. Python scripts usually have a command-line interface (CLI), where you type the options as part of the command, rather than the graphical user interface (GUI) you are used to.
To download a video, pass the script a URL. Use https://www.youtube.com/watch?v=dQw4w9WgXcQ for this lab. Paste the URL at the end of the command:
$ python3 yt_dlp/__main__.py 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'Depending on your computer’s configuration, you may get an error about a failed SSL certificate. If so, add the --no-check-certificate option:
$ python3 yt_dlp/__main__.py --no-check-certificate 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'SSL (the secure socket layer) is what encrypts web traffic, and this error means something about your Python setup cannot verify the encryption. That would matter if you were logging into your bank; for downloading a public video it is fine.
Most command-line scripts print detailed instructions when you pass --help:
$ python3 yt_dlp/__main__.py --helpRun it and you will see that yt-dlp has options for setting audio and video quality, grabbing whole playlists, and routing the download through a proxy.
Legality
Using yt-dlp is legal, and downloading video has legitimate uses beyond piracy. The Aleph with Beth channel, which teaches Hebrew, gives explicit instructions for using yt-dlp to watch their lessons in places without internet. The KCNA Watch archive uses it to keep a permanent record of North Korean broadcasts so foreign-policy analysts can trust that videos will not be altered or deleted after the fact.
Most “YouTube downloader” websites are thin wrappers around yt-dlp. The package used to be called youtube-dl, but after legal pressure from the RIAA over documentation examples that looked like they encouraged piracy, the project forked into yt-dlp. In the US, writing or possessing software is never itself a crime; you can only be prosecuted for illegal things you do with it. The Hacker News discussion covers the case in more detail.
Submitting
Put the downloaded .mp4, .webm, or .mkv video in a GitHub repository, then submit that repository and branch to the Gradescope Programming Assignment. Gradescope’s instructor-owned check verifies that the file is a complete, recognizable video and is the authoritative grade. Any GitHub Action is only preliminary feedback. Fix the repository and resubmit if the check fails.