Skip to content

Commit

Permalink
Possibly fix misdetections as livestreams
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Sep 25, 2024
1 parent b3fde19 commit e4ec766
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,7 @@ public AudioItem loadVideo(@NotNull YoutubeAudioSourceManager source,
author = "Unknown artist";
}

TemporalInfo temporalInfo = TemporalInfo.fromRawData(
!playabilityStatus.get("liveStreamability").isNull(),
videoDetails.get("lengthSeconds"),
videoDetails.get("isLive").asBoolean(false)
);

TemporalInfo temporalInfo = TemporalInfo.fromRawData(playabilityStatus, videoDetails);
return buildAudioTrack(source, videoDetails, title, author, temporalInfo.durationMillis, videoId, temporalInfo.isActiveStream);
}

Expand Down
18 changes: 11 additions & 7 deletions common/src/main/java/dev/lavalink/youtube/track/TemporalInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@ private TemporalInfo(boolean isActiveStream, long durationMillis) {
this.durationMillis = durationMillis;
}

// normal video? but has liveStreamability: PRRBJOn_n-Y
// livestream: jfKfPfyJRdk

@NotNull
public static TemporalInfo fromRawData(boolean hasLivestreamability, JsonBrowser durationSecondsField, boolean isLive) {
long durationValue = durationSecondsField.asLong(0L);
public static TemporalInfo fromRawData(JsonBrowser playabilityStatus, JsonBrowser videoDetails) {
JsonBrowser durationField = videoDetails.get("lengthSeconds");
long durationValue = durationField.asLong(0L);

boolean hasLivestreamability = !playabilityStatus.get("liveStreamability").isNull();
boolean isLive = videoDetails.get("isLive").asBoolean(false)
|| videoDetails.get("isLiveContent").asBoolean(false);

if (hasLivestreamability) {
// Premieres have duration information, but act as a normal stream. When we play it, we don't know the
// current position of it since YouTube doesn't provide such information, so assume duration is unknown.
durationValue = 0;
}

// VODs are not really live streams, even though the response JSON indicates that it is.
// If it is actually live, then duration is also missing or 0.
boolean isActiveStream = hasLivestreamability || isLive;

return new TemporalInfo(
isActiveStream,
(isLive || hasLivestreamability) && durationValue == 0,
durationValue == 0 ? DURATION_MS_UNKNOWN : Units.secondsToMillis(durationValue)
);
}
Expand Down
1 change: 1 addition & 0 deletions v2/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ base {
dependencies {
api(projects.common)
compileOnly(libs.lavaplayer.v2)

implementation(libs.rhino.engine)
implementation(libs.nanojson)
compileOnly(libs.slf4j)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,7 @@ public AudioItem loadVideo(@NotNull YoutubeAudioSourceManager source,
author = "Unknown artist";
}

TemporalInfo temporalInfo = TemporalInfo.fromRawData(
!playabilityStatus.get("liveStreamability").isNull(),
videoDetails.get("lengthSeconds"),
false
);

TemporalInfo temporalInfo = TemporalInfo.fromRawData(playabilityStatus, videoDetails);
String thumbnailUrl = ThumbnailTools.getYouTubeThumbnail(videoDetails, videoId);

AudioTrackInfo info = new AudioTrackInfo(title, author, temporalInfo.durationMillis, videoId, temporalInfo.isActiveStream, WATCH_URL + videoId, thumbnailUrl, null);
Expand Down

0 comments on commit e4ec766

Please sign in to comment.