Last active
February 12, 2022 17:55
-
-
Save CrabAss/cd12167da08a3fb8a35f909e3b0c87e2 to your computer and use it in GitHub Desktop.
MKV生肉+ASS字幕自动合并工具
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!C:/Users/janha/AppData/Local/Programs/Python/Python38/python.exe | |
""" | |
MKV 生肉 + ASS 字幕自动合并脚本 | |
1. 找到脚本所在目录下所有 MKV 和 ASS 文件(非递归) | |
2. 使用 mkvtoolnix 附带的 mkvmerge 合并 MKV 和 ASS 文件 | |
3. 将合并前的原始文件移动到 OriginalFiles 文件夹 | |
注意: | |
1. 使用前请先下载 mkvtoolnix:https://mkvtoolnix.download/downloads.html , | |
并将 mkvmerge_dir 设置为 mkvmerge 的所在路径。 | |
2. MKV 和 ASS 的文件命名方式可以不同,但必须保证 MKV 和 ASS 文件在数量上一一对应! | |
请注意总集篇等特殊集数是否有遗漏字幕,视频和字幕的文件名顺序是否一致;否则将导致视频与 | |
字幕不匹配! | |
该脚本由 MIT 许可证授权,且再分发时无需署名。欢迎在使用时按需要修改代码。 | |
""" | |
import subprocess | |
import os | |
cwd = os.path.dirname(os.path.realpath(__file__)) | |
mkvmerge_dir = 'C:\Applets\MediaTools\mkvtoolnix\mkvmerge.exe' | |
def get_files_with_ext(files, ext): | |
return sorted([f for f in files if f.endswith(f'.' + ext)]) | |
def get_filename_without_ext(filename): | |
if '.' not in filename: | |
return filename | |
return '.'.join(filename.split('.')[:-1]) | |
def main(): | |
print(cwd) | |
all_files = os.listdir(cwd) | |
mkv_files = get_files_with_ext(all_files, 'mkv') | |
ass_files = get_files_with_ext(all_files, 'ass') | |
if len(mkv_files) != len(ass_files): | |
raise Exception('MKV 和 ASS 文件个数不一致!') | |
try: | |
os.mkdir(f'{cwd}/OriginalFiles') | |
except FileExistsError: | |
pass | |
for i in range(len(mkv_files)): | |
merging_filename = get_filename_without_ext( | |
mkv_files[i]) + '.MERGING.mkv' | |
merge_command = [mkvmerge_dir, '-o', f'{cwd}/{merging_filename}', | |
f'{cwd}/{mkv_files[i]}', f'{cwd}/{ass_files[i]}'] | |
print(' '.join(merge_command)) | |
subprocess.run(merge_command) | |
os.rename(f'{cwd}/{mkv_files[i]}', | |
f'{cwd}/OriginalFiles/{mkv_files[i]}') | |
os.rename(f'{cwd}/{ass_files[i]}', | |
f'{cwd}/OriginalFiles/{ass_files[i]}') | |
os.rename(f'{cwd}/{merging_filename}', | |
f'{cwd}/{mkv_files[i]}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment