diff --git a/spotify_dl/spotify_dl.py b/spotify_dl/spotify_dl.py index 11136ba8..cfa2da70 100755 --- a/spotify_dl/spotify_dl.py +++ b/spotify_dl/spotify_dl.py @@ -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') @@ -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']): @@ -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() + + diff --git a/spotify_dl/youtube.py b/spotify_dl/youtube.py index 478092f7..6536cfb5 100644 --- a/spotify_dl/youtube.py +++ b/spotify_dl/youtube.py @@ -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 @@ -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}") @@ -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))