Python實現視頻字幕時間軸格式轉換的示例
自己喜歡收藏電影,有時網上能找到的中文字幕文件都不滿足自己電影版本。在自己下載的壓制版電影中已內封非中文srt字幕時,可以選擇自己將srt的時間軸轉為ass并替換ass中的時間軸。自己在頻繁復制粘貼改格式的時候想起可以用Python代碼完成轉換這一操作,借助ChatGPT并自己稍作修改后用代碼實現了。自己測試可用。
沒指定srt文件的路徑,使用前首先需要將srt后綴先改為txt文本格式,代碼默認輸入為“input.txt”,輸出“output.txt”。運行時待轉換的txt文件需要和py文件在同一目錄內。
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))
不過還是需要手動對照翻譯復制粘貼進行調軸,就是比以前手動改時間軸格式方便了些。不知道有沒有一鍵將srt直接按照格式轉為ass的軟件,甚至實現普通字幕改特效字幕。
轉換前srt格式時間軸
轉換后ass格式時間軸
到此這篇關于Python實現視頻字幕時間軸格式轉換的示例的文章就介紹到這了,更多相關Python視頻字幕時間軸轉換內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
一行代碼解決動態(tài)執(zhí)行Python函數方法實例
這篇文章主要為大家介紹了如何用一行代碼解決動態(tài)執(zhí)行Python函數方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12python pandas中對Series數據進行軸向連接的實例
今天小編就為大家分享一篇python pandas中對Series數據進行軸向連接的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06