Skip to content

Instantly share code, notes, and snippets.

@sshopov
Created July 1, 2024 07:28
Show Gist options
  • Save sshopov/8f1de3f7fc53ad0f6eb03aea86e5cb3d to your computer and use it in GitHub Desktop.
Save sshopov/8f1de3f7fc53ad0f6eb03aea86e5cb3d to your computer and use it in GitHub Desktop.

Revisions

  1. sshopov created this gist Jul 1, 2024.
    24 changes: 24 additions & 0 deletions youtube2mp3.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import youtube_dl
    from pydub import AudioSegment

    # Define the YouTube video URL
    url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
    url = "https://www.youtube.com/watch?v=21nt2kbj5Kk" #Goalie Pre Game Visualization

    # Download the video using youtube_dl
    ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
    'key': 'FFmpegExtractAudio',
    'preferredcodec': 'mp3',
    'preferredquality': '192',
    }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([url])

    # Load the downloaded video
    video = AudioSegment.from_file("{}.mp3".format(url.split("=")[-1]), format="mp3")

    # Save the video as an MP3 file
    video.export("song.mp3", format="mp3")