Skip to content

Commit e4afb21

Browse files
committed
make Subtitle object Stream compliant
* merge SubtitlesFormat into MediaFormat * implement Stream interface on Subtitle class * misc fixes: dont show a snackbar error on age-restricted videos, lint fix.
1 parent 7129d6d commit e4afb21

6 files changed

Lines changed: 140 additions & 20 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/MediaFormat.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ public enum MediaFormat {
3636
M4A (0x3, "m4a", "m4a", "audio/mp4"),
3737
WEBMA (0x4, "WebM", "webm", "audio/webm"),
3838
MP3 (0x5, "MP3", "mp3", "audio/mpeg"),
39-
OPUS (0x6, "opus", "opus", "audio/opus");
39+
OPUS (0x6, "opus", "opus", "audio/opus"),
40+
// subtitles formats
41+
VTT (0x7, "WebVTT", "vtt", "text/vtt"),
42+
TTML (0x8, "Timed Text Markup Language", "ttml", "application/ttml+xml"),
43+
TRANSCRIPT1 (0x9, "TranScript v1", "srv1", "text/xml"),
44+
TRANSCRIPT2 (0xA, "TranScript v2", "srv2", "text/xml"),
45+
TRANSCRIPT3 (0xB, "TranScript v3", "srv3", "text/xml"),
46+
SRT (0xC, "SubRip file format", "srt", "text/srt");
4047

4148
public final int id;
4249
public final String name;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ public List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionExc
172172

173173
@Override
174174
@Nonnull
175-
public List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException {
175+
public List<SubtitlesStream> getSubtitlesDefault() throws IOException, ExtractionException {
176176
return Collections.emptyList();
177177
}
178178

179179
@Override
180180
@Nonnull
181-
public List<Subtitles> getSubtitles(SubtitlesFormat format) throws IOException, ExtractionException {
181+
public List<SubtitlesStream> getSubtitles(MediaFormat format) throws IOException, ExtractionException {
182182
return Collections.emptyList();
183183
}
184184

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.jsoup.Jsoup;
88
import org.jsoup.nodes.Document;
99
import org.jsoup.nodes.Element;
10+
import org.jsoup.select.Elements;
1011
import org.mozilla.javascript.Context;
1112
import org.mozilla.javascript.Function;
1213
import org.mozilla.javascript.ScriptableObject;
@@ -460,15 +461,15 @@ public List<VideoStream> getVideoOnlyStreams() throws ExtractionException {
460461

461462
@Override
462463
@Nonnull
463-
public List<Subtitles> getSubtitlesDefault() throws IOException, ExtractionException {
464-
return getSubtitles(SubtitlesFormat.TTML);
464+
public List<SubtitlesStream> getSubtitlesDefault() throws IOException, ExtractionException {
465+
return getSubtitles(MediaFormat.TTML);
465466
}
466467

467468
@Override
468469
@Nonnull
469-
public List<Subtitles> getSubtitles(final SubtitlesFormat format) throws IOException, ExtractionException {
470+
public List<SubtitlesStream> getSubtitles(final MediaFormat format) throws IOException, ExtractionException {
470471
assertPageFetched();
471-
List<Subtitles> subtitles = new ArrayList<>();
472+
List<SubtitlesStream> subtitles = new ArrayList<>();
472473
for (final SubtitlesInfo subtitlesInfo : subtitlesInfos) {
473474
subtitles.add(subtitlesInfo.getSubtitle(format));
474475
}
@@ -494,9 +495,13 @@ public StreamInfoItem getNextStream() throws IOException, ExtractionException {
494495
assertPageFetched();
495496
try {
496497
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
497-
collector.commit(extractVideoPreviewInfo(doc.select("div[class=\"watch-sidebar-section\"]")
498-
.first().select("li").first()));
499498

499+
Elements watch = doc.select("div[class=\"watch-sidebar-section\"]");
500+
if (watch.size() < 1) {
501+
return null;// prevent the snackbar notification "report error" on age-restricted videos
502+
}
503+
504+
collector.commit(extractVideoPreviewInfo(watch.first().select("li").first()));
500505
return collector.getItems().get(0);
501506
} catch (Exception e) {
502507
throw new ParsingException("Could not get next video", e);
@@ -815,21 +820,16 @@ private class SubtitlesInfo {
815820
final String languageCode;
816821
final boolean isGenerated;
817822

818-
final Locale locale;
819-
820823
public SubtitlesInfo(final String baseUrl, final String languageCode, final boolean isGenerated) {
821824
this.cleanUrl = baseUrl
822825
.replaceAll("&fmt=[^&]*", "") // Remove preexisting format if exists
823826
.replaceAll("&tlang=[^&]*", ""); // Remove translation language
824827
this.languageCode = languageCode;
825828
this.isGenerated = isGenerated;
826-
827-
final String[] splits = languageCode.split("-");
828-
this.locale = splits.length == 2 ? new Locale(splits[0], splits[1]) : new Locale(languageCode);
829829
}
830830

831-
public Subtitles getSubtitle(final SubtitlesFormat format) {
832-
return new Subtitles(format, locale, cleanUrl + "&fmt=" + format.getExtension(), isGenerated);
831+
public SubtitlesStream getSubtitle(final MediaFormat format) {
832+
return new SubtitlesStream(format, languageCode, cleanUrl + "&fmt=" + format.getSuffix(), isGenerated);
833833
}
834834
}
835835

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
import org.schabi.newpipe.extractor.Extractor;
24+
import org.schabi.newpipe.extractor.MediaFormat;
2425
import org.schabi.newpipe.extractor.StreamingService;
2526
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
2627
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@@ -314,5 +315,44 @@ protected long getTimestampSeconds(String regexPattern) throws ParsingException
314315
}
315316
} else {
316317
return 0;
317-
}};
318+
}
319+
}
320+
321+
public abstract long getViewCount() throws ParsingException;
322+
public abstract long getLikeCount() throws ParsingException;
323+
public abstract long getDislikeCount() throws ParsingException;
324+
325+
@Nonnull
326+
public abstract String getUploaderUrl() throws ParsingException;
327+
@Nonnull
328+
public abstract String getUploaderName() throws ParsingException;
329+
@Nonnull
330+
public abstract String getUploaderAvatarUrl() throws ParsingException;
331+
332+
/**
333+
* Get the dash mpd url
334+
* @return the url as a string or an empty string
335+
* @throws ParsingException if an error occurs while reading
336+
*/
337+
@Nonnull public abstract String getDashMpdUrl() throws ParsingException;
338+
@Nonnull public abstract String getHlsUrl() throws ParsingException;
339+
public abstract List<AudioStream> getAudioStreams() throws IOException, ExtractionException;
340+
public abstract List<VideoStream> getVideoStreams() throws IOException, ExtractionException;
341+
public abstract List<VideoStream> getVideoOnlyStreams() throws IOException, ExtractionException;
342+
343+
@Nonnull
344+
public abstract List<SubtitlesStream> getSubtitlesDefault() throws IOException, ExtractionException;
345+
@Nonnull
346+
public abstract List<SubtitlesStream> getSubtitles(MediaFormat format) throws IOException, ExtractionException;
347+
348+
public abstract StreamType getStreamType() throws ParsingException;
349+
public abstract StreamInfoItem getNextVideo() throws IOException, ExtractionException;
350+
public abstract StreamInfoItemsCollector getRelatedVideos() throws IOException, ExtractionException;
351+
352+
/**
353+
* Analyses the webpage's document and extracts any error message there might be.
354+
*
355+
* @return Error message; null if there is no error message.
356+
*/
357+
public abstract String getErrorMessage();
318358
}

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ private static StreamInfo extractOptionalData(StreamInfo streamInfo, StreamExtra
283283
private List<InfoItem> relatedStreams;
284284

285285
private long startPosition = 0;
286-
private List<Subtitles> subtitles;
286+
private List<SubtitlesStream> subtitles;
287287

288288
/**
289289
* Get the stream type
@@ -494,11 +494,11 @@ public void setStartPosition(long startPosition) {
494494
this.startPosition = startPosition;
495495
}
496496

497-
public List<Subtitles> getSubtitles() {
497+
public List<SubtitlesStream> getSubtitles() {
498498
return subtitles;
499499
}
500500

501-
public void setSubtitles(List<Subtitles> subtitles) {
501+
public void setSubtitles(List<SubtitlesStream> subtitles) {
502502
this.subtitles = subtitles;
503503
}
504504

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.schabi.newpipe.extractor.stream;
2+
3+
import org.schabi.newpipe.extractor.MediaFormat;
4+
5+
import java.io.Serializable;
6+
import java.util.Locale;
7+
8+
public class SubtitlesStream extends Stream implements Serializable {
9+
private final MediaFormat format;
10+
private final Locale locale;
11+
private final String url;
12+
private final boolean autoGenerated;
13+
private final String code;
14+
15+
public SubtitlesStream(MediaFormat format, String languageCode, String url, boolean autoGenerated) {
16+
super(url, format);
17+
18+
/*
19+
* Locale.forLanguageTag only for API >= 21
20+
* Locale.Builder only for API >= 21
21+
* Country codes doesn't work well without
22+
*/
23+
final String[] splits = languageCode.split("-");
24+
switch (splits.length) {
25+
default:
26+
this.locale = new Locale(splits[0]);
27+
break;
28+
case 3:
29+
this.locale = new Locale(splits[0], splits[1], splits[2]);// complex variants doesn't work!
30+
break;
31+
case 2:
32+
this.locale = new Locale(splits[0], splits[1]);
33+
break;
34+
}
35+
this.code = languageCode;
36+
this.format = format;
37+
this.url = url;
38+
this.autoGenerated = autoGenerated;
39+
}
40+
41+
public String getExtension() {
42+
return format.suffix;
43+
}
44+
45+
public String getURL() {
46+
return url;
47+
}
48+
49+
public boolean isAutoGenerated() {
50+
return autoGenerated;
51+
}
52+
53+
@Override
54+
public boolean equalStats(Stream cmp) {
55+
return super.equalStats(cmp)&&
56+
cmp instanceof SubtitlesStream &&
57+
code.equals(((SubtitlesStream) cmp).code) &&
58+
autoGenerated == ((SubtitlesStream) cmp).autoGenerated;
59+
}
60+
61+
public String getDisplayLanguageName() {
62+
return locale.getDisplayName(locale);
63+
}
64+
65+
public String getLanguageTag() {
66+
return code;
67+
}
68+
69+
public Locale getLocale() {
70+
return locale;
71+
}
72+
73+
}

0 commit comments

Comments
 (0)