Skip to content

Instantly share code, notes, and snippets.

@mithi
Last active July 18, 2025 18:11
Show Gist options
  • Save mithi/534395432d8d3024634502bf24e7c2c1 to your computer and use it in GitHub Desktop.
Save mithi/534395432d8d3024634502bf24e7c2c1 to your computer and use it in GitHub Desktop.
Download MP3 and SRT Files from Youtube

Download MP3 and SRT Files from Youtube

Open your terminal and install Dependencies (yt-dlp)

$ pip install yt-dlp
$ brew install ffmpeg

Copy-paste this python script in your file download.py

import yt_dlp
import os

def download_youtube_audio_and_subtitles(youtube_url, output_dir="downloads"):
    """
    Download audio as MP3 and subtitles as SRT from a YouTube video.
    
    Args:
        youtube_url (str): The URL of the YouTube video
        output_dir (str): Directory to save the downloaded files
    """
    # Ensure output directory exists
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # yt-dlp options for downloading audio and subtitles
    ydl_opts = {
        'format': 'bestaudio/best',  # Select best audio quality
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',  # Convert to MP3
            'preferredquality': '192',  # Audio quality (kbps)
        }],
        'writesubtitles': True,  # Download subtitles
        'writeautomaticsub': True,  # Download auto-generated subtitles if available
        'subtitlesformat': 'srt',  # Subtitle format
        'subtitleslangs': ['ja', 'en'],  # Specify language (e.g., English)
        'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'),  # Output template
        'quiet': False,  # Show progress
        'no_warnings': True,  # Suppress warnings
    }

    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            # Download the video info and files
            info = ydl.extract_info(youtube_url, download=True)
            print(f"Successfully downloaded audio and subtitles for: {info['title']}")
            print(f"Files saved in: {output_dir}")
    except Exception as e:
        print(f"An error occurred: {str(e)}")

if __name__ == "__main__":
    youtube_url = input("Enter YouTube video URL: ")
    download_youtube_audio_and_subtitles(youtube_url)

Then run the script and enter the Youtube URL.

$ python3 download.py
Enter YouTube video URL: https://www.youtube.com/watch?v=eg65SbqmT0s

FAQ

Nothing in this document should be construed as legal advice. AI can make mistakes. Check important info.

Is Downloading Music a Crime?

  • Mass piracy (selling/downloading torrents)? → Potential criminal charges.
  • Personal use → Not a crime

You can use sites such as https://search.creativecommons.org/ to help you find music in platforms like Youtube or Sound Cloud. You can also ask explicit permission from music artists if you can use their music to help you study Japanese.

Is it ok to use third-party tools for downloading from Youtube?

Does It Violate Copyright Law?

No, if you have the right to download such as:

  • Your own videos → You own the rights
  • Public domain → No copyright restrictions
  • Creative Commons (CC) license → Downloading is allowed by the license
  • Explicit consent from owner → You have legal permission

However, YouTube's ToS is separate from copyright law, it may still breach YouTube’s rules.

Is breaching Youtube's TOS illegal?

Breaching YouTube's Terms of Service (ToS) is not inherently illegal (i.e., it won’t land you in jail). ToS is a contract, not criminal law. Violating it is a breach of contract, not a crime (in most cases).

Will YouTube Take Action?

Unlikely for personal or educational use. Risks are higher you mass-download or redistribute downloaded videos.

How can Youtube take action if you are not logged in?

What Youtube CANNOT Do If You’re Not Logged In:

  • Suspend/ban your Google account (since they can’t link activity to you).
  • Send copyright strikes (no account = no way to issue them).
  • Track you persistently (unless you’re logged in or they use browser fingerprinting)

What Youtube CAN Do If You’re Not Logged In:

  • If detected, they may temporarily block your IP from accessing YouTube or temporarily make downloads painfully slow on your device
  • In the future, YouTube could alter its encryption (like Netflix/HBO does)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment