Python實現(xiàn)視頻字幕時間軸格式轉(zhuǎn)換的示例
自己喜歡收藏電影,有時網(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格式時間軸

到此這篇關(guān)于Python實現(xiàn)視頻字幕時間軸格式轉(zhuǎn)換的示例的文章就介紹到這了,更多相關(guān)Python視頻字幕時間軸轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一行代碼解決動態(tài)執(zhí)行Python函數(shù)方法實例
這篇文章主要為大家介紹了如何用一行代碼解決動態(tài)執(zhí)行Python函數(shù)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
如何使用PyTorch實現(xiàn)自由的數(shù)據(jù)讀取
這篇文章主要給大家介紹了關(guān)于如何使用PyTorch實現(xiàn)自由的數(shù)據(jù)讀取的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03
python pandas中對Series數(shù)據(jù)進行軸向連接的實例
今天小編就為大家分享一篇python pandas中對Series數(shù)據(jù)進行軸向連接的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06

