Skip to content

Commit

Permalink
Fix #268: 修复紧跟影片番号的CDx被误识别为内嵌字幕的问题
Browse files Browse the repository at this point in the history
(cherry picked from commit 729f652)
  • Loading branch information
Yuukiy committed Apr 5, 2024
1 parent 562ea70 commit 199417c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion core/avid.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ def get_id(filepath: str) -> str:
return ''


CD_POSTFIX = re.compile(r'([-_]\w|cd\d)$')
def get_cid(filepath: str) -> str:
"""尝试将给定的文件名匹配为CID(Content ID)"""
basename = os.path.splitext(os.path.basename(filepath))[0]
# 移除末尾可能带有的分段影片序号
possible = re.sub(r'[-_]\w$', '', basename)
possible = CD_POSTFIX.sub('', basename)
# cid只由数字、小写字母和下划线组成
match = re.match(r'^([a-z\d_]+)$', possible, re.A)
if match:
Expand Down
4 changes: 2 additions & 2 deletions core/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def detect_special_attr(filepath: str, avid: str = None) -> str:
if postfix in ('U', 'C', 'UC'):
result += postfix
elif avid:
pattern_str = re.sub(r'[_-]', '[_-]*', avid) + '(UC|U|C)'
pattern_str = re.sub(r'[_-]', '[_-]*', avid) + r'(UC|U|C)\b'
match = re.search(pattern_str, base, flags=re.I)
if match:
result += match.group(1)
Expand All @@ -69,4 +69,4 @@ def detect_special_attr(filepath: str, avid: str = None) -> str:


if __name__ == "__main__":
print(detect_special_attr('STARS225Uc.mp4', 'STARS-225'))
print(detect_special_attr('ipx-177cd1.mp4', 'IPX-177'))
1 change: 1 addition & 0 deletions unittest/test_avid.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_cid_valid():
assert '1234wvr00001rp' == get_cid('1234wvr00001rp.mp4')
assert '402abc_hello000089' == get_cid('402abc_hello000089.mp4')
assert 'h_826zizd021' == get_cid('h_826zizd021.mp4')
assert '403abcd56789' == get_cid('403abcd56789cd1.mp4')


def test_from_file():
Expand Down
11 changes: 11 additions & 0 deletions unittest/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ def test_scan_movies__cdx(prepare_files):
assert basenames[2] == 'ABC-123.CD3.mp4'


@pytest.mark.parametrize('files', [('abc123cd1.mp4','abc123cd2.mp4')])
def test_scan_movies__cdx_without_delimeter(prepare_files):
movies = scan_movies(tmp_folder)
assert len(movies) == 1
assert movies[0].dvdid == 'abc-123'
assert len(movies[0].files) == 2
basenames = [os.path.basename(i) for i in movies[0].files]
assert basenames[0] == 'abc123cd1.mp4'
assert basenames[1] == 'abc123cd2.mp4'


# 文件夹以番号命名,分片位于文件夹内且无番号信息
@pytest.mark.parametrize('files', [('ABC-123/CD1.mp4','ABC-123/CD2 .mp4','ABC-123/CD3.mp4')])
def test_scan_movies__from_folder(prepare_files):
Expand Down
3 changes: 2 additions & 1 deletion unittest/test_lib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import sys
import random

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from core.lib import *
Expand All @@ -23,3 +22,5 @@ def test_detect_special_attr():
assert run('STARS225u.mp4', 'STARS-225') == 'U'
assert run('STARS225C.mp4', 'STARS-225') == 'C'
assert run('STARS225uC.mp4', 'STARS-225') == 'UC'
assert run('STARS-225CD1.mp4', 'STARS-225') == ''
assert run('stars225cd2.mp4', 'STARS-225') == ''

0 comments on commit 199417c

Please sign in to comment.