Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .cache

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
.cache
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Next install the required packages
```
pip3 install --user -r requirements.txt
```
Last, install `ffmpeg`. See: https://ffmpeg.org/download.html

# Spotify Credentials

Expand Down Expand Up @@ -81,4 +82,4 @@ $ chmod +x main.sh
$ ./main.sh <main download path>
```

# Have Fun!
# Have Fun!
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
youtube-search-python
pytube
pytubefix
spotipy
python-dotenv
httpx < 0.28
33 changes: 21 additions & 12 deletions spotify_to_mp3.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from spotipy.oauth2 import SpotifyClientCredentials
from youtubesearchpython import CustomSearch, VideoSortOrder
import os
from pytube import YouTube
from pytubefix import YouTube
import sys
import argparse
from dotenv import load_dotenv, find_dotenv
Expand All @@ -20,10 +20,11 @@

def you_tube_downloader(url, path):
yt = YouTube(url)
audio_file = yt.streams.filter(only_audio=True).first().download(path)
base, ext = os.path.splitext(audio_file)
new_file = base + '.mp3'
os.rename(audio_file, new_file)
video_file = yt.streams.filter(only_audio=True).first().download(path)
base, ext = os.path.splitext(video_file)
audio_file = base + '.mp3'
os.system("ffmpeg -y -stats -v 1 -i '"+video_file+"' -q:a 0 -map a '"+audio_file+"'")
os.remove(video_file)
print("Done")


Expand All @@ -37,15 +38,20 @@ def you_tube_downloader(url, path):
parser = argparse.ArgumentParser(description='If you are having problems running the script please go over the README file, make sure you have installed the necessary packages in the requirments.txt file and make sure you have updated the keys.env file with the correct credentials. Command line example: $ python3 spotify_to_mp3.py --path <download path for the mp3 folders> --uname <the spotify user name>')
parser.add_argument("--path", help="The path for the downloaded playlis folders", default='None')
parser.add_argument("--uname", help="The spotify username to download playlists from", default='None')
parser.add_argument("--playlist", help="The spotify playlist to download (optional, if not provided all playlists will be downloaded)", default='None')
args = parser.parse_args()
path = args.path
user_name = args.uname
in_playlist = args.playlist
if path == "None":
print("\n----- Welcome! -----\n")
print("To get started please input some details")
path = input("\n1. Please enter the directory path for the downloads:")
print("\nGreat, thank you.")
user_name = input("Now please enter the user name you want to fetch playlists for (The download will start automatically):")
print("\n----- Welcome! -----\n")
print("To get started please input some details")
path = input("\n1. Please enter the directory path for the downloads: ")
in_playlist = input("\n2. Please enter a specific playlist to download (just press [enter] do download all playlists): ")
if in_playlist == "":
in_playlist = None
print("\nGreat, thank you.")
user_name = input("Now please enter the user name you want to fetch playlists for (The download will start automatically): ")



Expand All @@ -72,6 +78,9 @@ def you_tube_downloader(url, path):

while playlists:
for playlist in playlists['items']:
if (in_playlist is not None and in_playlist != playlist['name']):
print("skipping " + playlist['name'])
continue
print("PLAY LIST: " + playlist['name'])
playlist_path = path + "/" + user_name + "/" + playlist['name']
playlist_details = sp.playlist_items(playlist['uri'], fields=None, limit=100, offset=0, market=None)
Expand All @@ -85,8 +94,8 @@ def you_tube_downloader(url, path):
song_url = custom_search.result()['result'][0]['link']
print(custom_search.result()['result'][0]['link'])
you_tube_downloader(song_url, playlist_path)
except:
print("could not download song")
except Exception as e:
print("could not download url. error: " + str(e))
if playlists['next']:
playlists = sp.next(playlists)
else:
Expand Down