自己喜歡收藏電影,有時網(wǎng)上能找到的中文字幕文件都不滿足自己電影版本。在自己下載的壓制版電影中已內(nèi)封非中文srt字幕時,可以選擇自己將srt的時間軸轉(zhuǎn)為ass并替換ass中的時間軸。自己在頻繁
復制粘貼改格式的時候想起可以用Python代碼完成轉(zhuǎn)換這一操作,借助ChatGPT并自己稍作修改后用代碼實現(xiàn)了。自己測試可用。
沒指定srt文件的路徑,使用前首先需要將srt后綴先改為txt文本格式,代碼默認輸入為“input.txt”,輸出“output.txt”。運行時待轉(zhuǎn)換的txt文件需要和py文件在同一目錄內(nèi)。
?
import re
def convert_timecode(line):
if "-->" in line:
# Split the line into two parts using the arrow
parts = line.strip().split(" --> ")
# Process each part separately
new_parts = []
for i, part in enumerate(parts):
if i == 0:
# For the first timecode, insert a comma between the first two characters
part = part[0] + "," + part[1:]
else:
# For the second timecode, remove the first character
part = part[1:]
# Remove the last digit from the milliseconds part
hours, minutes, seconds_milliseconds = part.split(":")
seconds, milliseconds = seconds_milliseconds.split(",")
milliseconds = milliseconds[:-1] # Remove the last digit
# Replace the last colon before the milliseconds part with a dot
new_part = f"{hours}:{minutes}:{seconds}.{milliseconds}"
new_parts.append(new_part)
# Join the parts back together using a comma
new_line = ",".join(new_parts)
return new_line + "\n"
else:
# If the line doesn't contain an arrow, return it unchanged
return line
# Replace 'input.txt' with the name of your input file, and 'output.txt' with the name of your output file.
with open('input.txt', 'r', encoding='utf-8') as infile, open('output.txt', 'w', encoding='utf-8') as outfile:
for line in infile:
outfile.write(convert_timecode(line))
不過還是需要手動對照翻譯復制粘貼進行調(diào)軸,就是比以前手動改時間軸格式方便了些。不知道有沒有一鍵將srt直接按照格式轉(zhuǎn)為ass的軟件,甚至實現(xiàn)普通字幕改特效字幕。
轉(zhuǎn)換前srt格式時間軸
轉(zhuǎn)換后ass格式時間軸文章來源:http://www.zghlxwxcb.cn/news/detail-817038.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-817038.html
到了這里,關于Python實現(xiàn)視頻字幕時間軸格式轉(zhuǎn)換的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!