Skip to content

Commit

Permalink
Improve video quality fallback
Browse files Browse the repository at this point in the history
Improves video quality fallback for progressive
videos.

Resolves #65
  • Loading branch information
jaylinski committed Apr 26, 2024
1 parent 9013630 commit 3b9883a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
[![CI Build Status](https://github.com/jaylinski/kodi-addon-vimeo/actions/workflows/ci.yml/badge.svg)](https://github.com/jaylinski/kodi-addon-vimeo/actions)
[![Link to Kodi forum](https://img.shields.io/badge/Kodi-Forum-informational.svg)](https://forum.kodi.tv/showthread.php?tid=220437)
[![Link to Kodi wiki](https://img.shields.io/badge/Kodi-Wiki-informational.svg)](https://kodi.wiki/view/Add-on:Vimeo)
[![Link to Kodi releases](https://img.shields.io/badge/Kodi-v19%20%22Matrix%22-green.svg)](https://kodi.wiki/view/Releases)

This [Kodi](https://github.com/xbmc/xbmc) Add-on provides a minimal interface for Vimeo.

Expand All @@ -16,6 +15,12 @@ This [Kodi](https://github.com/xbmc/xbmc) Add-on provides a minimal interface fo
* Discover new videos
* Play videos

### Compatibility

[![Link to Kodi releases](https://img.shields.io/badge/Kodi-v19%20%22Matrix%22-green.svg)](https://kodi.wiki/view/Releases)
[![Link to Kodi releases](https://img.shields.io/badge/Kodi-v20%20%22Nexus%22-green.svg)](https://kodi.wiki/view/Releases)
[![Link to Kodi releases](https://img.shields.io/badge/Kodi-v21%20%22Omega%22-green.svg)](https://kodi.wiki/view/Releases)

## Installation

### Kodi Repository
Expand Down
21 changes: 10 additions & 11 deletions resources/lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,6 @@ def _get_on_demand_trailer(self, uri):
res = self._do_api_request(uri, {"fields": "uri,type,play"})
return self._extract_url_from_search_response(res["play"])

def _video_matches(self, video, video_format):
video_height = video_format[1].replace("p", "")
return str(video["height"]) == video_height and video["codec"] == self._get_video_codec()

def _extract_url_from_search_response(self, video_files):
video_format = self.settings.VIDEO_FORMAT[self.video_stream]
video_format = video_format.split(":")
Expand All @@ -315,17 +311,20 @@ def _extract_url_from_search_response(self, video_files):
return self._hls_playlist_without_av1_streams(video_files["hls"]["link"])

elif video_type == "progressive" or video_files.get("hls") is None:
for video_file in video_files["progressive"]:
if self._video_matches(video_file, video_format):
return video_file["link"]
# Use a smart quality fallback for videos with weird ratios like 1000x1000
nearest_lower_quality = [0, None]
video_height = video_format[1].replace("p", "")
video_height = int(video_height) if video_height.isdigit() else 0

# Fallback if no matching quality was found
for video_file in video_files["progressive"]:
if video_file["codec"] == self._get_video_codec():
return video_file["link"]
if video_file["height"] == video_height:
return video_file["link"]
if video_height > video_file["height"] > nearest_lower_quality[0]:
nearest_lower_quality = [video_height, video_file["link"]]

# Fallback if fallback failed
return video_files["progressive"][0]["link"]
# Fallback if no exact match
return nearest_lower_quality[1] or video_files["progressive"][0]["link"]

else:
raise RuntimeError("Could not extract video URL")
Expand Down
5 changes: 3 additions & 2 deletions tests/mocks/api_videos_detail.json
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@
"type": "video/mp4",
"codec": "H264",
"width": 1280,
"height": "720 made invalid for test",
"COMMENT": "Value 'height' below was changed for testing the quality fallback!",
"height": 721,
"link_expiration_time": "2020-03-30T00:04:34+00:00",
"link": "https://vimeo-prod-skyfire-std-us.storage.googleapis.com/01/498/14/352494023/1430794545.mp4",
"created_time": "2019-08-07T14:25:20+00:00",
Expand Down Expand Up @@ -555,7 +556,7 @@
"width": 960,
"height": 540,
"link_expiration_time": "2020-03-30T00:04:34+00:00",
"link": "https://vimeo-prod-skyfire-std-us.storage.googleapis.com/01/498/14/352494023/1430794417.mp4?GoogleAccessId=GOOGLW2TRT7BCCZZO5AX&Expires=1585526674&Signature=0IWqcv2PLmNPNMcgLbWUolsuCy8%3D",
"link": "https://vimeo-prod-skyfire-std-us.storage.googleapis.com/01/498/14/352494023/1430794417.mp4",
"created_time": "2019-08-07T14:25:17+00:00",
"fps": 24,
"size": 15382541,
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/lib/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def test_resolve_media_url(self):
self.api.video_stream = "720p" # Resolution does not exist in API response
self.api._do_api_request = Mock(return_value=json.loads(mock_data))
res = self.api.resolve_media_url("/videos/352494023")
self.assertEqual(res, "https://vimeo-prod-skyfire-std-us.storage.googleapis.com/01/498/14/352494023/1430794570.mp4")
self.assertEqual(res, "https://vimeo-prod-skyfire-std-us.storage.googleapis.com/01/498/14/352494023/1430794417.mp4")

# HLS stream
self.api.video_stream = "HLS (Adaptive)"
Expand Down

0 comments on commit 3b9883a

Please sign in to comment.