Skip to content

Commit

Permalink
--skip_non_music_sections (#256)
Browse files Browse the repository at this point in the history
Co-authored-by: OlegSea <olegsea1334@gmail.com>
  • Loading branch information
OlegSea and OlegSea committed Mar 22, 2022
1 parent 4432f7f commit df84b3d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 4 additions & 1 deletion spotify_dl/spotify_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def spotify_dl():
help='Whether to keep original playlist ordering or not.')
parser.add_argument('-m', '--skip_mp3', action='store_true',
help='Don\'t convert downloaded songs to mp3')
parser.add_argument('-s', '--skip_non_music_sections', default=False,
action='store_true',
help='Whether to skip non-music sections using SponsorBlock API.')
parser.add_argument('-w', '--no-overwrites', action='store_true',
help="Whether we should avoid overwriting the target audio file if it already exists",
default=False)
Expand Down Expand Up @@ -96,7 +99,7 @@ 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, 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)


if __name__ == '__main__':
Expand Down
18 changes: 16 additions & 2 deletions spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def playlist_num_filename(song):


def download_songs(songs, download_directory, format_string, skip_mp3,
keep_playlist_order=False, no_overwrites=False, file_name_f=default_filename):
keep_playlist_order=False, no_overwrites=False, skip_non_music_sections=False,
file_name_f=default_filename):
"""
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 @@ -29,6 +30,7 @@ def download_songs(songs, download_directory, format_string, skip_mp3,
:param skip_mp3: Whether to skip conversion to MP3
:param keep_playlist_order: Whether to keep original playlist ordering. Also, prefixes songs files with playlist num
: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)
"""
overwrites = not no_overwrites
Expand All @@ -40,6 +42,8 @@ def download_songs(songs, download_directory, format_string, skip_mp3,
file_name = file_name_f(song)
file_path = path.join(download_directory, file_name)

sponsorblock_remove_list = ['music_offtopic'] if skip_non_music_sections else []

outtmpl = f"{file_path}.%(ext)s"
ydl_opts = {
'format': format_string,
Expand All @@ -48,6 +52,16 @@ def download_songs(songs, download_directory, format_string, skip_mp3,
'default_search': 'ytsearch',
'noplaylist': True,
'no_color': False,
'postprocessors': [
{
'key': 'SponsorBlock',
'categories': sponsorblock_remove_list,
},
{
'key': 'ModifyChapters',
'remove_sponsor_segments': ['music_offtopic'],
'force_keyframes': True,
}],
'postprocessor_args': ['-metadata', 'title=' + song.get('name'),
'-metadata', 'artist=' + song.get('artist'),
'-metadata', 'album=' + song.get('album')]
Expand All @@ -58,7 +72,7 @@ def download_songs(songs, download_directory, format_string, skip_mp3,
'preferredcodec': 'mp3',
'preferredquality': '192',
}
ydl_opts['postprocessors'] = [mp3_postprocess_opts.copy()]
ydl_opts['postprocessors'].append(mp3_postprocess_opts.copy())

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
try:
Expand Down

0 comments on commit df84b3d

Please sign in to comment.