Skip to content

Commit db253e2

Browse files
authored
Merge pull request #535 from TeamPiped/adaptive-parsing
Adaptive Stream parsing
2 parents 64f6b04 + c24afa2 commit db253e2

4 files changed

Lines changed: 201 additions & 7 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/ItagItem.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class ItagItem {
1010
/**
11-
* List can be found here https://github.com/rg3/youtube-dl/blob/master/youtube_dl/extractor/youtube.py#L360
11+
* List can be found here https://github.com/ytdl-org/youtube-dl/blob/9fc5eafb8e384453a49f7cfe73147be491f0b19d/youtube_dl/extractor/youtube.py#L1071
1212
*/
1313
private static final ItagItem[] ITAG_LIST = {
1414
/////////////////////////////////////////////////////
@@ -155,4 +155,77 @@ public MediaFormat getMediaFormat() {
155155
public String resolutionString;
156156
public int fps = -1;
157157

158+
// Fields for Dash
159+
private int bitrate;
160+
private int width;
161+
private int height;
162+
private int initStart;
163+
private int initEnd;
164+
private int indexStart;
165+
private int indexEnd;
166+
private String codec;
167+
168+
public int getBitrate() {
169+
return bitrate;
170+
}
171+
172+
public void setBitrate(int bitrate) {
173+
this.bitrate = bitrate;
174+
}
175+
176+
public int getWidth() {
177+
return width;
178+
}
179+
180+
public void setWidth(int width) {
181+
this.width = width;
182+
}
183+
184+
public int getHeight() {
185+
return height;
186+
}
187+
188+
public void setHeight(int height) {
189+
this.height = height;
190+
}
191+
192+
public int getInitStart() {
193+
return initStart;
194+
}
195+
196+
public void setInitStart(int initStart) {
197+
this.initStart = initStart;
198+
}
199+
200+
public int getInitEnd() {
201+
return initEnd;
202+
}
203+
204+
public void setInitEnd(int initEnd) {
205+
this.initEnd = initEnd;
206+
}
207+
208+
public int getIndexStart() {
209+
return indexStart;
210+
}
211+
212+
public void setIndexStart(int indexStart) {
213+
this.indexStart = indexStart;
214+
}
215+
216+
public int getIndexEnd() {
217+
return indexEnd;
218+
}
219+
220+
public void setIndexEnd(int indexEnd) {
221+
this.indexEnd = indexEnd;
222+
}
223+
224+
public String getCodec() {
225+
return codec;
226+
}
227+
228+
public void setCodec(String codec) {
229+
this.codec = codec;
230+
}
158231
}

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public List<AudioStream> getAudioStreams() throws ExtractionException {
502502
for (Map.Entry<String, ItagItem> entry : getItags(ADAPTIVE_FORMATS, ItagItem.ItagType.AUDIO).entrySet()) {
503503
ItagItem itag = entry.getValue();
504504

505-
AudioStream audioStream = new AudioStream(entry.getKey(), itag.getMediaFormat(), itag.avgBitrate);
505+
AudioStream audioStream = new AudioStream(entry.getKey(), itag);
506506
if (!Stream.containSimilarStream(audioStream, audioStreams)) {
507507
audioStreams.add(audioStream);
508508
}
@@ -522,7 +522,7 @@ public List<VideoStream> getVideoStreams() throws ExtractionException {
522522
for (Map.Entry<String, ItagItem> entry : getItags(FORMATS, ItagItem.ItagType.VIDEO).entrySet()) {
523523
ItagItem itag = entry.getValue();
524524

525-
VideoStream videoStream = new VideoStream(entry.getKey(), itag.getMediaFormat(), itag.resolutionString);
525+
VideoStream videoStream = new VideoStream(entry.getKey(), false, itag);
526526
if (!Stream.containSimilarStream(videoStream, videoStreams)) {
527527
videoStreams.add(videoStream);
528528
}
@@ -542,7 +542,7 @@ public List<VideoStream> getVideoOnlyStreams() throws ExtractionException {
542542
for (Map.Entry<String, ItagItem> entry : getItags(ADAPTIVE_FORMATS, ItagItem.ItagType.VIDEO_ONLY).entrySet()) {
543543
ItagItem itag = entry.getValue();
544544

545-
VideoStream videoStream = new VideoStream(entry.getKey(), itag.getMediaFormat(), itag.resolutionString, true);
545+
VideoStream videoStream = new VideoStream(entry.getKey(), true, itag);
546546
if (!Stream.containSimilarStream(videoStream, videoOnlyStreams)) {
547547
videoOnlyStreams.add(videoStream);
548548
}
@@ -949,6 +949,21 @@ private Map<String, ItagItem> getItags(final String streamingDataKey,
949949
+ deobfuscateSignature(cipher.get("s"));
950950
}
951951

952+
JsonObject initRange = formatData.getObject("initRange");
953+
JsonObject indexRange = formatData.getObject("indexRange");
954+
String mimeType = formatData.getString("mimeType", EMPTY_STRING);
955+
String codec = mimeType.contains("codecs") ? mimeType.split("\"")[1] : EMPTY_STRING;
956+
957+
itagItem.setBitrate(formatData.getInt("bitrate"));
958+
itagItem.setWidth(formatData.getInt("width"));
959+
itagItem.setHeight(formatData.getInt("height"));
960+
itagItem.setInitStart(Integer.parseInt(initRange.getString("start", "-1")));
961+
itagItem.setInitEnd(Integer.parseInt(initRange.getString("end", "-1")));
962+
itagItem.setIndexStart(Integer.parseInt(indexRange.getString("start", "-1")));
963+
itagItem.setIndexEnd(Integer.parseInt(indexRange.getString("end", "-1")));
964+
itagItem.fps = formatData.getInt("fps");
965+
itagItem.setCodec(codec);
966+
952967
urlAndItags.put(streamUrl, itagItem);
953968
}
954969
} catch (UnsupportedEncodingException ignored) {

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,19 @@
2121
*/
2222

2323
import org.schabi.newpipe.extractor.MediaFormat;
24+
import org.schabi.newpipe.extractor.services.youtube.ItagItem;
2425

2526
public class AudioStream extends Stream {
2627
public int average_bitrate = -1;
2728

29+
// Fields for Dash
30+
private int bitrate;
31+
private int initStart;
32+
private int initEnd;
33+
private int indexStart;
34+
private int indexEnd;
35+
private String codec;
36+
2837
/**
2938
* Create a new audio stream
3039
* @param url the url
@@ -36,6 +45,21 @@ public AudioStream(String url, MediaFormat format, int averageBitrate) {
3645
this.average_bitrate = averageBitrate;
3746
}
3847

48+
/**
49+
* Create a new audio stream
50+
* @param url the url
51+
* @param itag the ItagItem of the Stream
52+
*/
53+
public AudioStream(String url, ItagItem itag) {
54+
this(url, itag.getMediaFormat(), itag.avgBitrate);
55+
this.bitrate = itag.getBitrate();
56+
this.initStart = itag.getInitStart();
57+
this.initEnd = itag.getInitEnd();
58+
this.indexStart = itag.getIndexStart();
59+
this.indexEnd = itag.getIndexEnd();
60+
this.codec = itag.getCodec();
61+
}
62+
3963
@Override
4064
public boolean equalStats(Stream cmp) {
4165
return super.equalStats(cmp) && cmp instanceof AudioStream &&
@@ -49,4 +73,28 @@ public boolean equalStats(Stream cmp) {
4973
public int getAverageBitrate() {
5074
return average_bitrate;
5175
}
76+
77+
public int getBitrate() {
78+
return bitrate;
79+
}
80+
81+
public int getInitStart() {
82+
return initStart;
83+
}
84+
85+
public int getInitEnd() {
86+
return initEnd;
87+
}
88+
89+
public int getIndexStart() {
90+
return indexStart;
91+
}
92+
93+
public int getIndexEnd() {
94+
return indexEnd;
95+
}
96+
97+
public String getCodec() {
98+
return codec;
99+
}
52100
}

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

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,42 @@
2121
*/
2222

2323
import org.schabi.newpipe.extractor.MediaFormat;
24+
import org.schabi.newpipe.extractor.services.youtube.ItagItem;
2425

2526
public class VideoStream extends Stream {
2627
public final String resolution;
2728
public final boolean isVideoOnly;
2829

30+
// Fields for Dash
31+
private int bitrate;
32+
private int initStart;
33+
private int initEnd;
34+
private int indexStart;
35+
private int indexEnd;
36+
private int width;
37+
private int height;
38+
private int fps;
39+
private String codec;
2940

3041
public VideoStream(String url, MediaFormat format, String resolution) {
3142
this(url, format, resolution, false);
3243
}
3344

3445
public VideoStream(String url, MediaFormat format, String resolution, boolean isVideoOnly) {
35-
super(url, format);
36-
this.resolution = resolution;
37-
this.isVideoOnly = isVideoOnly;
46+
this(url, null, format, resolution, isVideoOnly);
47+
}
48+
49+
public VideoStream(String url, boolean isVideoOnly, ItagItem itag) {
50+
this(url, itag.getMediaFormat(), itag.resolutionString, isVideoOnly);
51+
this.bitrate = itag.getBitrate();
52+
this.initStart = itag.getInitStart();
53+
this.initEnd = itag.getInitEnd();
54+
this.indexStart = itag.getIndexStart();
55+
this.indexEnd = itag.getIndexEnd();
56+
this.codec = itag.getCodec();
57+
this.height = itag.getHeight();
58+
this.width = itag.getWidth();
59+
this.fps = itag.fps;
3860
}
3961

4062
public VideoStream(String url, String torrentUrl, MediaFormat format, String resolution) {
@@ -73,4 +95,40 @@ public String getResolution() {
7395
public boolean isVideoOnly() {
7496
return isVideoOnly;
7597
}
98+
99+
public int getBitrate() {
100+
return bitrate;
101+
}
102+
103+
public int getInitStart() {
104+
return initStart;
105+
}
106+
107+
public int getInitEnd() {
108+
return initEnd;
109+
}
110+
111+
public int getIndexStart() {
112+
return indexStart;
113+
}
114+
115+
public int getIndexEnd() {
116+
return indexEnd;
117+
}
118+
119+
public int getWidth() {
120+
return width;
121+
}
122+
123+
public int getHeight() {
124+
return height;
125+
}
126+
127+
public int getFps() {
128+
return fps;
129+
}
130+
131+
public String getCodec() {
132+
return codec;
133+
}
76134
}

0 commit comments

Comments
 (0)