This is a command-line tool for downloading YouTube videos in various qualities, specifically transcoded to h.264 MP4 format.
The scrape tool allows users to easily download YouTube videos by providing a URL. It automatically fetches available video qualities, prompts the user for their choice, and handles the entire download and transcoding process. It is designed to be simple, efficient, and self-contained, requiring no manual installation of external software like ffmpeg.
- Simple Video Downloads: Scrape any public YouTube video using its URL.
- Quality Selection: Automatically lists available MP4 video qualities (e.g., 1080p, 720p) and lets the user choose.
- h.264 MP4 Output: Downloads and merges streams into the standard h.264 MP4 format.
- Smart Output Location: Defaults to saving files in the user's
Downloadsfolder on macOS. A custom output directory can be specified with the--outputflag. - Real-time Progress Bar: Displays a
tqdmprogress bar to monitor the download speed and progress for both video and audio streams. - No External Dependencies: Automatically uses a Python-managed
ffmpegexecutable, so no system-wide installation is required. - Flexible Usage: Supports two ways of providing the URL:
- As a direct command-line argument (requires quotes).
- Via an interactive prompt if the script is run without a URL.
- Simple Alias: Can be installed as a
scrapecommand, making it accessible from anywhere in the terminal.
The tool follows this sequence to process a video:
- Initiation: The user runs the
scrapecommand, either with a YouTube URL as an argument or without. - URL Input: If no URL is provided, the script prompts the user to paste one.
- Quality Fetching: Using the
yt-dlplibrary, the script contacts YouTube to get a list of all available video-only MP4 streams for the given URL. - User Selection: The script displays a clean list of resolutions (e.g., 1080p, 720p) and prompts the user to choose one.
- Downloading: The tool initiates the download for the best video stream and the best audio stream (
m4a) that match the selected quality. - Progress Tracking: Separate progress bars are shown for the video and audio downloads, indicating the percentage complete, download size, and speed.
- Merging & Transcoding: Once the separate streams are downloaded, the script automatically uses a local
ffmpegexecutable (provided by theimageio-ffmpeglibrary) to merge them into a single, final MP4 file. - Cleanup: The temporary video and audio files are automatically deleted, leaving only the final MP4 file in the designated output folder.
graph TD
A[Start] --> B{URL Provided?};
B -- Yes --> D[Fetch Qualities];
B -- No --> C[Prompt for URL];
C --> D;
D --> E[Display Qualities & Ask for Choice];
E --> F[User Selects Quality];
F --> G[Download Video & Audio Streams];
G --> H{Show Progress Bars};
H --> I[Merge Streams with FFmpeg];
I --> J[Create Final MP4 File];
J --> K[Cleanup Temporary Files];
K --> L[End];
-
Prerequisites: Ensure you have Python 3 and
pip3installed on your system. -
Clone the Repository (Example):
git clone <your-repository-url> cd yt-scraper
-
Install Dependencies: Install all the required Python libraries from the
requirements.txtfile.pip3 install -r requirements.txt
-
Set Up the Command-Line Alias: To use the tool as a simple
scrapecommand, move the script to a directory in your system'sPATHand make it executable. You will likely needsudofor this.sudo mv /path/to/your/project/yt-scraper/scrape /usr/local/bin/ sudo chmod +x /usr/local/bin/scrape
There are two primary ways to use the tool.
Method 1: Command-Line Argument
Pass the YouTube URL directly as an argument. You must wrap the URL in single quotes (' ') to prevent your shell from interpreting special characters in the URL.
# Download to the default Downloads folder
scrape 'https://www.youtube.com/watch?v=your_video_id'
# Download to a custom folder
scrape 'https://www.youtube.com/watch?v=your_video_id' --output /path/to/your/folderMethod 2: Interactive Prompt
Run the command without any arguments. It will prompt you to enter the URL.
scrapeIt will then display:
Please paste the YouTube URL and press Enter: <paste-your-url-here>
- yt-dlp: The core library for downloading video content from YouTube.
- tqdm: Used to create and manage the visual progress bars.
- imageio-ffmpeg: Provides a local, executable version of
ffmpegfor merging video and audio streams, avoiding the need for a system-wide installation.