Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download music from alternate YouTube URL #280

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
23 changes: 21 additions & 2 deletions spotify_dl/spotify_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def spotify_dl():
help='Specify download directory.', required=False, default = ".")
parser.add_argument('-d', '--download', action='store_true',
help='Download using youtube-dl', default=True)

# Newly added argument (by kinglobster). Specifies an alternative youtube url in case that the one
# determined by the program is mistaken or does not fulfill the requirements of quality or any other.
parser.add_argument('-y', '--alternative_yt_url', action='store', type=str, required=False,
help='Specify youtube url to download the song from. The metadata form spotify'
'and the audio from youtube will still merge in a single file.')

parser.add_argument('-f', '--format_str', type=str, action='store',
help='Specify youtube-dl format string.',
default='bestaudio/best')
Expand All @@ -49,7 +56,7 @@ def spotify_dl():

if os.path.isfile(os.path.expanduser('~/.spotify_dl_settings')):
with open(os.path.expanduser('~/.spotify_dl_settings')) as file:
config = json.loads(file.read())
config = json.loads(file)

for key, value in config.items():
if (isinstance(value, bool) and value) or (isinstance(value, str) and value and value.lower() in ['true', 't']):
Expand Down Expand Up @@ -99,8 +106,20 @@ def spotify_dl():
if args.keep_playlist_order:
file_name_f = playlist_num_filename
if save_path is not None:
download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order, args.no_overwrites, args.skip_non_music_sections, file_name_f)
# download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order, args.no_overwrites, args.skip_non_music_sections, file_name_f)
# From this line downwards, modified code by kinglobster

if args.alternative_yt_url is not None:
alternative_yt_url = args.alternative_yt_url
download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order,
args.no_overwrites, args.skip_non_music_sections, file_name_f, alternative_yt_url)
else:
download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order,
args.no_overwrites, args.skip_non_music_sections, file_name_f)

# End of modified code by kinglobster

if __name__ == '__main__':
spotify_dl()


17 changes: 14 additions & 3 deletions spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def playlist_num_filename(song):

def download_songs(songs, download_directory, format_string, skip_mp3,
keep_playlist_order=False, no_overwrites=False, skip_non_music_sections=False,
file_name_f=default_filename):
file_name_f=default_filename, alternative_yt_url=None):
"""
Downloads songs from the YouTube URL passed to either current directory or download_directory, is it is passed.
:param songs: Dictionary of songs and associated artist
Expand All @@ -32,6 +32,7 @@ def download_songs(songs, download_directory, format_string, skip_mp3,
:param no_overwrites: Whether we should avoid overwriting the song if it already exists
:param skip_non_music_sections: Whether we should skip Non-Music sections using SponsorBlock API
:param file_name_f: optional func(song) -> str that returns a filename for the download (without extension)
:param alternative_yt_url: added by kinglobster, contains alternative youtube video to download the song from
"""
overwrites = not no_overwrites
log.debug(f"Downloading to {download_directory}")
Expand Down Expand Up @@ -70,13 +71,23 @@ def download_songs(songs, download_directory, format_string, skip_mp3,
mp3_postprocess_opts = {
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
'preferredquality': '320', # 320kbps is spotify's 'very high' quality. Was '192'
}
ydl_opts['postprocessors'].append(mp3_postprocess_opts.copy())

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try:
ydl.download([query])
# ydl.download([query])
# From this line, modified code by kinglobster

if alternative_yt_url is None:
ydl.download([query])
else:
print('###### Downloading from alternative url!!!: ' + alternative_yt_url)
ydl.download(alternative_yt_url)

# End of modified code

except Exception as e:
log.debug(e)
print('Failed to download: {}, please ensure YouTubeDL is up-to-date. '.format(query))
Expand Down