11package org .schabi .newpipe .extractor .services .youtube .dashmanifestcreators ;
22
33import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getAndroidUserAgent ;
4- import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getClientInfoHeaders ;
54import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getIosUserAgent ;
5+ import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getOriginReferrerHeaders ;
6+ import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getTvHtml5UserAgent ;
67import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .isAndroidStreamingUrl ;
78import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .isIosStreamingUrl ;
89import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .isTvHtml5StreamingUrl ;
2728import java .io .IOException ;
2829import java .io .StringWriter ;
2930import java .nio .charset .StandardCharsets ;
31+ import java .util .HashMap ;
3032import java .util .List ;
3133import java .util .Locale ;
3234import java .util .Map ;
@@ -602,8 +604,9 @@ public static Response getInitializationResponse(@Nonnull String baseStreamingUr
602604 @ Nonnull final ItagItem itagItem ,
603605 final DeliveryType deliveryType )
604606 throws CreationException {
607+ final boolean isTvHtml5StreamingUrl = isTvHtml5StreamingUrl (baseStreamingUrl );
605608 final boolean isHtml5StreamingUrl = isWebStreamingUrl (baseStreamingUrl )
606- || isTvHtml5StreamingUrl ( baseStreamingUrl )
609+ || isTvHtml5StreamingUrl
607610 || isWebEmbeddedPlayerStreamingUrl (baseStreamingUrl );
608611 final boolean isAndroidStreamingUrl = isAndroidStreamingUrl (baseStreamingUrl );
609612 final boolean isIosStreamingUrl = isIosStreamingUrl (baseStreamingUrl );
@@ -617,7 +620,7 @@ public static Response getInitializationResponse(@Nonnull String baseStreamingUr
617620 final String mimeTypeExpected = itagItem .getMediaFormat ().getMimeType ();
618621 if (!isNullOrEmpty (mimeTypeExpected )) {
619622 return getStreamingWebUrlWithoutRedirects (downloader , baseStreamingUrl ,
620- mimeTypeExpected );
623+ mimeTypeExpected , isTvHtml5StreamingUrl );
621624 }
622625 } else if (isAndroidStreamingUrl || isIosStreamingUrl ) {
623626 try {
@@ -732,6 +735,8 @@ private static String appendRnSqParamsIfNeeded(@Nonnull final String baseStreami
732735 * @param downloader the {@link Downloader} instance to be used
733736 * @param streamingUrl the streaming URL which we are trying to get a streaming URL
734737 * without any redirection on the network and/or IP used
738+ * @param isTvHtml5StreamingUrl whether the streaming URL comes from TVHTML5 client, in
739+ * order to use an appropriate HTTP User-Agent header
735740 * @param responseMimeTypeExpected the response mime type expected from Google video servers
736741 * @return the {@link Response} of the stream, which should have no redirections
737742 */
@@ -740,10 +745,15 @@ private static String appendRnSqParamsIfNeeded(@Nonnull final String baseStreami
740745 private static Response getStreamingWebUrlWithoutRedirects (
741746 @ Nonnull final Downloader downloader ,
742747 @ Nonnull String streamingUrl ,
743- @ Nonnull final String responseMimeTypeExpected )
748+ @ Nonnull final String responseMimeTypeExpected ,
749+ final boolean isTvHtml5StreamingUrl )
744750 throws CreationException {
745751 try {
746- final var headers = getClientInfoHeaders ();
752+ final var headers = new HashMap <>(
753+ getOriginReferrerHeaders ("https://www.youtube.com" ));
754+ if (isTvHtml5StreamingUrl ) {
755+ headers .put ("User-Agent" , List .of (getTvHtml5UserAgent ()));
756+ }
747757
748758 String responseMimeType = "" ;
749759
0 commit comments