Skip to content

Commit 7f6b01e

Browse files
committed
More refactoring + started implementing ItagInfo
1 parent 16ae77e commit 7f6b01e

38 files changed

+669
-301
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ public Description getDescription() {
152152
public List<AudioStream> getAudioStreams() {
153153
return Collections.singletonList(
154154
new SimpleAudioStreamImpl(
155+
AudioFormatRegistry.MP3,
155156
new SimpleProgressiveHTTPDeliveryDataImpl(albumJson
156157
.getArray("trackinfo")
157158
.getObject(0)
158159
.getObject("file")
159160
.getString("mp3-128")),
160-
AudioFormatRegistry.MP3,
161161
128
162162
)
163163
);

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCLiveStreamExtractor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ public List<AudioStream> getAudioStreams() throws IOException, ExtractionExcepti
171171
}
172172

173173
return new SimpleAudioStreamImpl(
174-
deliveryData,
175174
// TODO: This looks wrong
176-
new AudioFormatRegistry().getFromSuffix(dto.getUrlKey())
175+
new AudioFormatRegistry().getFromSuffix(dto.getUrlKey()),
176+
deliveryData
177177
);
178178
})
179179
.collect(Collectors.toList());
@@ -196,9 +196,9 @@ public List<VideoAudioStream> getVideoStreams() throws IOException, ExtractionEx
196196
final JsonArray videoSize = dto.getStreamJsonObj().getArray("videoSize");
197197

