A Python-based application for downloading short video segments (30 seconds) from YouTube using yt-dlp. This project supports asynchronous downloads and is fully containerized with Docker for streamlined execution.
- Cookie Management: Automatically handle cookies file specified in
.env. - Asynchronous Video Downloads: Use
yt-dlpandffmpegto download the first 30 seconds of videos. - Logging: Comprehensive logging system for tracking operations.
- Dockerized: Fully containerized environment, eliminating local setup complexities.
- Docker and Docker Compose
.envfile with the path to your cookies file.
git clone <repository-url>
cd <repository-directory>Create a .env file in the project root and specify the path to your cookies file:
COOKIES_FILE_PATH=/path/to/your/youtube.com_cookies.txt
You may use a browser extension to export cookies, such as:
• Get cookies.txt LOCALLY for Chrome
• cookies.txt for Firefox
This cookies file is needed because YouTube uses Google’s authentication system, which can involve Two-Factor Authentication (2FA) when logging in. By using a cookies file, you can bypass the need to manually log in and enter the verification code every time the application interacts with YouTube.
The cookies file captures the session details after a successful login, including the token generated by Google Authenticator or other 2FA methods. This allows the application to authenticate automatically without requiring you to re-enter your credentials or 2FA code.
⚠️ Important: Be cautious when installing browser extensions. If you had previously installed the “Get cookies.txt” extension (not “LOCALLY”) for Chrome, it has been reported as malware and removed from the Chrome Web Store. Uninstall it immediately if it’s still present on your system.
The application will continuously monitor the input_dir volume for new loading_instructions.csv files and start processing them.
Steps to Use the Application
• Open the Chrome browser.
• Install one of the extensions mentioned in the Prepare .env File section:
• Log in to YouTube with your account.
• Use the extension to export cookies and save the file (e.g., into /Downloads).
• Navigate to the Directory of the project
• Open create_loading_instructions.py file.
• Update the sample_data list with the desired YouTube video URLs:
• Execute the script to copy the cookies file and create the loading_instructions.csv file:
python3 create_loading_instructions.pyOnce the CSV is created, the Docker container will automatically start processing the file if it’s running.
Note: You can open separate terminal windows to run new create_loading_instructions.py scripts at any time. This is useful if you want to create multiple loading_instructions.csv files and have them processed simultaneously, even if Docker is already running.
1. Build the Docker image:
docker-compose build2. Start the application:
docker-compose upproject/
│
├── main.py # Main script to monitor and process video downloads.
├── settings.py # Configuration and setup of directories and logging.
├── Dockerfile # Docker container setup.
├── docker-compose.yml # Docker Compose configuration.
├── requirements.txt # Python dependencies.
├── .env # Environment configuration.
├── input_dir/ # Directory for input files.
├── output_dir/ # Directory for downloaded videos.
└── logs_dir/ # Directory for application logs.
Configuration
.env file
# Example .env file for YouTube Video Loader
# Rename this file to `.env` and update the value below with the actual path.
# Path to the exported cookies file from your browser extension
COOKIES_FILE_PATH=/path/to/your/youtube.com_cookies.txt
Docker Volumes
The following directories are mapped as volumes:
• ./input_dir:/app/input_dir - Directory for input files.
• ./output_dir:/app/output_dir - Directory for downloaded videos.
• ./logs_dir:/app/logs_dir - Directory for application logs.
Logs
• Logs are saved in ./logs_dir/project.log and streamed to the console in real-time.