Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions modules/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,27 @@ def __init__(
self.video_id_to_path = OrderedDict()

def add(self, url: str):
video = YouTube(url)
# Download video of set resolution
yt = YouTube(url)
video = (
video.streams.filter(
yt.streams.filter(
resolution="360p",
progressive=True,
)
.order_by("resolution")
.desc()
.first()
)
# for shorts get any progressive stream
if video is None:
video = (
yt.streams.filter(progressive=True)
.order_by("resolution")
.asc()
.first()
)
if video is None:
logging.error(f"No suitable stream found for {url}")
return None
if video.filesize > self.max_size_bytes:
logging.info(
f"Video size ({video.filesize} bytes) exceeds max cache size ({self.max_size_bytes} bytes). Caching cancelled."
Expand All @@ -67,8 +77,8 @@ def add(self, url: str):
logging.info(f"downloaded {url} to path {video_file_path}")
video_info = VideoInfo(
file_path=video_file_path,
thumbnail=YouTube(url).thumbnail_url,
title=YouTube(url).title,
thumbnail=yt.thumbnail_url,
title=yt.title,
size_bytes=video.filesize,
)
self.video_id_to_path[video_id] = video_info
Expand Down Expand Up @@ -156,5 +166,9 @@ def write_cache(self):
@staticmethod
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MEDIUM] The parse_qs function returns a dictionary where keys might be missing if the query parameter is not present. Accessing ['v'][0] directly without checking for the existence of the 'v' key can lead to a KeyError if the URL is malformed or does not contain the 'v' parameter. Consider adding a check for the 'v' key before accessing it, or using .get('v') with a default value and then handling the potential None.

def get_video_id(url) -> str:
parsed_url = urlparse(url)
# handled yt shorts URL format: youtube.com/shorts/id
if "/shorts/" in parsed_url.path:
return parsed_url.path.split("/shorts/")[1].split("?")[0]
# handled regular URL format: youtube.com/watch?v=VIDEO_ID
video_id = parse_qs(parsed_url.query)["v"][0]
return video_id
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ fastapi==0.96.0
uvicorn==0.22.0
py-grpc-prometheus==0.7.0
psutil==5.9.8
pytubefix==9.1.1
pytubefix>=9.1.1
3 changes: 2 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def create_ffmpeg_stream(
"-i",
video_path,
"-vf",
f"scale=640:360",
# this helps the shorts to actually be vertical and not compressed
"scale=640:360:force_original_aspect_ratio=decrease,pad=640:360:(ow-iw)/2:(oh-ih)/2",
"-c:v",
"libx264",
"-preset",
Expand Down
2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h2 id="cache_tag">SCE TV</h2>
</div>
<div style="display: flex; justify-content: center; align-items: center; padding-bottom: 10px;">
<button id="show-logs-btn">Show Logs</button>
<button id="debug-btn">Debug</button>
<button id="debug-btn"> Debug </button>
</div>
</div>

Expand Down