Skip to content

Commit

Permalink
[YouTube] Use less requests when generating DASH manifests of OTF str…
Browse files Browse the repository at this point in the history
…eams

Also fix some comments
  • Loading branch information
AudricV committed Aug 14, 2021
1 parent 4b08632 commit cef6678
Showing 1 changed file with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,11 @@ public static String createDashManifestFromOtfStreamingUrl(
final Downloader downloader = NewPipe.getDownloader();
final String responseBody;
try {
// First try to avoid redirects when streaming the content by fetching the base URL,
// which returns 404
// Use a head request in order to reduce the download size.
final Response headRequestResponse = downloader.head(otfBaseStreamingUrl,
null);
otfBaseStreamingUrl = headRequestResponse.latestUrl();
// Try to avoid redirects when streaming the content by saving the last URL we get
// from video servers.

final Response response = downloader.get(otfBaseStreamingUrl + "&sq=0");
otfBaseStreamingUrl = response.latestUrl().replace("&sq=0", "");
final int responseCode = response.responseCode();
if (responseCode != 200) {
throw new YoutubeDashManifestCreationException(
Expand Down Expand Up @@ -127,22 +124,20 @@ public static String createDashManifestFromPostLiveStreamDvrStreamingUrl(
}

try {
// First try to avoid redirects when streaming the content by fetching the base URL,
// which is the last segment of the media.
// Use a head request in order to reduce the download size.
final Response headRequestResponse = downloader.head(postLiveStreamDvrStreamingUrl
+ "&sq=0");
postLiveStreamDvrStreamingUrl = headRequestResponse.latestUrl()
.replace("&sq=0", "");
final int responseCode = headRequestResponse.responseCode();
// Try to avoid redirects when streaming the content by saving the latest URL we get
// from video servers.
// Use a HEAD request in order to reduce the download size.

final Response response = downloader.head(postLiveStreamDvrStreamingUrl + "&sq=0");
postLiveStreamDvrStreamingUrl = response.latestUrl().replace("&sq=0", "");
final int responseCode = response.responseCode();
if (responseCode != 200) {
throw new YoutubeDashManifestCreationException(
"Unable to create the DASH manifest: could not fetch the initialization URL of the post live DVR stream: response code "
+ responseCode);
}

final Map<String, List<String>> responseHeaders = headRequestResponse
.responseHeaders();
final Map<String, List<String>> responseHeaders = response.responseHeaders();
streamDuration = responseHeaders.get("X-Head-Time-Millis").get(0);
segmentCount = responseHeaders.get("X-Head-Seqnum").get(0);
} catch (final IOException | ReCaptchaException | IndexOutOfBoundsException e) {
Expand Down

0 comments on commit cef6678

Please sign in to comment.