This commit is contained in:
Stypox 2019-09-11 19:05:41 +02:00
parent cfc72d8dc9
commit bf017bf5b9
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
1 changed files with 29 additions and 37 deletions

View File

@ -85,6 +85,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
private JsonObject playerArgs; private JsonObject playerArgs;
@Nonnull @Nonnull
private final Map<String, String> videoInfoPage = new HashMap<>(); private final Map<String, String> videoInfoPage = new HashMap<>();
private JsonObject playerResponse;
@Nonnull @Nonnull
private List<SubtitlesInfo> subtitlesInfos = new ArrayList<>(); private List<SubtitlesInfo> subtitlesInfos = new ArrayList<>();
@ -486,7 +487,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
assertPageFetched(); assertPageFetched();
List<VideoStream> videoStreams = new ArrayList<>(); List<VideoStream> videoStreams = new ArrayList<>();
try { try {
for (Map.Entry<String, ItagItem> entry : getItags(URL_ENCODED_FMT_STREAM_MAP, ItagItem.ItagType.VIDEO).entrySet()) { for (Map.Entry<String, ItagItem> entry : getItags(ADAPTIVE_FMTS, ItagItem.ItagType.VIDEO).entrySet()) {
ItagItem itag = entry.getValue(); ItagItem itag = entry.getValue();
VideoStream videoStream = new VideoStream(entry.getKey(), itag.getMediaFormat(), itag.resolutionString); VideoStream videoStream = new VideoStream(entry.getKey(), itag.getMediaFormat(), itag.resolutionString);
@ -620,7 +621,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
private static final String URL_ENCODED_FMT_STREAM_MAP = "url_encoded_fmt_stream_map"; private static final String URL_ENCODED_FMT_STREAM_MAP = "url_encoded_fmt_stream_map";
private static final String ADAPTIVE_FMTS = "adaptive_fmts"; private static final String ADAPTIVE_FMTS = "adaptiveFormats";
private static final String HTTPS = "https:"; private static final String HTTPS = "https:";
private static final String CONTENT = "content"; private static final String CONTENT = "content";
private static final String DECRYPTION_FUNC_NAME = "decrypt"; private static final String DECRYPTION_FUNC_NAME = "decrypt";
@ -667,6 +668,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
playerUrl = getPlayerUrl(ytPlayerConfig); playerUrl = getPlayerUrl(ytPlayerConfig);
isAgeRestricted = false; isAgeRestricted = false;
} }
playerResponse = getPlayerResponse();
if (decryptionCode.isEmpty()) { if (decryptionCode.isEmpty()) {
decryptionCode = loadDecryptionCode(playerUrl); decryptionCode = loadDecryptionCode(playerUrl);
@ -728,6 +730,20 @@ public class YoutubeStreamExtractor extends StreamExtractor {
} }
} }
private JsonObject getPlayerResponse() throws ParsingException {
try {
String playerResponseStr;
if(playerArgs != null) {
playerResponseStr = playerArgs.getString("player_response");
} else {
playerResponseStr = videoInfoPage.get("player_response");
}
return JsonParser.object().from(playerResponseStr);
} catch (Exception e) {
throw new ParsingException("Could not parse yt player response", e);
}
}
@Nonnull @Nonnull
private EmbeddedInfo getEmbeddedInfo() throws ParsingException, ReCaptchaException { private EmbeddedInfo getEmbeddedInfo() throws ParsingException, ReCaptchaException {
try { try {
@ -924,46 +940,22 @@ public class YoutubeStreamExtractor extends StreamExtractor {
"&sts=" + sts + "&ps=default&gl=US&hl=en"; "&sts=" + sts + "&ps=default&gl=US&hl=en";
} }
private Map<String, ItagItem> getItags(String encodedUrlMapKey, ItagItem.ItagType itagTypeWanted) throws ParsingException { private Map<String, ItagItem> getItags(String streamingDataKey, ItagItem.ItagType itagTypeWanted) throws ParsingException {
Map<String, ItagItem> urlAndItags = new LinkedHashMap<>(); Map<String, ItagItem> urlAndItags = new LinkedHashMap<>();
String encodedUrlMap = ""; JsonArray formats = playerResponse.getObject("streamingData").getArray(streamingDataKey);
if (playerArgs != null && playerArgs.isString(encodedUrlMapKey)) { for (int i = 0; i != formats.size(); ++i) {
encodedUrlMap = playerArgs.getString(encodedUrlMapKey, ""); JsonObject formatData = formats.getObject(i);
} else if (videoInfoPage.containsKey(encodedUrlMapKey)) { int itag = formatData.getInt("itag");
encodedUrlMap = videoInfoPage.get(encodedUrlMapKey);
}
for (String url_data_str : encodedUrlMap.split(",")) {
try {
// This loop iterates through multiple streams, therefore tags
// is related to one and the same stream at a time.
Map<String, String> tags = Parser.compatParseMap(
org.jsoup.parser.Parser.unescapeEntities(url_data_str, true));
int itag = Integer.parseInt(tags.get("itag"));
if (ItagItem.isSupported(itag)) { if (ItagItem.isSupported(itag)) {
ItagItem itagItem = ItagItem.getItag(itag); ItagItem itagItem = ItagItem.getItag(itag);
if (itagItem.itagType == itagTypeWanted) { if (itagItem.itagType == itagTypeWanted) {
String streamUrl = tags.get("url"); String streamUrl = formatData.getString("url");
// if video has a signature: decrypt it and add it to the url System.out.println(streamUrl);
if (tags.get("s") != null) {
if (tags.get("sp") == null) {
// fallback for urls not conaining the "sp" tag
streamUrl = streamUrl + "&signature=" + decryptSignature(tags.get("s"), decryptionCode);
}
else {
streamUrl = streamUrl + "&" + tags.get("sp") + "=" + decryptSignature(tags.get("s"), decryptionCode);
}
}
urlAndItags.put(streamUrl, itagItem); urlAndItags.put(streamUrl, itagItem);
} }
} }
} catch (DecryptException e) {
throw e;
} catch (Exception ignored) {
}
} }
return urlAndItags; return urlAndItags;