# 格式轉化文件,包含多種文件的互相轉化,主要與視頻相關
from pathlib import Path
import subprocess
import random
import os
import reclass Utils(object):@staticmethoddef get_decimal_part(x: float) -> float:s = format(x, '.15f') # 格式化為15位小數字符串if '.' in s:_, decimal_part = s.split('.')decimal_str = decimal_part.rstrip('0') # 移除末尾多余的0if not decimal_str: # 如果小數部分全為0return 0.0result = float("0." + decimal_str)return -result if x < 0 else result # 處理負數return 0.0@staticmethoddef file_path_processor(*file_paths: str) -> str | list[str]:if len(file_paths) == 1:return file_paths[0].replace("\\", "\\\\")return [file_path.replace("\\", "\\\\") for file_path in file_paths]class TimeConverter(object):def time_analysis(self, time_str, offset: float = 0.0) -> tuple[int, int, int, int]:pattern = r'^(\d{1,2})\D(\d{1,2})\D(\d{1,2})[.,](\d{1,3})$'match: re.Match = re.match(pattern, time_str)if not match:raise ValueError(f"無法解析的時間格式: {time_str}")results = list(match.groups())results[-1] = f"{results[-1]:0<3}"hours, minutes, seconds, milliseconds = map(int, results)if offset != 0:milliseconds += int(Utils.get_decimal_part(offset) * 1000)offset = int(offset)hours += (offset // 3600)minutes += (offset // 60)seconds += (offset % 60)# 注意進位seconds += milliseconds // 1000minutes += seconds // 60hours += minutes // 60seconds %= 60milliseconds %= 1000minutes %= 60return hours, minutes, seconds, millisecondsdef format_number(self, input_str: str, length: int) -> str:if not input_str.isdigit():raise ValueError("輸入必須是數字字符串")if not isinstance(length, int) or length <= 0:raise ValueError("長度必須是正整數")if len(input_str) > length:return input_str[:length] # 截斷超長部分else:return '0' * (length - len(input_str)) + input_strdef format_copy(self, ref_time):match: re.Match = re.match("^(\d+)(\D)(\d+)(\D)(\d+)(\D)(\d+)$", ref_time)if not match:raise ValueError(f"無法復制的時間格式: {ref_time}")(h_str, sep1, m_str, sep2, s_str, sep3, ms_str) = match.groups()h_len, m_len, s_len, ms_len = map(len, (h_str, m_str, s_str, ms_str))def time_formater(hours: int, minutes: int, seconds: int, milliseconds: int) -> str:return (f"{self.format_number(str(hours), h_len)}{sep1}"f"{self.format_number(str(minutes), m_len)}{sep2}"f"{self.format_number(str(seconds), s_len)}{sep3}"f"{self.format_number(str(milliseconds), ms_len)}")return time_formaterclass SubtitleConverter(object):def ass_analyser(self, ass_file) -> list[dict]:ass_object = []with open(ass_file, "r", encoding="utf-8") as f:start_record = Falserecord_fields = Falsefor i in f:if i.strip() == "[Events]":record_fields = Truecontinueif re.match("\[.*?\]$", i.strip()):start_record = Falsecontinueif record_fields:record_fields = Falsestart_record = Truefields = [j.strip() for j in i.split(",")]continueif start_record:row = map(lambda j: j.strip(), i.split(",", maxsplit=len(fields) - 1))ass_object.append({k: v for k, v in zip(fields, row)})return ass_objectdef ass2srt(self, ass_file: str, srt_file: str, offset: float) -> None:if not ass_file.endswith(".ass"):returntime_analyser = TimeConverter()formatter = time_analyser.format_copy("00:00:00,000")srt_file_obj = open(srt_file, "w", encoding="utf-8")count = 1for row in self.ass_analyser(ass_file):if not re.match("標準", row["Style"]):continuestart_time = formatter(*time_analyser.time_analysis(row["Start"], offset))end_time = formatter(*time_analyser.time_analysis(row["End"], offset))subtitle = row["Text"]srt_file_obj.write(f"{count}\n{start_time} --> {end_time}\n{subtitle}\n\n")count += 1srt_file_obj.close()def ass2ass_eng(self, ass_file: str, output_file: str, remove_style='標準-Chi') -> None:"""這個方法是純AI寫的,與ass_analyser脫節。其作用是處理ass文件中的樣式,去除其中的描邊設置,同時移除掉指定字幕。字幕文件下載自網站:http://154.17.3.217:8888/sub/new, 原生字幕可能中英夾雜,但有時我們出于學習目的,可能只想要英文字幕;或純粹出于娛樂目的,只想保留中文字幕等;"""with open(ass_file, 'r', encoding='utf-8-sig') as f:lines = f.readlines()output = []current_section = Nonefor line in lines:line = line.rstrip('\r\n')stripped = line.strip()if stripped.startswith('[') and stripped.endswith(']'):current_section = strippedoutput.append(line)continueif current_section == '[V4+ Styles]':if line.startswith('Style:'):parts = line[len('Style:'):].split(',')if len(parts) >= 17:parts[16] = '0'output.append('Style:' + ','.join(parts))else:output.append(line)else:output.append(line)elif current_section == '[Events]' and line.startswith('Dialogue:'):content = line[len('Dialogue:'):].strip()fields = content.split(',', 9)if len(fields) >= 4 and fields[3].strip() == remove_style:continueoutput.append(line)else:output.append(line)with open(output_file, 'w', encoding='utf-8') as f:f.write('\n'.join(output))def ass2ass_offset(self, ass_file1, ass_file2, offset: float) -> None:time_analyser = TimeConverter()ref_time = "0:00:00.00"time_formatter = time_analyser.format_copy(ref_time)ass_object = self.ass_analyser(ass_file1)for row in ass_object:offset_start = time_analyser.time_analysis(row["Start"], offset)offset_end = time_analyser.time_analysis(row["End"], offset)row["Start"] = time_formatter(*offset_start)row["End"] = time_formatter(*offset_end)fields = ",".join(ass_object[0].keys())rows = "\n".join(",".join(row[k] for k in ass_object[0]) for row in ass_object)rows = rows.replace("\\", "\\\\")new_body = f"[Events]\n{fields}\n{rows}"pattern = r'\[Events\]\nFormat:.*(?:\nDialogue:.*)*'with open(ass_file1, "r", encoding="utf-8") as f:content = f.read()new_content = re.sub(pattern, new_body, content, re.MULTILINE)with open(ass_file2, "w", encoding="utf-8") as f:f.write(new_content)def srt2srt_offset(self, srt_file1, srt_file2, offset: float) -> None:time_analyser = TimeConverter()ref_time = "00:00:00,000"time_formatter = time_analyser.format_copy(ref_time)with open(srt_file1, "r", encoding="utf-8") as f1:f2 = open(srt_file2, "w", encoding="utf-8")for i in f1:res = re.match("^(.*?) --> (.*)$", i.strip())if res:start_time, end_time = res.groups()offset_start = time_analyser.time_analysis(start_time, offset)offset_end = time_analyser.time_analysis(end_time, offset)start_time = time_formatter(*offset_start)end_time = time_formatter(*offset_end)i = f"{start_time} --> {end_time}\n"f2.write(i)f2.close() class VedioConverter(object):def mkv2mp4(self, mkv_file: str, mp4_file: str) -> None:mkv_file, mp4_file = Utils.file_path_processor(mkv_file, mp4_file)command = [ffmpeg_file_path,'-i', Utils.file_path_processor(mkv_file),'-c:v', 'copy','-c:a', 'copy','-y',Utils.file_path_processor(mp4_file)]subprocess.run(command, check=True)def ass_embed_mp4(self, mp4_file: str, ass_file: str, output_file: str, itsoffset: float = 0.0) -> None:mp4_file, ass_file, output_file = Utils.file_path_processor(mp4_file, ass_file, output_file)converter = SubtitleConverter()random_name = f"{random.randint(0, 1000000)}.ass"converter.ass2ass_offset(ass_file, random_name, itsoffset)command = [ffmpeg_file_path,'-i', mp4_file,'-vf', f"subtitles={random_name}",'-c:v', 'libx264','-profile:v', 'high','-pix_fmt', 'yuv420p','-preset', 'fast','-b:a', '192k','-c:a', 'aac','-movflags', '+faststart','-y',output_file]subprocess.run(command, check=True)os.remove(random_name)def ass_embed_mkv(self, mkv_file: str, ass_file: str, output_file: str, itsoffset) -> None:mkv_file, ass_file, output_file = Utils.file_path_processor(mkv_file, ass_file, output_file)command = [ffmpeg_file_path,'-i', mkv_file,'-itsoffset', str(itsoffset),'-i', ass_file,'-map', '0','-map', '-0:s','-map', '1','-c', 'copy','-metadata:s:s:0', 'language=eng','-y',output_file]subprocess.run(command, check=True)ffmpeg_file_path = r"ffmpeg.exe"
vedio_converter = VedioConverter()
# 加入硬字幕(mp4)
vedio_converter.ass_embed_mp4(r"your_input_mp4_file.mp4",r"your_ass_subtitle_file.ass",r"your_output_mp4_file.mp4",itsoffset=6.3 # 指定的字幕偏移時間,讓字幕對齊音頻
)
為了方便字幕的視頻嵌入,上述代碼實現了一些較為重要的功能,部分如下:
①ass文件轉srt文件,見SubtitleConverter的ass2srt方法;
②根據srt文件和ass文件的時間偏移指定時間生成新的srt文件和ass文件的方法,見SubtitleConverter的ass2ass_offset和srt2srt_offset
③ass字幕文件嵌入mp4視頻的方法(可指定時間偏移),見VedioConverter的ass_embed_mp4方法,注意是硬字幕嵌入,相對耗時。
這些方法的實現都不是很難,不過并不能保證沒有Bug;經過簡單而基本測試,暫時沒有出現問題
在使用之前,請確保找到你的ffmpeg路徑。如果已經添加到環境變量,可以直接使用;否則請自行修改里面ffmpeg的路徑為絕對路徑。此處提供ffmpeg的下載路徑:
FFmpeg 最新 Windows 64 位 GPL 版本下載
關于為什么mp4視頻嵌入硬字幕,而mkv視頻卻是嵌入軟字幕(在代碼中),其實是有原因的:
mp4被更加廣泛的兼容,幾乎所有視頻播放器都可以正確播放。但如果是嵌入軟字幕,mp4視頻的字幕則不一定能被播放器支持。硬字幕嵌入能保證字幕一定顯示,更凸顯mp4兼容的優勢。缺陷就是硬字幕不能選擇隱藏,同時需要重新編碼,比較耗時;
mkv視頻的支持性就明顯要差不少,如手機上很多視頻播放器就對mkv視頻的支持不好,或視頻變形,或音頻丟失;但是軟字幕的顯示往往不是問題,這得益于mkv視頻是字幕的天然容器,所以只要能找到合適的mkv視頻播放器,幾乎就能正常的顯示軟字幕。