# downlaod python 3.13.13
# Inside a terminal (Windows key, type "cmd" and press enter) install yt_dlp by using (pip install --no-user yt-dlp)
# after those 2 steps, run this py script

import yt_dlp

def download_video(url):
    ydl_opts = {
        'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
        'merge_output_format': 'mp4',
        'outtmpl': r"C:\Users\Riletin\Downloads\%(title)s.%(ext)s",
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        print(f"Downloading MP4: {url}")
        ydl.download([url])
        print("Download complete!")

def download_audio(url):
    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': r"C:\Users\Riletin\Downloads\%(title)s.%(ext)s",
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        print(f"Downloading MP3: {url}")
        ydl.download([url])
        print("Download complete!")

if __name__ == "__main__":
    link = input("Paste the YouTube URL here: ").strip()

    if not link:
        print("No link provided.")
        exit()

    choice = input("Choose format (mp3/mp4): ").strip().lower()

    if choice == "mp3":
        download_audio(link)
    elif choice == "mp4":
        download_video(link)
    else:
        print("Invalid choice. Please type 'mp3' or 'mp4'.")