Created
February 2, 2025 18:05
-
-
Save hypeitnow/4ce4780b1bb3336831e9437ceeb56fa4 to your computer and use it in GitHub Desktop.
yt_downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import subprocess | |
import sys | |
import shutil | |
import platform | |
def is_yt_dlp_installed(): | |
"""Check if yt-dlp is available in the system PATH.""" | |
return shutil.which("yt-dlp") is not None | |
def install_yt_dlp(): | |
""" | |
Install yt-dlp based on the detected operating system and package managers. | |
Returns True if the installation process was initiated. | |
""" | |
system = platform.system() | |
if system == "Linux": | |
# Check for common package managers. | |
if shutil.which("pacman"): | |
answer = input("yt-dlp is not installed. Install it using pacman? [y/N]: ") | |
if answer.lower() == 'y': | |
subprocess.run(["sudo", "pacman", "-S", "yt-dlp"], check=True) | |
return True | |
elif shutil.which("apt-get"): | |
answer = input("yt-dlp is not installed. Install it using apt-get? [y/N]: ") | |
if answer.lower() == 'y': | |
subprocess.run(["sudo", "apt-get", "install", "yt-dlp"], check=True) | |
return True | |
elif shutil.which("dnf"): | |
answer = input("yt-dlp is not installed. Install it using dnf? [y/N]: ") | |
if answer.lower() == 'y': | |
subprocess.run(["sudo", "dnf", "install", "yt-dlp"], check=True) | |
return True | |
else: | |
print("No supported package manager found. Please install yt-dlp manually.") | |
return False | |
elif system == "Darwin": # macOS | |
if shutil.which("brew"): | |
answer = input("yt-dlp is not installed. Install it using Homebrew? [y/N]: ") | |
if answer.lower() == 'y': | |
subprocess.run(["brew", "install", "yt-dlp"], check=True) | |
return True | |
else: | |
print("Homebrew is not found. Please install yt-dlp manually.") | |
return False | |
elif system == "Windows": | |
print("Please install yt-dlp manually from https://github.com/yt-dlp/yt-dlp") | |
return False | |
else: | |
print("Unsupported system. Please install yt-dlp manually.") | |
return False | |
def main(): | |
# Check if yt-dlp is installed. | |
if not is_yt_dlp_installed(): | |
answer = input("yt-dlp is not installed on your system. Would you like to install it? [y/N]: ") | |
if answer.lower() == 'y': | |
if not install_yt_dlp() or not is_yt_dlp_installed(): | |
print("Installation of yt-dlp failed or it is still not available. Exiting.") | |
sys.exit(1) | |
else: | |
print("yt-dlp is required. Exiting.") | |
sys.exit(1) | |
# Set defaults for string interpolation in help text | |
defaults = { | |
'prog': 'yt_download', | |
'url_example': 'https://youtube.com/watch?v=VIDEO_ID', | |
'output_example': '/path/to/output.mp4', | |
} | |
# Set up command-line arguments. | |
parser = argparse.ArgumentParser( | |
description="Download YouTube video, audio (mp3), or subtitles using yt-dlp.", | |
epilog=""" | |
Examples: | |
%(prog)s -m %(url_example)s # Download video in best quality | |
%(prog)s -a %(url_example)s # Download audio as MP3 | |
%(prog)s -s %(url_example)s --lang es # Download Spanish subtitles | |
%(prog)s -m %(url_example)s -o "%(output_example)s" # Custom filename | |
""" % defaults, | |
formatter_class=argparse.RawDescriptionHelpFormatter | |
) | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument("-s", "--subtitles", action="store_true", | |
help="Download subtitles only (auto-generated if available)") | |
group.add_argument("-m", "--video", action="store_true", | |
help="Download video in best available quality") | |
group.add_argument("-a", "--audio", action="store_true", | |
help="Download audio only and convert to MP3 format") | |
parser.add_argument("url", help="YouTube video URL (e.g., https://youtube.com/watch?v=...)") | |
parser.add_argument("--lang", default="en", | |
help="Subtitle language code (default: %(default)s). Example: en, es, fr, de") | |
parser.add_argument("-o", "--output", | |
help='Output path (e.g., "/path/to/video.mp4")') | |
args = parser.parse_args() | |
# Build the yt-dlp command based on the selected option. | |
if args.subtitles: | |
cmd = [ | |
"yt-dlp", | |
"--skip-download", # Do not download the video. | |
"--write-auto-subs", # Download auto-generated subtitles. | |
"--sub-lang", args.lang, # Subtitle language. | |
] | |
elif args.video: | |
cmd = [ | |
"yt-dlp", | |
"-f", "best", | |
] | |
elif args.audio: | |
cmd = [ | |
"yt-dlp", | |
"-f", "bestaudio", | |
"--extract-audio", | |
"--audio-format", "mp3", | |
] | |
else: | |
print("Please select a valid option: --subtitles, --video, or --audio.") | |
sys.exit(1) | |
if args.output: | |
cmd.extend(["-o", args.output]) | |
cmd.append(args.url) | |
print("Executing:", " ".join(cmd)) | |
# Execute the command. | |
try: | |
subprocess.run(cmd, check=True) | |
except subprocess.CalledProcessError as e: | |
print("An error occurred during download:", e) | |
sys.exit(e.returncode) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment