Merge pull request #535 from TeamPiped/adaptive-parsing

Adaptive Stream parsing
This commit is contained in:
bopol 2021-02-18 15:55:27 +01:00 committed by GitHub
commit db253e202b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 201 additions and 7 deletions

View File

@ -8,7 +8,7 @@ import static org.schabi.newpipe.extractor.services.youtube.ItagItem.ItagType.*;
public class ItagItem {
/**
* List can be found here https://github.com/rg3/youtube-dl/blob/master/youtube_dl/extractor/youtube.py#L360
* List can be found here https://github.com/ytdl-org/youtube-dl/blob/9fc5eafb8e384453a49f7cfe73147be491f0b19d/youtube_dl/extractor/youtube.py#L1071
*/
private static final ItagItem[] ITAG_LIST = {
/////////////////////////////////////////////////////
@ -155,4 +155,77 @@ public class ItagItem {
public String resolutionString;
public int fps = -1;
// Fields for Dash
private int bitrate;
private int width;
private int height;
private int initStart;
private int initEnd;
private int indexStart;
private int indexEnd;
private String codec;
public int getBitrate() {
return bitrate;
}
public void setBitrate(int bitrate) {
this.bitrate = bitrate;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getInitStart() {
return initStart;
}
public void setInitStart(int initStart) {
this.initStart = initStart;
}
public int getInitEnd() {
return initEnd;
}
public void setInitEnd(int initEnd) {
this.initEnd = initEnd;
}
public int getIndexStart() {
return indexStart;
}
public void setIndexStart(int indexStart) {
this.indexStart = indexStart;
}
public int getIndexEnd() {
return indexEnd;
}
public void setIndexEnd(int indexEnd) {
this.indexEnd = indexEnd;
}
public String getCodec() {
return codec;
}
public void setCodec(String codec) {
this.codec = codec;
}
}

View File

@ -502,7 +502,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
for (Map.Entry<String, ItagItem> entry : getItags(ADAPTIVE_FORMATS, ItagItem.ItagType.AUDIO).entrySet()) {
ItagItem itag = entry.getValue();
AudioStream audioStream = new AudioStream(entry.getKey(), itag.getMediaFormat(), itag.avgBitrate);
AudioStream audioStream = new AudioStream(entry.getKey(), itag);
if (!Stream.containSimilarStream(audioStream, audioStreams)) {
audioStreams.add(audioStream);
}
@ -522,7 +522,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
for (Map.Entry<String, ItagItem> entry : getItags(FORMATS, ItagItem.ItagType.VIDEO).entrySet()) {
ItagItem itag = entry.getValue();
VideoStream videoStream = new VideoStream(entry.getKey(), itag.getMediaFormat(), itag.resolutionString);
VideoStream videoStream = new VideoStream(entry.getKey(), false, itag);
if (!Stream.containSimilarStream(videoStream, videoStreams)) {
videoStreams.add(videoStream);
}
@ -542,7 +542,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
for (Map.Entry<String, ItagItem> entry : getItags(ADAPTIVE_FORMATS, ItagItem.ItagType.VIDEO_ONLY).entrySet()) {
ItagItem itag = entry.getValue();
VideoStream videoStream = new VideoStream(entry.getKey(), itag.getMediaFormat(), itag.resolutionString, true);
VideoStream videoStream = new VideoStream(entry.getKey(), true, itag);
if (!Stream.containSimilarStream(videoStream, videoOnlyStreams)) {
videoOnlyStreams.add(videoStream);
}
@ -949,6 +949,21 @@ public class YoutubeStreamExtractor extends StreamExtractor {
+ deobfuscateSignature(cipher.get("s"));
}
JsonObject initRange = formatData.getObject("initRange");
JsonObject indexRange = formatData.getObject("indexRange");
String mimeType = formatData.getString("mimeType", EMPTY_STRING);
String codec = mimeType.contains("codecs") ? mimeType.split("\"")[1] : EMPTY_STRING;
itagItem.setBitrate(formatData.getInt("bitrate"));
itagItem.setWidth(formatData.getInt("width"));
itagItem.setHeight(formatData.getInt("height"));
itagItem.setInitStart(Integer.parseInt(initRange.getString("start", "-1")));
itagItem.setInitEnd(Integer.parseInt(initRange.getString("end", "-1")));
itagItem.setIndexStart(Integer.parseInt(indexRange.getString("start", "-1")));
itagItem.setIndexEnd(Integer.parseInt(indexRange.getString("end", "-1")));
itagItem.fps = formatData.getInt("fps");
itagItem.setCodec(codec);
urlAndItags.put(streamUrl, itagItem);
}
} catch (UnsupportedEncodingException ignored) {

View File

@ -21,10 +21,19 @@ package org.schabi.newpipe.extractor.stream;
*/
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.services.youtube.ItagItem;
public class AudioStream extends Stream {
public int average_bitrate = -1;
// Fields for Dash
private int bitrate;
private int initStart;
private int initEnd;
private int indexStart;
private int indexEnd;
private String codec;
/**
* Create a new audio stream
* @param url the url
@ -36,6 +45,21 @@ public class AudioStream extends Stream {
this.average_bitrate = averageBitrate;
}
/**
* Create a new audio stream
* @param url the url
* @param itag the ItagItem of the Stream
*/
public AudioStream(String url, ItagItem itag) {
this(url, itag.getMediaFormat(), itag.avgBitrate);
this.bitrate = itag.getBitrate();
this.initStart = itag.getInitStart();
this.initEnd = itag.getInitEnd();
this.indexStart = itag.getIndexStart();
this.indexEnd = itag.getIndexEnd();
this.codec = itag.getCodec();
}
@Override
public boolean equalStats(Stream cmp) {
return super.equalStats(cmp) && cmp instanceof AudioStream &&
@ -49,4 +73,28 @@ public class AudioStream extends Stream {
public int getAverageBitrate() {
return average_bitrate;
}
public int getBitrate() {
return bitrate;
}
public int getInitStart() {
return initStart;
}
public int getInitEnd() {
return initEnd;
}
public int getIndexStart() {
return indexStart;
}
public int getIndexEnd() {
return indexEnd;
}
public String getCodec() {
return codec;
}
}

View File

@ -21,20 +21,42 @@ package org.schabi.newpipe.extractor.stream;
*/
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.services.youtube.ItagItem;
public class VideoStream extends Stream {
public final String resolution;
public final boolean isVideoOnly;
// Fields for Dash
private int bitrate;
private int initStart;
private int initEnd;
private int indexStart;
private int indexEnd;
private int width;
private int height;
private int fps;
private String codec;
public VideoStream(String url, MediaFormat format, String resolution) {
this(url, format, resolution, false);
}
public VideoStream(String url, MediaFormat format, String resolution, boolean isVideoOnly) {
super(url, format);
this.resolution = resolution;
this.isVideoOnly = isVideoOnly;
this(url, null, format, resolution, isVideoOnly);
}
public VideoStream(String url, boolean isVideoOnly, ItagItem itag) {
this(url, itag.getMediaFormat(), itag.resolutionString, isVideoOnly);
this.bitrate = itag.getBitrate();
this.initStart = itag.getInitStart();
this.initEnd = itag.getInitEnd();
this.indexStart = itag.getIndexStart();
this.indexEnd = itag.getIndexEnd();
this.codec = itag.getCodec();
this.height = itag.getHeight();
this.width = itag.getWidth();
this.fps = itag.fps;
}
public VideoStream(String url, String torrentUrl, MediaFormat format, String resolution) {
@ -73,4 +95,40 @@ public class VideoStream extends Stream {
public boolean isVideoOnly() {
return isVideoOnly;
}
public int getBitrate() {
return bitrate;
}
public int getInitStart() {
return initStart;
}
public int getInitEnd() {
return initEnd;
}
public int getIndexStart() {
return indexStart;
}
public int getIndexEnd() {
return indexEnd;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getFps() {
return fps;
}
public String getCodec() {
return codec;
}
}