Skip to content

Commit

Permalink
Add a check to don't show MP3 128kbps stream twice and catch IOExcept…
Browse files Browse the repository at this point in the history
…ion 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.
  • Loading branch information
AudricV committed Feb 5, 2021
1 parent 1ad7e5a commit 228a12e
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ public List<AudioStream> 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;
Expand All @@ -202,6 +215,13 @@ public List<AudioStream> 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")) {
Expand All @@ -219,23 +239,28 @@ public List<AudioStream> 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();

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<String> hlsRangesList = new ArrayList<>();
final Matcher regex = Pattern.compile("((https?):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?+-=\\\\.&]*)")
.matcher(hlsManifestResponse);
Expand Down

0 comments on commit 228a12e

Please sign in to comment.