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

Add max-duration-difference option #335

Open
wants to merge 2 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
4 changes: 3 additions & 1 deletion spotify_dl/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def fetch_tracks(sp, item_type, url):
fields="items.track.name,items.track.artists(name, uri),"
"items.track.album(name, release_date, total_tracks, images),"
"items.track.track_number,total, next,offset,"
"items.track.id",
"items.track.id,items.track.duration_ms",
additional_types=["track"],
offset=offset,
)
Expand All @@ -47,6 +47,7 @@ def fetch_tracks(sp, item_type, url):
track_artist = ",".join(
[artist["name"] for artist in track_info.get("artists")]
)
track_duration = track_info.get("duration_ms") / 1000
if track_album_info:
track_album = track_album_info.get("name")
track_year = (
Expand Down Expand Up @@ -78,6 +79,7 @@ def fetch_tracks(sp, item_type, url):
"name": track_name,
"artist": track_artist,
"album": track_album,
"duration": track_duration,
"year": track_year,
"num_tracks": album_total,
"num": track_num,
Expand Down
9 changes: 9 additions & 0 deletions spotify_dl/spotify_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ def spotify_dl():
default="",
help="Download through a proxy. Support HTTP & SOCKS5. Use 'http://username:password@hostname:port' or 'http://hostname:port'",
)
parser.add_argument(
"-mdd",
"--max-duration-difference",
action="store",
type=float,
default=-1,
help="Only download songs that match the track duration up to a maximum difference in seconds",
)
args = parser.parse_args()
num_cores = os.cpu_count()
args.multi_core = int(args.multi_core)
Expand Down Expand Up @@ -188,6 +196,7 @@ def spotify_dl():
output_dir=args.output,
format_str=args.format_str,
skip_mp3=args.skip_mp3,
max_duration_difference=args.max_duration_difference,
keep_playlist_order=args.keep_playlist_order,
no_overwrites=args.no_overwrites,
use_sponsorblock=args.use_sponsorblock,
Expand Down
27 changes: 21 additions & 6 deletions spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def playlist_num_filename(**kwargs):
return f"{kwargs['track_num']} - {default_filename(**kwargs)}"


def duration_delta(info, expected_duration, delta):
"""Download only videos that match maximum duration difference"""
duration = info.get('duration')
if abs(duration - expected_duration) > delta:
return 'The video length does not match'


def write_tracks(tracks_file, song_dict):
"""
Writes the information of all tracks in the playlist[s] to a text file in csv kind of format
Expand All @@ -45,6 +52,7 @@ def write_tracks(tracks_file, song_dict):
track_artist = track["artist"]
track_num = track["num"]
track_album = track["album"]
track_duration = track["duration"]
track["save_path"] = url_dict["save_path"]
track_db.append(track)
track_index = i
Expand All @@ -56,6 +64,7 @@ def write_tracks(tracks_file, song_dict):
str(track_num),
track_album,
str(track_index),
track_duration,
]
try:
writer.writerow(csv_row)
Expand All @@ -74,7 +83,7 @@ def set_tags(temp, filename, kwargs):
:param filename: location of song whose tags are to be editted
:param kwargs: a dictionary of extra arguments to be used in tag editing
"""
song = kwargs["track_db"][int(temp[-1])]
song = kwargs["track_db"][int(temp[5])]
try:
song_file = MP3(filename, ID3=EasyID3)
except mutagen.MutagenError as e:
Expand Down Expand Up @@ -122,12 +131,13 @@ def find_and_download_songs(kwargs):
reference_file = kwargs["reference_file"]
with open(reference_file, "r", encoding="utf-8") as file:
for line in file:
temp = line.split(";")
name, artist, album, i = (
temp = line.replace("\n", "").split(";")
name, artist, album, i, duration = (
temp[0],
temp[1],
temp[4],
int(temp[-1].replace("\n", "")),
int(temp[5]),
float(temp[6]),
)

query = f"{artist} - {name} Lyrics".replace(":", "").replace('"', "")
Expand Down Expand Up @@ -163,7 +173,8 @@ def find_and_download_songs(kwargs):
outtmpl = f"{file_path}.%(ext)s"
ydl_opts = {
"proxy": kwargs.get('proxy'),
"default_search": "ytsearch",
"default_search": "ytsearch10",
"max_downloads": 1,
"format": "bestaudio/best",
"outtmpl": outtmpl,
"postprocessors": sponsorblock_postprocessor,
Expand All @@ -178,6 +189,9 @@ def find_and_download_songs(kwargs):
"album=" + album,
],
}
if kwargs["max_duration_difference"] >= 0:
ydl_opts["match_filter"] = lambda info, *, incomplete: duration_delta(
info, duration, kwargs["max_duration_difference"])
if not kwargs["skip_mp3"]:
mp3_postprocess_opts = {
"key": "FFmpegExtractAudio",
Expand All @@ -190,7 +204,8 @@ def find_and_download_songs(kwargs):
ydl.download([query])
except Exception as e: # skipcq: PYL-W0703
log.debug(e)
print(f"Failed to download {name}, make sure yt_dlp is up to date")
if type(e) != yt_dlp.utils.MaxDownloadsReached:
print(f"Failed to download {name}, make sure yt_dlp is up to date")
if not kwargs["skip_mp3"]:
set_tags(temp, mp3filename, kwargs)

Expand Down