Why this script
- Lightweight and self-contained.
- Uses
yt-dlp(actively maintained fork of youtube-dl) to fetch best video + audio and merge to MP4. - Auto-installs
yt-dlpif not present, so you can get started quickly.
Requirements
- Python 3.8 or newer
- pip available
- Internet connection
- (Optional)
ffmpegon PATH for merging (yt-dlp will usually bundle merging if available)
Quick install
Option A — let the script auto-install yt-dlp (default behavior):
python youtubevideodownloder.py
Option B — install manually before running:
pip install --upgrade yt-dlp
How to use
- Save the script as
youtubevideodownloder.py. - Run the script:
python youtubevideodownloder.py
- When prompted, paste the YouTube video URL and press Enter. The video will be saved into a
downloads/directory by default.
Script (ready to paste)
import os
import re
import sys
import subprocess
import yt_dlp
def install_yt_dlp():
"""
Install or upgrade yt-dlp.
"""
try:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'yt-dlp'])
print("yt-dlp installed/upgraded successfully.")
except subprocess.CalledProcessError:
print("Failed to install yt-dlp. Please install manually using:")
print("pip install --upgrade yt-dlp")
def sanitize_filename(filename):
"""
Sanitize filename to remove invalid characters and limit length.
"""
filename = re.sub(r'[^\w\-_\. ]', '_', filename)
return filename.strip()[:200]
def download_youtube_video(youtube_url, output_dir='downloads'):
"""
Download a YouTube video in HD format.
"""
os.makedirs(output_dir, exist_ok=True)
try:
ydl_opts = {
'format': 'bestvideo+bestaudio/best',
'merge_output_format': 'mp4',
'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'),
'nooverwrites': True,
'no_color': True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([youtube_url])
print("Download completed successfully.")
except Exception as e:
print(f"An error occurred: {e}")
def main():
# Install or upgrade yt-dlp
install_yt_dlp()
video_url = input("Enter the YouTube video URL: ")
download_youtube_video(video_url)
if __name__ == "__main__":
main()
Explanation of important parts
install_yt_dlp()uses the current Python interpreter to runpip install --upgrade yt-dlpso the script can run even on fresh systems.sanitize_filename()replaces unsafe characters and truncates long names to avoid filesystem errors.download_youtube_video()setsformattobestvideo+bestaudio/best— this asks yt-dlp to download the best separate video and audio streams (if available) and merge them; otherwise it falls back to the best single stream.merge_output_format: 'mp4'tells yt-dlp to produce an MP4 file after merging.
Customization ideas
- Add command-line flags with
argparseto accept--url,--output, or--no-install. - Change the template
outtmplto include%(id)sor timestamps to avoid collisions. - Force a specific resolution, e.g.
format: 'bestvideo[height<=720]+bestaudio/best'to limit downloads to 720p.
Troubleshooting
- If automatic installation fails, run:
pip install --upgrade yt-dlp
-
If merging fails (errors mentioning ffmpeg), install
ffmpegand add it toPATH. -
For verbose debugging, run:
yt-dlp -v <VIDEO_URL>
Legal & Ethical note
Only download videos you have permission to download. Respect copyright and YouTube’s Terms of Service.
Where the script saves files
By default the script creates a downloads/ directory next to the script and saves files as the video title (sanitized). You can change output_dir when calling download_youtube_video() or modify outtmpl in the ydl_opts dictionary.