Skip to content

Commit

Permalink
Updated to 2.43
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxe authored and Maxe committed Jun 9, 2022
1 parent a75396e commit 586f3c9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 88 deletions.
Binary file modified bgm.mp3
Binary file not shown.
25 changes: 25 additions & 0 deletions source/doc/zh_TW/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# 更新日誌

## V2.43, 2022.6.9

### 新增項目

1. 新增 Youtube 頻道宣傳影片的字幕支援。
測試連結:[KSP 的頻道,宣傳影片:【KSP】撥条少女時計【歌ってみた】](https://www.youtube.com/c/KSPKSP)
測試方式:點開上面的連結,會進入 KSP 的 Youtube 頻道首頁,若您沒有訂閱 KSP 的頻道,則會在頻道首頁出現宣傳影片,請將焦點移至影片播放器,若您是 Firefox 瀏覽器使用者,影片不會立即開始播放,請點擊播放按鈕。由於目前該頻道的宣傳影片有提供 CC 字幕,則可在播放時聽取字幕內容。

### 修正巷木

1. 此次複雜的修正,全是參考 KSP 宣傳影片當中的字幕內容。
影片:[【KSP】撥条少女時計【歌ってみた】](https://www.youtube.com/watch?v=LFLyLIfcueQ&ab_channel=KSP)
1. 修正字幕閱讀器只會閱讀 Youtube 字幕第一行的錯誤。
之前參考影片《 Embrace 》修正的,其實並非多行字幕,而是同一行字幕被拆分成多個區塊而已。
這次參考的影片,確實能夠使用瀏覽模式瀏覽到多行的字幕內容,之前的參考影片,使用瀏覽模式查看,結果都只有單行字幕。
2. 修正此參考影片會不斷朗讀相同字幕內容的錯誤。
問題產生原因:可能為了視覺效果,此歌曲的影片字幕會顯示許多行相同的歌詞,有些還會再不訂行出現上一句歌詞,且每一行位於開頭與結尾還會出現不定數量的空格與一個特殊符號。
處理方式:檢查字幕的每一行,過濾掉開頭與結尾的空格與特殊符號,然後在其他字幕行尋找是否有相同的字幕內容,若有則將其刪除。


P.S 第一次寫到卡關卡超九,每次寫完,測試結果都失敗,繼續找可能問題的概念。 KSP 為啥填這樣的字幕還被我看到 (X
打倒 KSP!

---

## V2.41, 2022.5.29

版本代號: If Only
Expand Down
83 changes: 20 additions & 63 deletions source/globalPlugins/subtitle_reader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, *args, **kwargs):
self.videoPlayer = None
self.subtitleContainer = None
self.subtitle = str()
self.emptySubTitleTime = 0
self.emptySubtitleTime = 0
# 使用 wx.PyTimer 不斷執行函數
self.read_subtitle_timer = wx.PyTimer(self.read_subtitle)

Expand Down Expand Up @@ -147,87 +147,44 @@ def read_subtitle(self):

if not subtitle:
# 沒有字幕超過一秒鐘才清除字幕緩衝區。
if not self.emptySubTitleTime:
self.emptySubTitleTime = time.time()
elif time.time() - self.emptySubTitleTime >= 1:
if not self.emptySubtitleTime:
self.emptySubtitleTime = time.time()
elif time.time() - self.emptySubtitleTime >= 1:
self.subtitle = ''
self.emptySubTitleTime = 0

return

self.emptySubtitleTime = 0

msg = subtitle

# 若新的字幕內容是前一字幕的一部分,則不報讀。
if subtitle in self.subtitle:
msg = None
msg = ''

# 若新的字幕包含前一字幕的內容,則只報讀填充的部分。
if self.subtitle and self.subtitle in subtitle:
msg = subtitle.replace(self.subtitle, '', 1)

split = self.subtitle.split('\r\n')
# 使用換行符號來忽略兩次字幕之間相同的內容
split = subtitle.split('\r\n')
for part in split:
if part in msg:
msg = msg.replace(part, '')


ui.message(msg)
self.subtitle = subtitle

def getStartOrEndSameStr(self, a, b):
def getStartSameStr(a, b):
same = []
for i in range(0, len(a)):
if a[i] != b[i:i+1]:
break

same.append(a[i])
part = part.strip()
count = msg.count(part)
if count > 1:
msg = msg.replace(part, '', count -1)

return ''.join(same)

sameStr = ['', '', '', '']
maxLen = 0
maxLenIndex = -1
if a[0:1] == b[0:1]:
s = getStartSameStr(a, b)
sameStr[0] = s
maxLen = len(s)
maxLenIndex = 0

n = a.rfind(b[-1:])
if n >= 0:
if a[:n+1] == b[n*-1-1:]:
sameStr[1] = a[:n+1]
if len(sameStr[1]) > maxLen:
maxLen = len(sameStr[1])
maxLenIndex = 1

if part in self.subtitle:
msg = msg.replace(part, '')


a = a[::-1]
b = b[::-1]

if a[0:1] == b[0:1]:
s = getStartSameStr(a, b)
sameStr[2] = s[::-1]
if len(sameStr[2]) > maxLen:
maxLen = len(s)
maxLenIndex = 0

n = a.find(b[-1:])
if n >= 0:
if a[:n+1] == b[n*-1-1:]:
sameStr[3] = a[:n+1:-1][::-1]
if len(sameStr[1]) > maxLen:
maxLen = len(sameStr[1])
maxLenIndex = 1


msg = msg.strip()

if maxLenIndex == -1:
return ''
if not msg:
msg = None

return sameStr[maxLenIndex]
ui.message(msg)
self.subtitle = subtitle

def toggleInfoCardPrompt(self, evt):
conf['infoCardPrompt'] = not conf['infoCardPrompt']
Expand Down
4 changes: 4 additions & 0 deletions source/globalPlugins/subtitle_reader/object_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def find(obj, nextAttr, attrName, attrValue):
pass


if isinstance(attrValue, list) and value in attrValue:
log.debug(attrName + ' = ' + str(value) + ' found in the list. ')
return o

if value == attrValue:
log.debug(attrName + ' = ' + str(attrValue) + ' found. ')
return o
Expand Down
52 changes: 28 additions & 24 deletions source/globalPlugins/subtitle_reader/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .sound import play
from .config import conf
from .subtitle_alg import SubtitleAlg
from .object_finder import find

class Youtube(SubtitleAlg):
def __init__(self, *args, **kwargs):
Expand All @@ -16,13 +17,8 @@ def getVideoPlayer(self):
'''
根據元件 id 找出 Youtube 影片撥放器
'''
o = self.main.focusObject
while o:
if o.IA2Attributes.get('id') == 'movie_player':
return o

o = o.parent

obj = self.main.focusObject
return find(obj, 'parent', 'id', ['movie_player', 'c4-player'])

def getSubtitleContainer(self):
# 由於 Youtube 的字幕容器是不停變動的,所以改為在每次取得字幕時一併取得容器,在此方法回傳 True 作為假的字幕容器。
Expand Down Expand Up @@ -65,14 +61,19 @@ def chromeGetSubtitle(self):

subtitle = ''

obj = getattr(obj, 'firstChild', None)
obj = getattr(obj, 'firstChild', None)
while obj is not None:
# 取得多行字幕
if obj.name.strip():
subtitle += obj.name + '\r\n'
line = getattr(obj, 'firstChild', None)
# 變例每一行字幕
while line is not None:
part = line.firstChild
# 處理一行當中被切成多個部分的字幕
while part is not None:
text = getattr(part, 'name', '').strip()
if text:
subtitle += text + '\r\n'

part = part.next

obj = obj.next
line = line.next

return subtitle

Expand All @@ -84,18 +85,21 @@ def firefoxGetSubtitle(self):
subtitle = ''
obj = obj.firstChild
while obj is not None and 'caption-window-' in str(obj.IA2Attributes.get('id')):
# 取得多行字幕
char_obj = obj.firstChild.firstChild
for i in range(char_obj.parent.childCount):
try:
try:
# 取得多行字幕
char_obj = obj.firstChild.firstChild
for i in range(char_obj.parent.childCount):
# 處理一個元素只放一個字的狀況
subtitle += char_obj.firstChild.firstChild.name
except:
return ''
subtitle += char_obj.firstChild.firstChild.name + '\r\n'
char_obj = char_obj.next

char_obj = char_obj.next
subtitle += '\r\n'
obj = obj.next
obj = obj.next

except:
return


return subtitle

def msedgeGetSubtitle(self):
Expand Down
2 changes: 1 addition & 1 deletion source/manifest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ summary = 字幕閱讀器
description = "當焦點位於網頁上的影片播放器時,讓 NVDA 讀出字幕。"
author = "謝福恩 <[email protected]>"
url = https://github.com/maxe-hsieh/subtitle_reader
version = 2.41
version = 2.43
docFileName = readme.html
lastTestedNVDAVersion = 2022.1

0 comments on commit 586f3c9

Please sign in to comment.