mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-12 23:45:18 +00:00
[Feature] Add Duration Match Algorithm
Added duration matching option to track matching algorithms
This commit is contained in:
parent
c3bf5119eb
commit
a550d21b9a
@ -278,7 +278,11 @@ class Settings extends HookConsumerWidget {
|
||||
value: SpotubeTrackMatchAlgorithm.popular,
|
||||
),
|
||||
DropdownMenuItem(
|
||||
child: Text("YouTube's choice is my choice"),
|
||||
child: Text("Match Song Duration"),
|
||||
value: SpotubeTrackMatchAlgorithm.duration,
|
||||
),
|
||||
DropdownMenuItem(
|
||||
child: Text("YouTube's Top choice"),
|
||||
value: SpotubeTrackMatchAlgorithm.youtube,
|
||||
),
|
||||
],
|
||||
|
@ -10,6 +10,8 @@ enum SpotubeTrackMatchAlgorithm {
|
||||
popular,
|
||||
// selects the most popular one from the author of the track
|
||||
authenticPopular,
|
||||
// selects song that most closely matches the actual song's duration
|
||||
duration,
|
||||
}
|
||||
|
||||
class SpotubeTrack extends Track {
|
||||
|
@ -375,7 +375,25 @@ class Playback extends PersistedChangeNotifier {
|
||||
} else {
|
||||
VideoSearchList videos =
|
||||
await raceMultiple(() => youtube.search.search(queryString));
|
||||
if (matchAlgorithm != SpotubeTrackMatchAlgorithm.youtube) {
|
||||
|
||||
if (matchAlgorithm == SpotubeTrackMatchAlgorithm.duration) {
|
||||
//Actual duration of desired song
|
||||
int targetDuration = track.duration!.inSeconds;
|
||||
//start with the first result
|
||||
Video bestVideoMatch = videos[0];
|
||||
int minDurationDifference =
|
||||
(targetDuration - videos[0].duration!.inSeconds).abs();
|
||||
//Check if any other results are closer to the actual song duration and prefer those
|
||||
for (int i = 1; i < videos.length; i++) {
|
||||
int durationDifference =
|
||||
(targetDuration - videos[i].duration!.inSeconds).abs();
|
||||
if (durationDifference < minDurationDifference) {
|
||||
minDurationDifference = durationDifference;
|
||||
bestVideoMatch = videos[i];
|
||||
}
|
||||
}
|
||||
ytVideo = bestVideoMatch;
|
||||
} else if (matchAlgorithm != SpotubeTrackMatchAlgorithm.youtube) {
|
||||
List<Map> ratedRankedVideos = videos
|
||||
.map((video) {
|
||||
// the find should be lazy thus everything case insensitive
|
||||
|
Loading…
Reference in New Issue
Block a user