Skip to content

Commit

Permalink
Added detection of multiple aspect ratios
Browse files Browse the repository at this point in the history
  • Loading branch information
osumoclement committed Sep 11, 2023
1 parent ecf4a33 commit e688833
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
36 changes: 21 additions & 15 deletions addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import xbmcaddon
import xbmcgui
import soupsieve

from imdb import getOriginalAspectRatio

monitor = xbmc.Monitor()
Expand All @@ -14,7 +15,6 @@
CaptureWidth = 48
CaptureHeight = 54


def notify(msg):
xbmcgui.Dialog().notification("BlackBarsNever", msg, None, 1000)

Expand Down Expand Up @@ -84,18 +84,20 @@ def GetAspectRatioFromFrame(self):
# is not dark
while (True):
__myimage = self.CaptureFrame()

xbmc.log(line1, level=xbmc.LOGINFO)

__middleScreenDark = self.LineColorLessThan(
__myimage, 7, 2, __threshold)
if __middleScreenDark == False:
xbmc.sleep(1000)
# xbmc.sleep(1000)
break
else:
xbmc.sleep(1000)
pass
# xbmc.sleep(1000)

# Capture another frame. after we have waited for transitions
__myimage = self.CaptureFrame()
# __myimage = self.CaptureFrame()
__ar185 = self.LineColorLessThan(__myimage, 0, 1, __threshold)
__ar200 = self.LineColorLessThan(__myimage, 1, 3, __threshold)
__ar235 = self.LineColorLessThan(__myimage, 1, 5, __threshold)
Expand All @@ -115,6 +117,7 @@ def abolishBlackBars(self):
xbmcgui.Window(10000).setProperty('blackbarsnever_status', "on")
# notify(xbmcgui.Window(10000).getProperty('blackbarsnever_status'))

original_aspect_ratio = None
android_workaround = (xbmcaddon.Addon().getSetting(
"android_workaround") == 'true')

Expand All @@ -125,21 +128,24 @@ def abolishBlackBars(self):
if not title:
title = os.path.basename(
player.getVideoInfoTag().getFilenameAndPath()).split('/')[-1].split(".", 1)[0]

original_aspect_ratio = getOriginalAspectRatio(
title, imdb_number=imdb_number)
title, imdb_number=imdb_number)

if isinstance(original_aspect_ratio, list):
# media has multiple aspect ratios, so just assume the media reported one
if android_workaround == True:
notify("Multiple aspect ratios detected")
else:
notify("Multiple aspect ratios detected")

aspect_ratio = int((capture.getAspectRatio() + 0.005) * 100)
# media has multiple aspect ratios, show unaltered and let user do manual intervention
notify("Multiple aspect ratios detected")
else:
aspect_ratio = int(original_aspect_ratio)
if android_workaround != True:
aspect_ratio = self.GetAspectRatioFromFrame()
if android_workaround:
aspect_ratio = int(original_aspect_ratio)

self.doStiaff(aspect_ratio)
else:
aspect_ratio = self.GetAspectRatioFromFrame()
self.doStiaff(aspect_ratio)

def doStiaff(self, ratio):
aspect_ratio = ratio
aspect_ratio2 = int((capture.getAspectRatio() + 0.005) * 100)

window_id = xbmcgui.getCurrentWindowId()
Expand Down
2 changes: 1 addition & 1 deletion addon.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.black.bars.never" name="BlackBarsNever" version="1.0.8" provider-name="Clement Osumo">
<addon id="script.black.bars.never" name="BlackBarsNever" version="1.0.9" provider-name="Clement Osumo">
<requires>
<import addon="xbmc.python" version="3.0.0"/>
<import addon="script.module.requests" version="2.20.0"/>
Expand Down
50 changes: 33 additions & 17 deletions imdb.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
import requests
from bs4 import BeautifulSoup

import xbmc
import xbmcgui

def notify(msg):
xbmcgui.Dialog().notification("BlackBarsNever", msg, None, 1000)

def getOriginalAspectRatio(title, imdb_number=None):
BASE_URL = "https://www.imdb.com/"
HEADERS = {
'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'}

"""
if imdb_number:
URL = "{}/title/{}/".format(BASE_URL, imdb_number)
else:
URL = BASE_URL + "find/?q={}".format(title)
search_page = requests.get(URL, headers=HEADERS)
"""
URL = BASE_URL + "find/?q={}".format(title)
search_page = requests.get(URL, headers=HEADERS)