198198
return new SimpleVideoAudioStreamImpl(
199-
deliveryData,
200199
// TODO: This looks wrong
201200
new VideoAudioFormatRegistry().getFromSuffix(dto.getUrlKey()),
201+
deliveryData,
202202
VideoQualityData.fromHeightWidth(
203203
/*height=*/videoSize.getInt(1, VideoQualityData.UNKNOWN),
204204
/*width=*/videoSize.getInt(0, VideoQualityData.UNKNOWN))

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCStreamExtractor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public String getUploaderAvatarUrl() {
9999
public List<AudioStream> getAudioStreams() throws ExtractionException {
100100
return getRecordingsByMimeType("audio")
101101
.map(o -> new SimpleAudioStreamImpl(
102-
new SimpleProgressiveHTTPDeliveryDataImpl(o.getString("recording_url")),
103-
new AudioFormatRegistry().getFromMimeType(o.getString("mime_type"))
102+
new AudioFormatRegistry().getFromMimeType(o.getString("mime_type")),
103+
new SimpleProgressiveHTTPDeliveryDataImpl(o.getString("recording_url"))
104104
))
105105
.collect(Collectors.toList());
106106
}
@@ -109,10 +109,8 @@ public List<AudioStream> getAudioStreams() throws ExtractionException {
109109
public List<VideoAudioStream> getVideoStreams() throws ExtractionException {
110110
return getRecordingsByMimeType("video")
111111
.map(o -> new SimpleVideoAudioStreamImpl(
112-
new SimpleProgressiveHTTPDeliveryDataImpl(o.getString("recording_url")),
113-
null,
114-
AudioStream.UNKNOWN_BITRATE,
115112
new VideoAudioFormatRegistry().getFromMimeType(o.getString("mime_type")),
113+
new SimpleProgressiveHTTPDeliveryDataImpl(o.getString("recording_url")),
116114
VideoQualityData.fromHeightWidth(
117115
o.getInt("height", VideoQualityData.UNKNOWN),
118116
o.getInt("width", VideoQualityData.UNKNOWN))

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,15 @@ private void tryExtractSubtitles() {
403403
.map(JsonObject.class::cast)
404404
.map(caption -> {
405405
try {
406-
final String url = baseUrl + JsonUtils.getString(caption,
407-
"captionPath");
406+
final String url =
407+
baseUrl + JsonUtils.getString(caption, "captionPath");
408408

409409
return new SimpleSubtitleStreamImpl(
410-
new SimpleProgressiveHTTPDeliveryDataImpl(url),
410+
// TODO: Check for null
411411
new SubtitleFormatRegistry()
412412
.getFromSuffix(
413413
url.substring(url.lastIndexOf(".") + 1)),
414+
new SimpleProgressiveHTTPDeliveryDataImpl(url),
414415
false,
415416
JsonUtils.getString(caption, "language.id")
416417
);
@@ -468,9 +469,9 @@ private void extractLiveVideoStreams() throws ParsingException {
468469
.filter(JsonObject.class::isInstance)
469470
.map(JsonObject.class::cast)
470471
// TODO Check! This is the master playlist!
471-
.map(stream -> new SimpleVideoAudioStreamImpl(
472-
new SimpleHLSDeliveryDataImpl(stream.getString(PLAYLIST_URL, "")),
473-
VideoAudioFormatRegistry.MPEG_4)
472+
.map(s -> new SimpleVideoAudioStreamImpl(
473+
VideoAudioFormatRegistry.MPEG_4,
474+
new SimpleHLSDeliveryDataImpl(s.getString(PLAYLIST_URL, "")))
474475
)
475476
// Don't use the containsSimilarStream method because it will always
476477
// return
@@ -502,9 +503,9 @@ private void addStreamsFromArray(
502503
stream,
503504
playlistUrl,
504505
(s, dd) -> new SimpleAudioStreamImpl(
505-
dd,
506506
new AudioFormatRegistry()
507-
.getFromSuffix(getExtensionFromStream(s))
507+
.getFromSuffix(getExtensionFromStream(s)),
508+
dd
508509
)
509510
);
510511

@@ -515,9 +516,9 @@ private void addStreamsFromArray(
515516
stream,
516517
playlistUrl,
517518
(s, dd) -> new SimpleVideoAudioStreamImpl(
518-
dd,
519519
new VideoAudioFormatRegistry()
520520
.getFromSuffix(getExtensionFromStream(s)),
521+
dd,
521522
VideoQualityData.fromHeightFps(
522523
resJson.getInt("id", VideoQualityData.UNKNOWN),
523524
stream.getInt("fps", VideoQualityData.UNKNOWN))

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.schabi.newpipe.extractor.NewPipe;
1616
import org.schabi.newpipe.extractor.StreamingService;
1717
import org.schabi.newpipe.extractor.downloader.Downloader;
18+
import org.schabi.newpipe.extractor.downloader.Response;
1819
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
1920
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
2021
import org.schabi.newpipe.extractor.exceptions.GeographicRestrictionException;
@@ -268,8 +269,8 @@ private List<AudioStream> extractAudioStreams() {
268269

269270
final String preset = transcoding.getString("preset", "");
270271

271-
AudioMediaFormat mediaFormat = null;
272-
int averageBitrate = AudioStream.UNKNOWN_BITRATE;
272+
final AudioMediaFormat mediaFormat;
273+
final int averageBitrate;
273274
if (preset.contains("mp3")) {
274275
// Don't add the MP3 HLS stream if there is a progressive stream present
275276
// because the two have the same bitrate
@@ -281,13 +282,15 @@ private List<AudioStream> extractAudioStreams() {
281282
} else if (preset.contains("opus")) {
282283
mediaFormat = AudioFormatRegistry.OPUS;
283284
averageBitrate = 64;
285+
} else {
286+
return null;
284287
}
285288

286289
return (AudioStream) new SimpleAudioStreamImpl(
290+
mediaFormat,
287291
protocol.equals("hls")
288292
? new SimpleHLSDeliveryDataImpl(mediaUrl)
289293
: new SimpleProgressiveHTTPDeliveryDataImpl(mediaUrl),
290-
mediaFormat,
291294
averageBitrate
292295
);
293296
})
@@ -321,7 +324,17 @@ private Optional<AudioStream> extractDownloadableFileIfAvailable() {
321324
if (isNullOrEmpty(downloadUrl)) {
322325
return Optional.empty();
323326
}
327+
328+
// Find out what type of file is served
329+
final String fileType = determineFileTypeFromDownloadUrl(downloadUrl);
330+
331+
// No fileType found -> ignore it
332+
if (isNullOrEmpty(fileType)) {
333+
return Optional.empty();
334+
}
335+
324336
return Optional.of(new SimpleAudioStreamImpl(
337+
new AudioFormatRegistry().getFromSuffix(fileType),
325338
new SimpleProgressiveHTTPDeliveryDataImpl(downloadUrl)
326339
));
327340
} catch (final Exception ignored) {
@@ -331,6 +344,37 @@ private Optional<AudioStream> extractDownloadableFileIfAvailable() {
331344
}
332345
}
333346

347+
/**
348+
* Determines the file type/extension of the download url.
349+
* <p>
350+
* Note: Uses HTTP FETCH for inspection.
351+
* </p>
352+
*/
353+
@Nullable
354+
private String determineFileTypeFromDownloadUrl(final String downloadUrl)
355+
throws IOException, ReCaptchaException {
356+
357+
final Response response = NewPipe.getDownloader().head(downloadUrl);
358+
359+
// As of 2022-06 Soundcloud uses AWS S3
360+
// Use the AWS header to identify the filetype first because it's simpler
361+
final String amzMetaFileType = response.getHeader("x-amz-meta-file-type");
362+
if (!isNullOrEmpty(amzMetaFileType)) {
363+
return amzMetaFileType;
364+
}
365+
366+
// If the AWS header was not present try extract the filetype
367+
// by inspecting the download file name
368+
// Example-Value:
369+
// attachment;filename="SoundCloud%20Download"; filename*=utf-8''song.mp3
370+
final String contentDisp = response.getHeader("Content-Disposition");
371+
if (!isNullOrEmpty(contentDisp) && contentDisp.contains(".")) {
372+
return contentDisp.substring(contentDisp.lastIndexOf(".") + 1);
373+
}
374+
375+
return null;
376+
}
377+
334378
/**
335379
* Parses a SoundCloud HLS manifest to get a single URL of HLS streams.
336380
*

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/itag/AudioItagFormat.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/itag/ItagFormat.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/itag/VideoAudioItagFormat.java

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.schabi.newpipe.extractor.services.youtube.itag.format;
2+
3+
import org.schabi.newpipe.extractor.streamdata.format.AudioMediaFormat;
4+
5+
public interface AudioItagFormat extends ItagFormat<AudioMediaFormat>, BaseAudioItagFormat {
6+
// Nothing additional to implement
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.schabi.newpipe.extractor.services.youtube.itag.format;
2+
3+
public interface BaseAudioItagFormat {
4+
/**
5+
* Average audio bitrate in KBit/s
6+
*/
7+
int averageBitrate();
8+
}

0 commit comments

Comments
 (0)