Skip to content

Instantly share code, notes, and snippets.

@benzap
Last active May 3, 2022 05:39
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save benzap/90ff22790bc0a9c6fd2902e91da4baef to your computer and use it in GitHub Desktop.
Save benzap/90ff22790bc0a9c6fd2902e91da4baef to your computer and use it in GitHub Desktop.
Youtube to MP3 Downloader Script
#!/bin/env python
# Requires: youtube_dl module
# Requires: ffmpeg
# Usage:
#
# python youtube2mp3.py <URL>, ...
#
# Example:
#
# python youtube2mp3.py https://www.youtube.com/watch?v=dQw4w9WgXcQ
import youtube_dl
import sys
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
if __name__ == "__main__":
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
filenames = sys.argv[1:]
ydl.download(filenames)
@ivanleoncz
Copy link

Works like a charm! Tks ;)

@Akarius
Copy link

Akarius commented Feb 3, 2019

Fast and easy! Thanks.

@RajdipEminent
Copy link

Thank you for providing this script by this forum. it's really helpful to convert youtube video to mp3.

@kidsonfilms-python-rules

Rick Rolled, nice

@Isaac-Davidson
Copy link

Isaac-Davidson commented Jan 17, 2021

I heard about this service and I used it for a week and it was the worst week of my life. I don't know how to explain this, but the fact remains that the service works very badly and I don't know how to deal with it ... As a result, I realized that I urgently needed an alternative because I regularly had to deal with Yt mp3 convert one way or another. In the end, I found this service and I have been using it for over 3 months now and I am very satisfied. The service is free and fast. I noticed that there are fewer and fewer free services lately and this cannot but disappoint ... And what services do you use?

@kmrnzynlv
Copy link

hi, when i run it, it does download webm file. wasn't it supposed to be mp3 because of 'preferredcodec': 'mp3'?

@Phi999
Copy link

Phi999 commented Jul 4, 2021

where should I put the FFmpeg encoder?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment