# Downloading YouTube Videos

This guide explains how to download educational videos from YouTube for offline
use in the classroom. There are two methods available: using a recommended
command-line tool or a simple browser add-on.

---

## Before You Begin

Downloading videos can significantly impact your school's network and computer
storage. Please follow these guidelines:

- **Copyright Guidance:** Only download videos that are explicitly permitted for
  educational use or fall under "Fair Use" policies. Avoid downloading
  commercial movies or copyrighted music without permission.
  after school) to avoid slowing down the internet for other students.
- **Storage Caution:** High-quality videos (1080p and above) use a lot of disk
  space. Regularly delete old videos from your public folders to ensure there is
  enough room for new content.

---

## Method 1: Using the Command Line (Recommended)

This method is recommended for its power and reliability. It uses a
command-line tool called `yt-dlp`.

> [!WARNING] **Firewall Must Be Temporarily Disabled**
> To download from YouTube using this method, you must first stop the firewall.
> As the `admin` user, run `rsv stop nftables`. This is the only step that
> requires admin privileges. The rest of the process should be run as the
> `guest` user.

{{% steps %}}

### 1. Install `yt-dlp`

First, you need to add the `asdf` plugin for `yt-dlp` and install it. This only
needs to be done once.

```bash
# Add the plugin
asdf plugin add yt-dlp

# Install the latest version
asdf install yt-dlp latest

# Set it as the global default
asdf set yt-dlp latest

# Export the asdf shims to your PATH for the current session
export PATH="/srv/guest/asdf/shims:$PATH"
```

### 2. Navigate to Download Directory

Change to the directory where you want to save the videos. By default,
downloads are saved to the current folder you are in.

```bash
# We recommend saving to a teacher's public folder so others can access them
cd /srv/public/teachers/jdoe
```

### 3. Download the Video(s)

You can now download a video using its URL. The `-f best` flag tells the tool
to download the best available **merged** video and audio quality.

**Single Video:**

```bash
yt-dlp -f best https://www.youtube.com/watch?v=Qg1ml3aHAM4
```

**Batch Download (Multiple URLs):**
You can download multiple videos at once by listing the URLs separated by
spaces, or by pointing to a text file containing one URL per line.

```bash
# Using multiple URLs
yt-dlp -f best URL1 URL2 URL3

# Using a text file (e.g., videos.txt)
yt-dlp -f best -a videos.txt
```

> [!TIP]
> For the absolute highest quality (which might download video and audio
> separately), use `-f bestvideo+bestaudio`. You can also list all available
> formats with the `-F` flag (e.g., `yt-dlp -F <URL>`).

### 4. Restart the Firewall

**Important:** Once you have finished downloading your videos, you MUST
restart the firewall to ensure the computer remains protected.

```bash
# As the admin user
rsv start nftables
```

{{% /steps %}}

---

## Method 2: Using a Firefox Add-on (Alternative)

This is an alternative method for users who prefer a graphical interface. It
involves temporarily adding a video downloader tool to the Firefox web browser.

> [!WARNING]
> The add-ons listed in the Firefox store are developed by third parties.
> TechLit does not own, endorse, or provide support for any specific add-on.
> Use them at your own discretion.

{{% details title="Click to see the Firefox Add-on steps..." closed="true" %}}

> [!NOTE]
> Add-ons installed in the guest session are temporary. They will be
> automatically removed when you log out or reboot the computer. You will
> need to repeat these steps for each new session.

{{% steps %}}

### 1. Open Firefox Add-ons

- Open the Firefox browser.
- Click the menu button (☰) in the top-right corner and select **Add-ons and
  themes**.
- Alternatively, you can press <kbd>Ctrl</kbd> + <kbd>Shift</kbd> +
  <kbd>A</kbd>.

### 2. Find a Downloader Add-on

- In the "Find more add-ons" search box, type `youtube downloader` and press
  Enter.
- Browse the search results and choose an add-on that looks suitable. Read the
  descriptions and reviews to help you decide.

### 3. Add the Add-on to Firefox

- Once you've chosen an add-on, click on it, and then click the **Add to
  Firefox** button.
- A permission pop-up will appear. Click **Add** to confirm.

### 4. Download the Video

- Navigate to the YouTube video you want to download.
- The add-on will typically add a "Download" button near the video player.
  Click it and follow the on-screen instructions to save the video file to the
  computer.

{{% /steps %}}
{{% /details %}}

---

## Troubleshooting

If your download fails, check the following:

- **Firewall:** Did you run `rsv stop nftables` as the `admin` user? `yt-dlp`
  cannot connect to YouTube if the firewall is active.
- **Disk Space:** Check if the computer has enough space. You can run
  `df -h /srv/public` to see available space in the public folder.
- **Private/Deleted Videos:** Ensure the video is public and hasn't been
  removed by YouTube.
- **Update the Tool:** YouTube often changes how they deliver videos. If
  downloads consistently fail, you may need to update `yt-dlp` by running
  `asdf install yt-dlp latest` again.

