Wednesday, April 9, 2025

Download HD audio and video files with yt-dlp

#!/bin/bash

# Create a temporary file to store URLs
TMP_URLS=$(mktemp)

# Prompt user to paste URLs, each on a new line, and press Enter after each one. Press Enter on an empty line to finish.
echo "📥 Please paste the video or playlist URLs (one per line), press Enter after each one. When done, press Enter on an empty line to finish:"

# Read URLs from user input and store them in a temporary file
while true; do
    read -p "> " url
    [[ -z "$url" ]] && break  # Stop if the input is empty (empty line)
    echo "$url" >> "$TMP_URLS"  # Append URL to the temporary file
done

echo "🚀 Starting the download..."

# Loop through each URL and perform video and audio download
while read -r URL; do
    echo "🎬 Processing: $URL"

    # Download the video (MP4/WebM)
    yt-dlp -f "bv*+ba/bestvideo+bestaudio/best" 
    --merge-output-format mp4 
    --write-auto-sub --sub-lang en --convert-subs srt 
    --write-thumbnail --write-description --write-info-json 
    --embed-metadata 
    -o "%(upload_date)s_%(title)s.%(ext)s" "$URL"

    # Extract audio in Opus format
    yt-dlp -x --audio-format opus 
    --write-thumbnail --embed-thumbnail 
    --write-description --write-info-json 
    --write-auto-sub --sub-lang en --convert-subs srt 
    --embed-metadata 
    -o "%(upload_date)s_%(title)s_audio.%(ext)s" "$URL"

    echo "✅ Completed: $URL"
    echo ""
done < "$TMP_URLS"

# Clean up temporary file
rm "$TMP_URLS"

echo "🎉 All done! Video & Opus audio downloads completed."

Tutorial: How to Use This Script
Here’s a step-by-step guide to help you use the script correctly:

1. Install Dependencies
Before running the script, make sure that you have the necessary software installed:

yt-dlp: This is the tool used to download videos and audio.

Install yt-dlp:

pip install -U yt-dlp  # Using pip

# or
sudo apt install yt-dlp # For Debian/Ubuntu systems
2. Save the Script
Copy the script above and paste it into a file, say yt-dlp-download.sh.

Make the script executable:

chmod +x yt-dlp-download.sh

3. Run the Script
Now, run the script using:

./yt-dlp-download.sh

The script will prompt you to paste URLs.

4. Input URLs
After running the script, you will be prompted to paste the URLs of the videos or playlists you want to download.

Paste each URL one by one, and press Enter after each one.

When you’re done, just press Enter on an empty line (i.e., an empty input) to finish entering the URLs.

Example:

> https://www.youtube.com/watch?v=abc123
> https://www.youtube.com/watch?v=xyz456
>

5. Download Starts Automatically
Once you press Enter on the empty line, the script will start downloading:

It will first download the video in the best quality (MP4 or WebM).

It will then extract the audio in Opus format.

Each video will be processed and saved to the current directory with the format:
YYYYMMDD_Title.extension (for videos)
YYYYMMDD_Title_audio.extension (for audio).

6. Script Completion
Once all URLs are processed, the script will clean up temporary files and print:

🎉 All done! Video & Opus audio downloads completed.

Advanced Customization (Optional)
You can customize the script to fit your needs:

1. Change the output format: By default, the video is downloaded in MP4. If you want it in WebM or other formats, change the -f option in the script.

2. Audio formats: The script extracts audio in Opus. To extract audio in another format (e.g., MP3), modify the –audio-format option:

To download audio in MP3:

yt-dlp -x --audio-format mp3 ...

3. Additional options: You can add more options (like subtitle downloads, metadata, etc.) based on your requirements. Check the yt-dlp documentation for more advanced features.
Troubleshooting
If the script doesn’t work as expected, double-check that you’ve properly installed yt-dlp and that you are running the script from the directory where it’s located.

If you encounter issues like download failures or format errors, ensure that your yt-dlp version is up to date:

yt-dlp --update

😎