How to Properly Configure and Use yt-dlp in n8n (Docker Version)
How to Properly Configure and Use yt-dlp in n8n (Docker Version)
This tutorial guides you through permanently installing yt-dlp in an n8n instance running in Docker, and configuring cookie files to bypass login verification and anti-bot measures on platforms like YouTube and Bilibili.
Workflow automation illustration
📌 Why Is This So Complex?
By default, n8n runs in a “clean” Docker container. Any tools you install on the host server (like yt-dlp) are not accessible inside the n8n container. Therefore, we must build a custom n8n image that includes yt-dlp as part of the n8n environment.
Docker container isolation - showing the relationship between host machine and containers
🚀 Phase 1: Permanently Install yt-dlp in n8n
We will use docker-compose and a Dockerfile to build an n8n image containing yt-dlp and its dependencies (Python, ffmpeg).
Step 1: Create n8n Deployment Directory
On your server, create a new directory to store your configuration files.
1 | # For example, create a directory named n8n-custom under /root/ |
Step 2: Create Dockerfile
In the /root/n8n-custom directory, create a file named Dockerfile.
1 | nano Dockerfile |
Paste the following content into the file. This configuration is based on Alpine Linux (the foundation of the official n8n image).
1 | # ----------------------------------------------- |
Dockerfile code editing illustration
Step 3: Create docker-compose.yml
In the same directory, create a docker-compose.yml file.
1 | nano docker-compose.yml |
Paste the following content into the file.
1 | version: '3.8' |
Step 4: Build and Start
In the /root/n8n-custom directory, run docker-compose.
1 | docker-compose up -d --build |
Docker will begin building your new image (this may take a few minutes), then start n8n.
Docker build process illustration
🍪 Phase 2: Configure Cookies to Bypass Verification
When yt-dlp accesses YouTube or Bilibili, it may fail due to bot detection on the server (such as “412 Precondition Failed” or “Sign in to confirm”). We must provide cookies.
Step 1: Obtain Cookie Files Locally
- On your local computer (not the server), install a cookie export extension in Chrome/Firefox (e.g., Get cookies.txt)
- Log in to
youtube.com, click the extension icon, export cookies, and save asyoutube_cookies.txt - Log in to
bilibili.com, click the extension icon, export cookies, and save asbilibili_cookies.txt
Browser cookie export extension illustration
Step 2: Upload Cookies to n8n Data Volume
You need to upload these two .txt files to n8n’s persistent data directory.
2.1 Find the Volume Path
Based on our docker-compose.yml, the volume name is n8n_data_custom. Since your directory is n8n-custom, the actual Docker volume name might be n8n-custom_n8n_data_custom.
1 | # Run this command to find the exact path on the host machine |
You’ll get a path like: /var/lib/docker/volumes/n8n-custom_n8n_data_custom/_data
2.2 Create and Paste Cookie Content
Use nano with the path you found in the previous step to create files and paste cookie content.
1 | # Create YouTube Cookie |
Step 3: ⚠️ Fix Cookie File Permissions (Critical!)
The files you just created are owned by the root user. The node user inside the n8n container doesn’t have permission to read (or write) them, which will cause Permission denied errors.
We must change the ownership of these files inside the container.
1 | # Force execute chown (change owner) as root user inside the container |
File permission fix illustration
🎯 Phase 3: Use yt-dlp in n8n
Everything is now ready.
Step 1: Find the Absolute Path of yt-dlp
Since we installed via Dockerfile, we need to know its exact path.
1 | docker exec n8n_custom_service which yt-dlp |
It will most likely return: /usr/bin/yt-dlp
Step 2: Use in “Execute Command” Node
In your n8n workflow, add an Execute Command node. In the Command field, you must use the absolute path and corresponding cookies.
n8n workflow configuration example
YouTube Example (Check Subtitles):
1 | /usr/bin/yt-dlp --cookies /home/node/.n8n/youtube_cookies.txt --list-subs "https://www.youtube.com/watch?v=EXAMPLE_VIDEO_ID" |
Bilibili Example (Check Subtitles):
1 | /usr/bin/yt-dlp --cookies /home/node/.n8n/bilibili_cookies.txt --list-subs "https://www.bilibili.com/video/EXAMPLE_VIDEO_ID" |
🎊 Common Commands Reference
Download Video (Best Quality)
1 | /usr/bin/yt-dlp --cookies /home/node/.n8n/youtube_cookies.txt -f "bestvideo+bestaudio" "VIDEO_URL" -o "/home/node/.n8n/downloads/%(title)s.%(ext)s" |
Download Audio Only
1 | /usr/bin/yt-dlp --cookies /home/node/.n8n/youtube_cookies.txt -x --audio-format mp3 "VIDEO_URL" -o "/home/node/.n8n/downloads/%(title)s.%(ext)s" |
Download Subtitles
1 | /usr/bin/yt-dlp --cookies /home/node/.n8n/youtube_cookies.txt --write-subs --sub-lang en "VIDEO_URL" |
🔧 Troubleshooting
Issue 1: Permission denied
Solution: Make sure you’ve executed the chown command to fix cookie file permissions.
Issue 2: yt-dlp: command not found
Solution: Make sure to use the absolute path /usr/bin/yt-dlp, not just yt-dlp.
Issue 3: 412 Precondition Failed
Solution: Cookies may have expired, you need to export and upload new cookie files.
✅ Summary
- ✅ Built a custom n8n Docker image containing yt-dlp
- ✅ Configured cookie files for YouTube and Bilibili
- ✅ Fixed file permission issues
- ✅ Successfully used yt-dlp in n8n workflows
Your n8n is now fully configured and ready to use yt-dlp reliably! 🎉
📚 References
How to Properly Configure and Use yt-dlp in n8n (Docker Version)
https://xxc.li/2025/11/19/How-to-Configure-and-Use-yt-dlp-in-n8n-Docker/