3535import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .prepareAndroidMobileJsonBuilder ;
3636import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .prepareDesktopJsonBuilder ;
3737import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .prepareIosMobileJsonBuilder ;
38+ import static org .schabi .newpipe .extractor .utils .JsonUtils .getNullableInteger ;
3839import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
3940
4041import com .grack .nanojson .JsonArray ;
@@ -1332,7 +1333,7 @@ private <I extends ItagFormat<?>> ItagInfo<I> buildItagInfo(
13321333 final ItagInfo <I > itagInfo = new ItagInfo <>(itagFormat , streamUrl );
13331334
13341335 if (itagFormat instanceof BaseAudioItagFormat ) {
1335- final Integer averageBitrate = getIntegerFromJson (formatData , "averageBitrate" );
1336+ final Integer averageBitrate = getNullableInteger (formatData , "averageBitrate" );
13361337 if (averageBitrate != null ) {
13371338 itagInfo .setAverageBitrate ((int ) Math .round (averageBitrate / 1000d ));
13381339 }
@@ -1343,15 +1344,15 @@ private <I extends ItagFormat<?>> ItagInfo<I> buildItagInfo(
13431344 } catch (final Exception ignore ) {
13441345 // Ignore errors - leave default value
13451346 }
1346- itagInfo .setAudioChannels (getIntegerFromJson (formatData , "audioChannels" ));
1347+ itagInfo .setAudioChannels (getNullableInteger (formatData , "audioChannels" ));
13471348 }
13481349 if (itagFormat instanceof VideoItagFormat ) {
1349- itagInfo .setHeight (getIntegerFromJson (formatData , "height" ));
1350- itagInfo .setWidth (getIntegerFromJson (formatData , "width" ));
1351- itagInfo .setFps (getIntegerFromJson (formatData , "fps" ));
1350+ itagInfo .setHeight (getNullableInteger (formatData , "height" ));
1351+ itagInfo .setWidth (getNullableInteger (formatData , "width" ));
1352+ itagInfo .setFps (getNullableInteger (formatData , "fps" ));
13521353 }
13531354
1354- itagInfo .setBitRate (getIntegerFromJson (formatData , "bitRate" ));
1355+ itagInfo .setBitRate (getNullableInteger (formatData , "bitRate" ));
13551356 itagInfo .setQuality (formatData .getString ("quality" ));
13561357
13571358 final String mimeType = formatData .getString ("mimeType" , "" );
@@ -1377,96 +1378,11 @@ private <I extends ItagFormat<?>> ItagInfo<I> buildItagInfo(
13771378 }
13781379
13791380 itagInfo .setType (formatData .getString ("type" ));
1380- itagInfo .setTargetDurationSec (getIntegerFromJson (formatData , "targetDurationSec" ));
1381+ itagInfo .setTargetDurationSec (getNullableInteger (formatData , "targetDurationSec" ));
13811382
13821383 return itagInfo ;
13831384 }
13841385
1385- // TODO
1386- private static Integer getIntegerFromJson (final JsonObject jsonObject , final String key ) {
1387- return (Integer ) jsonObject .getNumber (key );
1388- }
1389-
1390- // private ItagInfo buildItagInfo(
1391- // @Nonnull final String videoId,
1392- // @Nonnull final JsonObject formatData,
1393- // @Nonnull final ItagItem itagItem,
1394- // @Nonnull final ItagItem.ItagType itagType,
1395- // @Nonnull final String contentPlaybackNonce) throws IOException, ExtractionException {
1396- // String streamUrl;
1397- // if (formatData.has("url")) {
1398- // streamUrl = formatData.getString("url");
1399- // } else {
1400- // // This url has an obfuscated signature
1401- // final String cipherString = formatData.has(CIPHER)
1402- // ? formatData.getString(CIPHER)
1403- // : formatData.getString(SIGNATURE_CIPHER);
1404- // final Map<String, String> cipher = Parser.compatParseMap(cipherString);
1405- // streamUrl = cipher.get("url") + "&" + cipher.get("sp") + "="
1406- // + deobfuscateSignature(cipher.get("s"));
1407- // }
1408- //
1409- // // Add the content playback nonce to the stream URL
1410- // streamUrl += "&" + CPN + "=" + contentPlaybackNonce;
1411- //
1412- // // Decrypt the n parameter if it is present
1413- // streamUrl = tryDecryptUrl(streamUrl, videoId);
1414- //
1415- // final JsonObject initRange = formatData.getObject("initRange");
1416- // final JsonObject indexRange = formatData.getObject("indexRange");
1417- // final String mimeType = formatData.getString("mimeType", EMPTY_STRING);
1418- // final String codec = mimeType.contains("codecs")
1419- // ? mimeType.split("\"")[1]
1420- // : EMPTY_STRING;
1421- //
1422- // itagItem.setBitrate(formatData.getInt("bitrate"));
1423- // itagItem.setWidth(formatData.getInt("width"));
1424- // itagItem.setHeight(formatData.getInt("height"));
1425- // itagItem.setInitStart(Integer.parseInt(initRange.getString("start", "-1")));
1426- // itagItem.setInitEnd(Integer.parseInt(initRange.getString("end", "-1")));
1427- // itagItem.setIndexStart(Integer.parseInt(indexRange.getString("start", "-1")));
1428- // itagItem.setIndexEnd(Integer.parseInt(indexRange.getString("end", "-1")));
1429- // itagItem.setQuality(formatData.getString("quality"));
1430- // itagItem.setCodec(codec);
1431- //
1432- // if (isLive() || isPostLive()) {
1433- // itagItem.setTargetDurationSec(formatData.getInt("targetDurationSec"));
1434- // }
1435- //
1436- // if (itagType == ItagItem.ItagType.VIDEO || itagType == ItagItem.ItagType.VIDEO_ONLY) {
1437- // itagItem.setFps(formatData.getInt("fps"));
1438- // }
1439- // if (itagType == ItagItem.ItagType.AUDIO) {
1440- // // YouTube returns the audio sample rate as a string
1441- // itagItem.setSampleRate(Integer.parseInt(formatData.getString("audioSampleRate")));
1442- // itagItem.setAudioChannels(formatData.getInt("audioChannels"));
1443- // }
1444- //
1445- // // YouTube return the content length and the approximate duration as strings
1446- // itagItem.setContentLength(Long.parseLong(formatData.getString(
1447- // "contentLength",
1448- // String.valueOf(CONTENT_LENGTH_UNKNOWN))));
1449- // itagItem.setApproxDurationMs(Long.parseLong(formatData.getString(
1450- // "approxDurationMs",
1451- // String.valueOf(APPROX_DURATION_MS_UNKNOWN))));
1452- //
1453- // final ItagInfo itagInfo = new ItagInfo(streamUrl, itagItem);
1454- //
1455- // if (streamType == StreamType.VIDEO_STREAM) {
1456- // itagInfo.setIsUrl(!formatData.getString("type", "")
1457- // .equalsIgnoreCase("FORMAT_STREAM_TYPE_OTF"));
1458- // } else {
1459- // // We are currently not able to generate DASH manifests for running
1460- // // livestreams, so because of the requirements of StreamInfo
1461- // // objects, return these streams as DASH URL streams (even if they
1462- // // are not playable).
1463- // // Ended livestreams are returned as non URL streams
1464- // itagInfo.setIsUrl(streamType != StreamType.POST_LIVE_STREAM);
1465- // }
1466- //
1467- // return itagInfo;
1468- // }
1469-
14701386 @ Nonnull
14711387 @ Override
14721388 public List <Frameset > getFrames () throws ExtractionException {
0 commit comments