字幕分为两种 一种可以直接写在视频中,另一种制作srt,然后播放器就可以自动打开
from _chj.comm.pic import *
from _chj.base.sys import exec_cmd
from datetime import datetime, timedelta
for i, line in enumerate( tqdm(readlines("data/qa_v_subtitle/text.txt")) ):
fiv = f"data/qa_video/{i+1:04d}.mp4"
fov = f"data/qa_v_subtitle/{i+1:04d}.mp4"
fsub= f"data/qa_video/{i+1:04d}.srt"
cap = cv.VideoCapture(fiv)
tm = cap.get(7) / 25
td = timedelta( seconds=tm )
hour = td.seconds//3600
minute = (td.seconds//60)%60
second = td.seconds % 60
microsecond = td.microseconds
tp="{\\an8}"
ss = f"{hour:02d}:{minute:02d}:{second:02d},{microsecond//1000:03d}"
ss = f"1\n00:00:00,0 --> {ss}\n{tp}{line}\n"
with open(fsub, "w") as fp:
fp.write(ss)
cmd=f"ffmpeg -y -loglevel error -i {fiv} -vf subtitles={fsub} {fov}"
exec_cmd(cmd)
|