From cef66786015d2f113401c0443d05d39cf57682c8 Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sat, 14 Aug 2021 17:45:49 +0200 Subject: [PATCH] [YouTube] Use less requests when generating DASH manifests of OTF streams Also fix some comments --- .../youtube/YoutubeDashManifestCreator.java | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeDashManifestCreator.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeDashManifestCreator.java index 473f946252..26ffcf15fd 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeDashManifestCreator.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeDashManifestCreator.java @@ -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( @@ -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> responseHeaders = headRequestResponse - .responseHeaders(); + final Map> 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) {