Skip to content

Commit cbacd3c

Browse files
committed
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.
1 parent 26f1b4e commit cbacd3c

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ public List<AudioStream> getAudioStreams() throws IOException, ExtractionExcepti
195195
try {
196196
final JsonArray transcodings = track.getObject("media").getArray("transcodings");
197197

198+
// Iterate a first time to see if there is a progressive MP3 stream available.
199+
// If yes, the MP3 HLS stream will be not added to audioStreams.
200+
201+
boolean mp3ProgressiveStreamInTranscodings = false;
202+
203+
for (final Object transcoding : transcodings) {
204+
final JsonObject t = (JsonObject) transcoding;
205+
if (t.getString("preset").contains("mp3") &&
206+
t.getObject("format").getString("protocol").equals("progressive")) {
207+
mp3ProgressiveStreamInTranscodings = true;
208+
}
209+
}
210+
198211
// Get information about what stream formats are available
199212
for (final Object transcoding : transcodings) {
200213
final JsonObject t = (JsonObject) transcoding;
@@ -205,6 +218,13 @@ public List<AudioStream> getAudioStreams() throws IOException, ExtractionExcepti
205218

206219
if (!isNullOrEmpty(url)) {
207220
if (t.getString("preset").contains("mp3")) {
221+
// Don't add the MP3 HLS stream if there is a progressive stream present
222+
// because the two have the same bitrate
223+
if (t.getObject("format").getString("protocol").equals("hls") &&
224+
mp3ProgressiveStreamInTranscodings) {
225+
continue;
226+
}
227+
208228
mediaFormat = MediaFormat.MP3;
209229
bitrate = 128;
210230
} else if (t.getString("preset").contains("opus")) {
@@ -222,23 +242,28 @@ public List<AudioStream> getAudioStreams() throws IOException, ExtractionExcepti
222242
final String res = dl.get(url).responseBody();
223243

224244
try {
225-
JsonObject mp3UrlObject = JsonParser.object().from(res);
245+
final JsonObject mp3UrlObject = JsonParser.object().from(res);
226246
// Links in this file are also only valid for a short period.
227247
mediaUrl = mp3UrlObject.getString("url");
228248
} catch (final JsonParserException e) {
229249
throw new ParsingException("Could not parse streamable url", e);
230250
}
231251
} else if (t.getObject("format").getString("protocol").equals("hls")) {
252+
232253
// This url points to the endpoint which generates a unique and short living url to the stream.
233254
url += "?client_id=" + SoundcloudParsingHelper.clientId();
234255
final String res = dl.get(url).responseBody();
235256

236257
try {
237258
final JsonObject mp3HlsUrlObject = JsonParser.object().from(res);
238259
// Links in this file are also only valid for a short period.
239-
240260
// Parsing the HLS manifest to get a single file by requesting a range equal to 0-track_length
241-
final String hlsManifestResponse = dl.get(mp3HlsUrlObject.getString("url")).responseBody();
261+
final String hlsManifestResponse;
262+
try {
263+
hlsManifestResponse = dl.get(mp3HlsUrlObject.getString("url")).responseBody();
264+
} catch (final IOException e) {
265+
continue;
266+
}
242267
final List<String> hlsRangesList = new ArrayList<>();
243268
final Matcher regex = Pattern.compile("((https?):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?+-=\\\\.&]*)")
244269
.matcher(hlsManifestResponse);

0 commit comments

Comments
 (0)