Python實(shí)現(xiàn)視頻字幕時(shí)間軸格式轉(zhuǎn)換的示例
自己喜歡收藏電影,有時(shí)網(wǎng)上能找到的中文字幕文件都不滿足自己電影版本。在自己下載的壓制版電影中已內(nèi)封非中文srt字幕時(shí),可以選擇自己將srt的時(shí)間軸轉(zhuǎn)為ass并替換ass中的時(shí)間軸。自己在頻繁復(fù)制粘貼改格式的時(shí)候想起可以用Python代碼完成轉(zhuǎn)換這一操作,借助ChatGPT并自己稍作修改后用代碼實(shí)現(xiàn)了。自己測(cè)試可用。
沒(méi)指定srt文件的路徑,使用前首先需要將srt后綴先改為txt文本格式,代碼默認(rèn)輸入為“input.txt”,輸出“output.txt”。運(yùn)行時(shí)待轉(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))不過(guò)還是需要手動(dòng)對(duì)照翻譯復(fù)制粘貼進(jìn)行調(diào)軸,就是比以前手動(dòng)改時(shí)間軸格式方便了些。不知道有沒(méi)有一鍵將srt直接按照格式轉(zhuǎn)為ass的軟件,甚至實(shí)現(xiàn)普通字幕改特效字幕。
轉(zhuǎn)換前srt格式時(shí)間軸

轉(zhuǎn)換后ass格式時(shí)間軸

到此這篇關(guān)于Python實(shí)現(xiàn)視頻字幕時(shí)間軸格式轉(zhuǎn)換的示例的文章就介紹到這了,更多相關(guān)Python視頻字幕時(shí)間軸轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+Turtle繪制一個(gè)可愛(ài)的生日蛋糕
每當(dāng)有朋友過(guò)生日時(shí),生日蛋糕自然是必不可少的。本文將利用Python中的turtle、math和random繪制一個(gè)可愛(ài)的生日蛋糕,需要的可以參考一下2022-05-05
一行代碼解決動(dòng)態(tài)執(zhí)行Python函數(shù)方法實(shí)例
這篇文章主要為大家介紹了如何用一行代碼解決動(dòng)態(tài)執(zhí)行Python函數(shù)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Python?gRPC流式通信協(xié)議詳細(xì)講解
這篇文章主要介紹了Python?gRPC流式通信協(xié)議,最近幾天在搞golang的grpc,跑通之后想用php作為客戶端調(diào)用一下grpc服務(wù),結(jié)果拉了,一個(gè)php的grpc服務(wù)安裝,搞了好幾天,總算搞定了2022-11-11
如何使用PyTorch實(shí)現(xiàn)自由的數(shù)據(jù)讀取
這篇文章主要給大家介紹了關(guān)于如何使用PyTorch實(shí)現(xiàn)自由的數(shù)據(jù)讀取的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
python pandas中對(duì)Series數(shù)據(jù)進(jìn)行軸向連接的實(shí)例
今天小編就為大家分享一篇python pandas中對(duì)Series數(shù)據(jù)進(jìn)行軸向連接的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06

