flying_loaf_3
flying_loaf_3

Reputation: 422

Python library for downloading MP3 files from youtube

This is my first time on the forum so excuse me for not following conventions.

I'm trying to download mp3 files from youtube videos for music. I was wondering if there were any existing python libraries that could do this, by e.g:

searching youtube for the song OR accepting a string which it would search youtube for?

Thanks, Matt.

Upvotes: 4

Views: 14167

Answers (5)

matan h
matan h

Reputation: 1162

Update 2022 I no longer Maintains mhyt library.

you can try youtube_dl option:

To install:

pip install youtube_dl
# or for faster download and more improvements: 
#pip install yt-dlp

And use:

from youtube_dl import YoutubeDL
# or for yt_dlp: 
# from yt_dlp import YoutubeDL
ydl_opts = {
    'format': 'm4a/bestaudio/best',
    'postprocessors': [{  # Extract audio using ffmpeg
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
    }]
}
with YoutubeDL() as ydl:
   ydl.download(["https://www.youtube.com/watch?v=0BVqFYParRs"])

ORIGINAL ANSWER:

I have created a library that makes it simpler.

To install:

pip install mhyt
# or $ sudo pip install mhyt 

And use it like:

from mhyt import yt_download file = "file.mp3" tmp_file =
os.path.splitext(file)[0]+".webm"
yt_download("url","file.format",ismucic=True) 

Upvotes: 1

Ziad Ahmed
Ziad Ahmed

Reputation: 1

you can use pafy pip install pafy pafy documentation

Upvotes: 0

flying_loaf_3
flying_loaf_3

Reputation: 422

Revisiting this post since there seem to be many visits to my question recently. I re-did this project after a few years and ended up using youtube-dl, which can be installed using the following command:

sudo pip install --upgrade youtube_dl

Upvotes: 1

Gab
Gab

Reputation: 530

Indeed there is pytube for Python3.

Example Download of a audio file:

from pytube import YouTube

# creating YouTube object
yt = YouTube("https://www.youtube.com/watch?v=1csFTDXXULY") 

# accessing audio streams of YouTube obj.(first one, more available)
stream = yt.streams.filter(only_audio=True).first()
# downloading a video would be: stream = yt.streams.first() 

# download into working directory
stream.download()

Upvotes: 1

Abhijeetk431
Abhijeetk431

Reputation: 846

I know of a github link to what you seek. Visit this:- https://github.com/mps-youtube/pafy

Upvotes: 2

Related Questions