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.
Convert a YouTube video to MP3.
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment