4

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.

flying_loaf_3
  • 397
  • 2
  • 3
  • 12

5 Answers5

2

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

Abhijeetk431
  • 847
  • 1
  • 8
  • 18
  • Thanks for the reply. I am fairly new to this stuff, and I was wondering how you would install this as a library via command line? – flying_loaf_3 Aug 24 '17 at 09:33
1

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()
Gab
  • 512
  • 4
  • 7
1

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) 
matan h
  • 900
  • 1
  • 10
  • 19
1

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
flying_loaf_3
  • 397
  • 2
  • 3
  • 12
0

you can use pafy pip install pafy pafy documentation