/
service.py
109 lines (93 loc) · 4.48 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import sys
import math
import json
import xbmc
import xbmcaddon
import xbmcgui
from lib.player import Player
LOG_TAG = "BlackBarsRemover"
addon = xbmcaddon.Addon()
def log(msg, level):
xbmc.log("%s: %s" % (LOG_TAG, msg), level=level)
def notify(text, seconds=None):
if seconds:
xbmc.executebuiltin('Notification(' + addon.getAddonInfo('name') + ', ' + text + ', ' + str(seconds * 1000) + ')')
else:
xbmc.executebuiltin('Notification(' + addon.getAddonInfo('name') + ', ' + text + ')')
def get_current_zoom_level():
result_raw = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "Player.GetViewMode", "id": 1}')
result_json = json.loads(result_raw)
if 'result' in result_json and 'zoom' in result_json['result']:
zoom = float("{:.2f}".format(result_json['result']['zoom']))
return zoom
log("Couldn't get zoom level properties, assuming no zoom", xbmc.LOGWARNING)
return 1.0
def apply_zoom(level):
result_raw = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "Player.SetViewMode", "params": {"viewmode": {"zoom": ' + str(level) + ' }}, "id": 1}')
result_json = json.loads(result_raw)
if 'result' in result_json and result_json['result'] == 'OK':
return True
return False
def get_desired_zoom_level():
blackbarcomp = None
blackbarcomp_raw = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "Settings.GetSettingValue", "params": {"setting": "videoplayer.errorinaspect"}, "id": 1}')
blackbarcomp_json = json.loads(blackbarcomp_raw)
try:
blackbarcomp = int(blackbarcomp_json['result']['value'])
except (TypeError, KeyError):
return 0
aspectratio_screen = float(xbmcgui.getScreenWidth()) / float(xbmcgui.getScreenHeight())
log("Current screen aspect ratio: %f" % aspectratio_screen, xbmc.LOGDEBUG)
aspectratio_video_raw = xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "Player.GetProperties", "params": {"playerid": 1, "properties": ["currentvideostream"]}, "id": 1}')
aspectratio_video_json = json.loads(aspectratio_video_raw)
try:
aspectratio_video = float(aspectratio_video_json['result']['currentvideostream']['width']) / float(aspectratio_video_json['result']['currentvideostream']['height'])
except (TypeError, KeyError):
return 0
log("Current video aspect ratio: %f" % aspectratio_video, xbmc.LOGDEBUG)
aspectratio_compensation = aspectratio_video - (aspectratio_video * (blackbarcomp / 100.0))
ratio_correction = (aspectratio_compensation / aspectratio_screen)
zoomlevel = math.ceil(ratio_correction * 100.0) / 100.0
user_compensation = int(addon.getSetting('user_compensation'))
zoomlevel = zoomlevel + (user_compensation / 100.0)
maximum_zoom_level = float(1.0 + int(addon.getSetting('maximum_compensation')) / 100.0)
if zoomlevel > maximum_zoom_level:
zoomlevel = maximum_zoom_level
return zoomlevel
monitor = xbmc.Monitor()
player = Player()
dialog = xbmcgui.Dialog()
while not monitor.abortRequested():
if monitor.waitForAbort(1):
break
if player.isPlaying():
last_file = player.getLastFile()
try:
current_file = player.getPlayingFile()
except Exception:
player.setPlaying(False)
continue
if last_file is None or last_file != current_file:
log("New video now playing", xbmc.LOGDEBUG)
player.setLastFile(current_file)
zoomlevel = get_desired_zoom_level()
if zoomlevel == 0:
log("Could not determine black bar compensation", xbmc.LOGERROR)
notify(addon.getLocalizedString(30003))
continue
log("Desired zoom level: %f" % zoomlevel, xbmc.LOGDEBUG)
if zoomlevel <= 1.0:
log("No zoom adjustment necessary", xbmc.LOGINFO)
continue
zoomlevel_current = get_current_zoom_level()
log("Current zoom level: %f" % zoomlevel_current, xbmc.LOGDEBUG)
if zoomlevel_current == zoomlevel:
log("Already got correct zoomlevel", xbmc.LOGINFO)
continue
if apply_zoom(zoomlevel):
log("Applied zoom level successfully", xbmc.LOGINFO)
if addon.getSetting('zoom_apply_notification') == 'true':
notify(addon.getLocalizedString(30001) % int(round((zoomlevel - 1.0) * 100.0)))
else:
log("Could not apply zoom", xbmc.LOGERROR)
notify(addon.getLocalizedString(30004))