From 228a12ecd62053aef289cc553e4881007b7496f2 Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Mon, 25 Jan 2021 20:42:08 +0100 Subject: [PATCH] Add a check to don't show MP3 128kbps stream twice and catch IOException when fetching the HLS Manifest If a progressive stream is present in the transcodings, it's unnecessary to show twice an MP3 128kbps stream so if this is the case, the MP3 HLS stream will be not added to the audioStreams, else it will. This commit also catch fetching errors in HLS manifests parsing and don't add the corresponding stream if an error occurs. --- .../extractors/SoundcloudStreamExtractor.java | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java index dc79d05d0c..f6b9a0482d 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java @@ -192,6 +192,19 @@ public List getAudioStreams() throws IOException, ExtractionExcepti try { final JsonArray transcodings = track.getObject("media").getArray("transcodings"); + // Iterate a first time to see if there is a progressive MP3 stream available. + // If yes, the MP3 HLS stream will be not added to audioStreams. + + boolean mp3ProgressiveStreamInTranscodings = false; + + for (final Object transcoding : transcodings) { + final JsonObject t = (JsonObject) transcoding; + if (t.getString("preset").contains("mp3") && + t.getObject("format").getString("protocol").equals("progressive")) { + mp3ProgressiveStreamInTranscodings = true; + } + } + // Get information about what stream formats are available for (final Object transcoding : transcodings) { final JsonObject t = (JsonObject) transcoding; @@ -202,6 +215,13 @@ public List getAudioStreams() throws IOException, ExtractionExcepti if (!isNullOrEmpty(url)) { if (t.getString("preset").contains("mp3")) { + // Don't add the MP3 HLS stream if there is a progressive stream present + // because the two have the same bitrate + if (t.getObject("format").getString("protocol").equals("hls") && + mp3ProgressiveStreamInTranscodings) { + continue; + } + mediaFormat = MediaFormat.MP3; bitrate = 128; } else if (t.getString("preset").contains("opus")) { @@ -219,13 +239,14 @@ public List getAudioStreams() throws IOException, ExtractionExcepti final String res = dl.get(url).responseBody(); try { - JsonObject mp3UrlObject = JsonParser.object().from(res); + final JsonObject mp3UrlObject = JsonParser.object().from(res); // Links in this file are also only valid for a short period. mediaUrl = mp3UrlObject.getString("url"); } catch (final JsonParserException e) { throw new ParsingException("Could not parse streamable url", e); } } else if (t.getObject("format").getString("protocol").equals("hls")) { + // This url points to the endpoint which generates a unique and short living url to the stream. url += "?client_id=" + SoundcloudParsingHelper.clientId(); final String res = dl.get(url).responseBody(); @@ -233,9 +254,13 @@ public List getAudioStreams() throws IOException, ExtractionExcepti try { final JsonObject mp3HlsUrlObject = JsonParser.object().from(res); // Links in this file are also only valid for a short period. - // Parsing the HLS manifest to get a single file by requesting a range equal to 0-track_length - final String hlsManifestResponse = dl.get(mp3HlsUrlObject.getString("url")).responseBody(); + final String hlsManifestResponse; + try { + hlsManifestResponse = dl.get(mp3HlsUrlObject.getString("url")).responseBody(); + } catch (final IOException e) { + continue; + } final List hlsRangesList = new ArrayList<>(); final Matcher regex = Pattern.compile("((https?):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?+-=\\\\.&]*)") .matcher(hlsManifestResponse);