-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
65 lines (55 loc) · 2.67 KB
/
Copy pathutils.py
File metadata and controls
65 lines (55 loc) · 2.67 KB
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
import re
import os
class VideoSubFinderUtils:
"""VideoSubFinder工具类"""
@staticmethod
def parse_filename(filename):
"""
解析VideoSubFinder生成的文件名,提取时间戳
支持格式如: 0_00_18_320__0_00_19_279_*.jpeg
"""
# 优先处理 VideoSubFinder 文件格式 "0_00_18_320__0_00_19_279_*.jpeg"
# 我们需要匹配一个以数字和下划线开头,双下划线分隔的模式
# 正则表达式:(\d+)_(\d+)_(\d+)_(\d+)__(\d+)_(\d+)_(\d+)_(\d+).*
# '.*' 用于匹配文件名后面的任意字符,避免使用 rsplit
match = re.search(r'(\d+)_(\d+)_(\d+)_(\d+)__(\d+)_(\d+)_(\d+)_(\d+)', filename)
if match:
# 提取第一个时间戳的各部分
h1, m1, s1, ms1 = map(int, match.groups()[:4])
# 提取第二个时间戳的各部分
h2, m2, s2, ms2 = map(int, match.groups()[4:])
# 格式化为 hh:mm:ss,mmm
start_time = f"{h1:02d}:{m1:02d}:{s1:02d},{ms1:03d}"
end_time = f"{h2:02d}:{m2:02d}:{s2:02d},{ms2:03d}"
return start_time, end_time
# 如果需要,可以在这里添加其他匹配规则
# 例如,你可以将原代码中第一个匹配规则放在这里
match_other = re.search(r'(\d{2}-\d{2}-\d{2}-\d{3})_(\d{2}-\d{2}-\d{2}-\d{3})', filename)
if match_other:
start_str = match_other.group(1).replace('-', ':').replace(':', ',', 2)
end_str = match_other.group(2).replace('-', ':').replace(':', ',', 2)
return start_str, end_str
# 最后,添加一个通用的、不那么精确的匹配规则
# 注意: 如果这个规则会误匹配,最好删除或修改
match_generic = re.search(r'(\d+)_(\d+)', filename)
if match_generic:
start_ms = int(match_generic.group(1)) * 10
end_ms = int(match_generic.group(2)) * 10
# 这里为了演示,我们先不处理
pass
raise ValueError("Could not extract timestamp from filename.")
@staticmethod
def format_milliseconds(ms):
"""将毫秒数转换为 'hh:mm:ss,mmm' 格式"""
hours = ms // 3600000
ms %= 3600000
minutes = ms // 60000
ms %= 60000
seconds = ms // 1000
milliseconds = ms % 1000
return f"{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}"
@staticmethod
def get_image_files(directory):
"""获取目录中的所有图像文件"""
image_extensions = ('.png', '.bmp', '.jpg', '.jpeg')
return sorted([f for f in os.listdir(directory) if f.lower().endswith(image_extensions)])