Created
December 9, 2021 09:27
-
-
Save mzsrtgzr2/d158c7411f93871248a1904010bca62d to your computer and use it in GitHub Desktop.
Download mp3 songs from Youtube
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://medium.com/@mklaben15/using-python-to-download-a-youtube-playlist-and-convert-to-mp3-for-a-custom-spotify-library-87ab950958a7 | |
| from pytube import YouTube | |
| from pytube import Playlist | |
| import os | |
| import moviepy.editor as mp | |
| import re | |
| from youtubesearchpython import PlaylistsSearch | |
| for search in ( | |
| 'happy songs', 'electronic chill'): | |
| print('searching playlist for', search) | |
| playlistsSearch = PlaylistsSearch(search, limit = 3) | |
| for item in playlistsSearch.result()['result']: | |
| print('downloading list', item['link']) | |
| playlist = Playlist(item['link']) | |
| folder = 'songs' | |
| for url in playlist: | |
| print('downloading video', url) | |
| YouTube(url).streams.filter(only_audio=True).first().download(folder) | |
| folder = 'songs' | |
| print('converting...') | |
| for file in os.listdir(folder): | |
| mp4_path = os.path.join(folder,file) | |
| if re.search('mp4', file): | |
| mp3_path = os.path.join(folder,os.path.splitext(file)[0]+'.mp3') | |
| new_file = mp.AudioFileClip(mp4_path) | |
| print('converting', mp3_path) | |
| new_file.write_audiofile(mp3_path) | |
| os.remove(mp4_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment