问了DS,他给了这段代码,测试一下很好用,给需要的朋友。
[Python] - import os
- import json
- from datetime import datetime
- def find_target_json():
- base_dir = r"C:\Users\174501259\Documents\Bcut Drafts"
- target_files = []
-
- for root, dirs, files in os.walk(base_dir):
- for file in files:
- if file.lower().endswith(".json") and file.lower() != "draftinfo.json":
- full_path = os.path.join(root, file)
- mtime = os.path.getmtime(full_path)
- size = os.path.getsize(full_path)
- target_files.append((full_path, mtime, size))
-
- if not target_files:
- return None
- # 按修改时间降序和文件大小降序排序
- target_files.sort(key=lambda x: (-x[1], -x[2]))
- return target_files[0][0]
- def parse_subtitles(json_path):
- with open(json_path, 'r', encoding='utf-8') as f:
- data = json.load(f)
-
- subtitles = []
- for track in data.get('tracks', []):
- if track.get('BTrackType') == 0: # 只处理字幕轨道
- for clip in track.get('clips', []):
- asset = clip.get('AssetInfo', {})
- content = asset.get('content', '')
- in_point = clip.get('inPoint', 0)
- duration = clip.get('duration', 0)
-
- # 假设时间单位为毫秒
- start = in_point
- end = in_point + duration
-
- subtitles.append({
- 'start': start,
- 'end': end,
- 'content': content
- })
-
- # 按开始时间排序
- subtitles.sort(key=lambda x: x['start'])
- return subtitles
- def ms_to_srt_time(ms):
- 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}"
- def create_srt(subtitles):
- srt_content = []
- for i, sub in enumerate(subtitles, 1):
- start = ms_to_srt_time(sub['start'])
- end = ms_to_srt_time(sub['end'])
- srt_content.append(f"{i}\n{start} --> {end}\n{sub['content']}\n")
-
- return "\n".join(srt_content)
- def main():
- if os.path.exists("subtitles.srt"):
- return
-
- json_path = find_target_json()
- if not json_path:
- return
-
- subtitles = parse_subtitles(json_path)
- if not subtitles:
- return
-
- srt_content = create_srt(subtitles)
- with open("subtitles.srt", 'w', encoding='utf-8-sig') as f:
- f.write(srt_content)
- if __name__ == "__main__":
- main()
复制代码 |