# lxml parser would have been better but not currently supported in Kodi
soup = BeautifulSoup(search_page.text, 'html.parser')
# lxml parser would have been better but not currently supported in Kodi
soup = BeautifulSoup(search_page.text, 'html.parser')

title_url_tag = soup.select_one(
'.ipc-metadata-list-summary-item__t')
if title_url_tag:
# we have matches, pick the first one
title_url = title_url_tag['href']
imdb_number = title_url.rsplit(
'/title/', 1)[-1].split("/")[0]
# this below could have worked instead but for some reason SoupSieve not working inside Kodi
"""title_url = soup.css.select(
'.ipc-metadata-list-summary-item__t')[0].get('href')
"""
title_url_tag = soup.select_one(
'.ipc-metadata-list-summary-item__t')
if title_url_tag:
# we have matches, pick the first one
title_url = title_url_tag['href']
imdb_number = title_url.rsplit(
'/title/', 1)[-1].split("/")[0]
# this below could have worked instead but for some reason SoupSieve not working inside Kodi
"""title_url = soup.css.select(
'.ipc-metadata-list-summary-item__t')[0].get('href')
"""

URL = BASE_URL + title_url
URL = BASE_URL + title_url

title_page = requests.get(URL, headers=HEADERS)
soup = BeautifulSoup(title_page.text, 'html.parser')

# this below could have worked instead but for some reason SoupSieve not working inside Kodi
aspect_ratio_tags = soup.find(
attrs={"data-testid": "title-techspec_aspectratio"})

if aspect_ratio_tags:
aspect_ratio_full = aspect_ratio_tags.select_one(
".ipc-metadata-list-item__list-content-item").decode_contents()
Expand All @@ -58,9 +66,17 @@ def getOriginalAspectRatio(title, imdb_number=None):
for li in aspect_ratio_li:
aspect_ratio_full = li.select_one(
".ipc-metadata-list-item__list-content-item").decode_contents()

aspect_ratio = aspect_ratio_full.split(':')[0].replace('.', '')
sub_text = li.select_one(".ipc-metadata-list-item__list-content-item--subText").decode_contents()

if sub_text == "(theatrical ratio)":
xbmc.log("using theatrical ratio " + str(aspect_ratio), level=xbmc.LOGINFO)
return aspect_ratio


aspect_ratios.append(aspect_ratio)

return aspect_ratios

return aspect_ratio
return aspect_ratio
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Currently, Kodi can't capture sreenshots in Android and Embedded Systems if hard

1) Disable hardware acceleration (turn off MediaCodec Surface in Android). The problem with this is that Kodi will now use CPU for decoding and playback may be affected to the point of being unwatchable, especially for high bitrate media. Also in the devices I tested, HDR won't work on Android if hardware acceleration is turned on, I am not sure if this affects all of Android.

2) Enable the Android & Embedded Systems Workaround from the addon settings. This feature requires an internet connection to fetch media metadata, and works best if your library adopts a decent naming pattern i.e `Title Year`. Also works properly only if media aspect ratio is unchanged from original
2) Enable the Android & Embedded Systems Workaround from the addon settings. This feature requires an internet connection to fetch media metadata, and works best if your library adopts a decent naming pattern i.e `Title Year`. Also works properly only if media aspect ratio is unchanged from original (i.e has not been cropped from the original)

# Installation
Download the zip file from [releases](https://github.com/osumoclement/script.black.bars.never/releases)
Expand All @@ -30,6 +30,9 @@ Launch Kodi >> Add-ons >> Get More >> Install from zip file

Feel free to ask any questions, submit feature/bug reports

# Multiple Aspect Ratios in Media
For media with multiple aspect ratios, the addon will notify you of this, and will do nothing. In such cases, I recommend you watch the media as is, since if you change the aspect ratio manually, you may not know where in the media the ratio changes in order to adjust again.

# Customization
There are a few ways to customize the addon
By default, the addon automatically removes black bars. If you want to change this behavior, you can turn this off in the addon settings. You would then need to manually trigger the addon by manually calling it from elsewhere in Kodi (ie from a Skin) like this `RunScript(script.black.bars.never,toggle)`. You could even map this to a key for convenience
Expand Down

0 comments on commit e688833

Please sign in to comment.