Skip to content

Commit

Permalink
Merge pull request #16 from jhg3410/fix/videoplayer
Browse files Browse the repository at this point in the history
videoPlayer streamUrl 버그 수정
  • Loading branch information
jhg3410 authored Sep 28, 2023
2 parents 6a66958 + 002bfd5 commit b1b327b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
3 changes: 1 addition & 2 deletions lib-videoplayer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ chaquopy {
defaultConfig {
pip {
install("pytube")
install("mock")
}
}
}
Expand All @@ -70,6 +71,4 @@ dependencies {
// exoplayer
implementation(libs.media3.exoplayer)
implementation(libs.media3.ui)


}
46 changes: 38 additions & 8 deletions lib-videoplayer/src/main/python/youtube_stream_url.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
import re
import mock
from pytube import YouTube
from pytube.cipher import get_throttling_function_code

def getVideoStreamUrl(videoId):
# YouTube URL
youtube_url = "https://www.youtube.com/watch?v=" + videoId
with mock.patch('pytube.cipher.get_throttling_plan', patched_throttling_plan):
# YouTube URL
youtube_url = "https://www.youtube.com/watch?v=" + videoId

# Create a YouTube object
youtube = YouTube(youtube_url)
# Create a YouTube object
youtube = YouTube(youtube_url)

# Get the best stream (video) available
video_stream = youtube.streams.get_highest_resolution()
# Get the best stream (video) available
video_stream = youtube.streams.get_highest_resolution()

# Get the video URL
return video_stream.url


def patched_throttling_plan(js: str):
"""Patch throttling plan, from https://github.com/pytube/pytube/issues/1498"""
raw_code = get_throttling_function_code(js)

transform_start = r"try{"
plan_regex = re.compile(transform_start)
match = plan_regex.search(raw_code)

#transform_plan_raw = find_object_from_startpoint(raw_code, match.span()[1] - 1)
transform_plan_raw = js

# Steps are either c[x](c[y]) or c[x](c[y],c[z])
step_start = r"c\[(\d+)\]\(c\[(\d+)\](,c(\[(\d+)\]))?\)"
step_regex = re.compile(step_start)
matches = step_regex.findall(transform_plan_raw)
transform_steps = []
for match in matches:
if match[4] != '':
transform_steps.append((match[0],match[1],match[4]))
else:
transform_steps.append((match[0],match[1]))

return transform_steps

# Get the video URL
return video_stream.url

0 comments on commit b1b327b

Please sign in to comment.