Skip to content
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

Varying Resolutions #2

Open
RMGhub opened this issue Jul 30, 2024 · 0 comments
Open

Varying Resolutions #2

RMGhub opened this issue Jul 30, 2024 · 0 comments

Comments

@RMGhub
Copy link

RMGhub commented Jul 30, 2024

I have an issue with some videos being a varied height resulting in a "list index out of range" error.

Chat gippity provided some code to get the nearest resolution it could with this rewrite:
def parse_resolution(metadata, resolution, filename):
try:
with open(metadata, "r") as file:
res = json.load(file)

        resolution_mapping = {
            "1080p": 1080,
            "720p": 720,
            "540p": 540,
            "480p": 480,
            "360p": 360,
        }

        selected_resolution = resolution_mapping.get(resolution, 1080)

        # Find the closest available resolution
        available_resolutions = [(x["height"], x["url"]) for x in res]
        available_resolutions.sort(key=lambda x: abs(x[0] - selected_resolution))

        if available_resolutions:
            closest_resolution = available_resolutions[0]
            video_url = closest_resolution[1]
            download_video(video_url, filename + ".mp4")
        else:
            print("No video URLs found.")
except Exception as e:
    print(e)
    sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant