Skip to content

plugins.youtube: Better API age-gate bypassing #4058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 2, 2021

Conversation

MinePlayersPE
Copy link
Contributor

Based on yt-dlp/yt-dlp#574 (comment)

@bastimeyer
Copy link
Member

Doesn't seem to work

$ streamlink -l debug 'https://www.youtube.com/watch?v=Tq92D6wQ1mg'
[cli][debug] OS:         Linux-5.14.8-1-git-x86_64-with-glibc2.33
[cli][debug] Python:     3.9.7
[cli][debug] Streamlink: 2.4.0+29.g9d9cb99
[cli][debug] Requests(2.26.0), Socks(1.7.1), Websocket(0.58.0)
[cli][debug] Arguments:
[cli][debug]  url=https://www.youtube.com/watch?v=Tq92D6wQ1mg
[cli][debug]  --loglevel=debug
[cli][debug]  --player=mpv
[cli][info] Found matching plugin youtube for URL https://www.youtube.com/watch?v=Tq92D6wQ1mg
[plugins.youtube][error] Could not get video info - LOGIN_REQUIRED: Melde dich an, um dein Alter zu bestätigen.
error: Unable to open URL: https://www.youtube.com/youtubei/v1/player (400 Client Error: Bad Request for url: https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8)

Doesn't work either

$ streamlink -l debug 'https://www.youtube.com/watch?v=HsUATh_Nc2U'
[cli][debug] OS:         Linux-5.14.8-1-git-x86_64-with-glibc2.33
[cli][debug] Python:     3.9.7
[cli][debug] Streamlink: 2.4.0+29.g9d9cb99
[cli][debug] Requests(2.26.0), Socks(1.7.1), Websocket(0.58.0)
[cli][debug] Arguments:
[cli][debug]  url=https://www.youtube.com/watch?v=HsUATh_Nc2U
[cli][debug]  --loglevel=debug
[cli][debug]  --player=mpv
[cli][info] Found matching plugin youtube for URL https://www.youtube.com/watch?v=HsUATh_Nc2U
[plugins.youtube][error] Could not get video info - LOGIN_REQUIRED: Melde dich an, um dein Alter zu bestätigen.
error: Unable to open URL: https://www.youtube.com/youtubei/v1/player (400 Client Error: Bad Request for url: https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8)

Based on yt-dlp/yt-dlp#574 (comment)

- Works in videos like https://www.youtube.com/watch?v=Tq92D6wQ1mg
- embedUrl is required for videos like https://www.youtube.com/watch?v=HsUATh_Nc2U but I can't find any examples that aren't protected so I left it out
- Still doesn't work for videos like https://www.youtube.com/watch?v=Cr381pDsSsA which seem to have embedding explicitly disabled (since it returns an `Playback on other websites has been disabled by the video owner.` error)
@MinePlayersPE
Copy link
Contributor Author

@bastimeyer I'm so sorry, I thought I already changed it before pushing 😅 It should be fixed now
P.S. I didn't add embedUrl because I can't find any unprotected examples (since it seems to be directly tied to music copyright restrictions)

@bastimeyer
Copy link
Member

This works, but there's an unnecessary error log message which needs to be fixed.

This is caused by the _data_status method which reads the data from the embedded JSON first and then from the API response which this PR fixes. The log message should only be printed if the second check fails. The current implementation is a bit awkward, probably because of the returned tuple and the status+reason vars.

$ streamlink -l debug 'https://www.youtube.com/watch?v=Tq92D6wQ1mg'
[cli][debug] OS:         Linux-5.14.8-1-git-x86_64-with-glibc2.33
[cli][debug] Python:     3.9.7
[cli][debug] Streamlink: 2.4.0+29.gfeadc7c
[cli][debug] Requests(2.26.0), Socks(1.7.1), Websocket(0.58.0)
[cli][debug] Arguments:
[cli][debug]  url=https://www.youtube.com/watch?v=Tq92D6wQ1mg
[cli][debug]  --loglevel=debug
[cli][debug]  --player=mpv
[cli][info] Found matching plugin youtube for URL https://www.youtube.com/watch?v=Tq92D6wQ1mg
[plugins.youtube][error] Could not get video info - LOGIN_REQUIRED: Melde dich an, um dein Alter zu bestätigen.
[plugins.youtube][debug] Using video ID: Tq92D6wQ1mg
[plugins.youtube][debug] MuxedStream: v 137 a 251 = 1080p
[plugins.youtube][debug] MuxedStream: v 135 a 251 = 480p
[plugins.youtube][debug] MuxedStream: v 133 a 251 = 240p
[plugins.youtube][debug] MuxedStream: v 160 a 251 = 144p
Available streams: audio_mp4a, audio_opus, 144p (worst), 240p, 360p, 480p, 1080p (best)

A bit messy and not ideal, but this should do:

diff --git a/src/streamlink/plugins/youtube.py b/src/streamlink/plugins/youtube.py
index 0d4de670..1a9ea804 100644
--- a/src/streamlink/plugins/youtube.py
+++ b/src/streamlink/plugins/youtube.py
@@ -279,12 +279,13 @@ class YouTube(Plugin):
                 if videoId is not None:
                     return videoId
 
-    def _data_status(self, data):
+    def _data_status(self, data, errorlog=False):
         if not data:
             return False
         status, reason = self._schema_playabilitystatus(data)
         if status != "OK":
-            log.error(f"Could not get video info - {status}: {reason}")
+            if errorlog:
+                log.error(f"Could not get video info - {status}: {reason}")
             return False
         return True
 
@@ -303,7 +304,7 @@ class YouTube(Plugin):
         data = self._get_data_from_regex(res, self._re_ytInitialPlayerResponse, "initial player response")
         if not self._data_status(data):
             data = self._get_data_from_api(res)
-            if not self._data_status(data):
+            if not self._data_status(data, True):
                 return
 
         video_id, self.author, self.title, is_live = self._schema_videodetails(data)

@bastimeyer bastimeyer added the plugin enhancement A new feature for a working Plugin label Oct 1, 2021
@bastimeyer
Copy link
Member

@MinePlayersPE could you please add the diff I've posted to your changes, so that the incorrect log message can be fixed?

@MinePlayersPE
Copy link
Contributor Author

@MinePlayersPE could you please add the diff I've posted to your changes, so that the incorrect log message can be fixed?

@bastimeyer Done! 👍 I thought by not ideal you meant we should find a new solution first

@bastimeyer bastimeyer merged commit 099e7e4 into streamlink:master Oct 2, 2021
@bastimeyer
Copy link
Member

Thanks

Billy2011 added a commit to Billy2011/streamlink-27 that referenced this pull request Oct 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin enhancement A new feature for a working Plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants