File size: 4,836 Bytes
f1a1a92 5616b5a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | import cv2
import easyocr
import pysrt
import numpy as np
from tqdm import tqdm
from dataclasses import dataclass
# =============================
# Language Priority Mapping
# =============================
OCR_LANG_MAP = {
"english": ["en"],
"japanese": ["ja"],
"korean": ["ko"],
"chinese_simplified": ["ch_sim", "en"],
"chinese_traditional": ["ch_tra", "en"],
"thai": ["th"]
}
# =============================
# Data Models
# =============================
@dataclass
class SubtitleBox:
x: int
y: int
width: int
height: int
@dataclass
class SubtitleLine:
text: str
start: float
end: float
status: str # entered | active | exited
# =============================
# Video Reader (Frame Accurate)
# =============================
class VideoReader:
def __init__(self, path):
self.cap = cv2.VideoCapture(path)
if not self.cap.isOpened():
raise Exception("Cannot open video file")
self.fps = self.cap.get(cv2.CAP_PROP_FPS)
self.total_frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
def time_to_frame(self, t):
return int(t * self.fps)
def get_frame(self, index):
self.cap.set(cv2.CAP_PROP_POS_FRAMES, index)
ret, frame = self.cap.read()
return frame if ret else None
# =============================
# OCR Engine
# =============================
class SubtitleOCR:
def __init__(self, language_key):
langs = OCR_LANG_MAP[language_key]
self.reader = easyocr.Reader(langs, gpu=False)
def recognize(self, image):
result = self.reader.readtext(image, detail=0)
return " ".join(result).strip()
# =============================
# Subtitle Tracker
# =============================
class SubtitleTracker:
def __init__(self):
self.last_text = ""
self.start_time = None
self.subtitles = []
def update(self, text, current_time):
if text != self.last_text:
if self.last_text.strip() != "":
self.subtitles.append(
SubtitleLine(
text=self.last_text,
start=self.start_time,
end=current_time,
status="exited"
)
)
self.last_text = text
self.start_time = current_time
return "entered"
return "active"
def finalize(self, end_time):
if self.last_text.strip() != "":
self.subtitles.append(
SubtitleLine(
text=self.last_text,
start=self.start_time,
end=end_time,
status="exited"
)
)
# =============================
# Crop Subtitle Region
# =============================
def crop_subtitle(frame, box: SubtitleBox):
return frame[
box.y : box.y + box.height,
box.x : box.x + box.width
]
# =============================
# Main OCR Process
# =============================
def process_video(
video_path,
subtitle_box,
start_time,
end_time,
language_key
):
video = VideoReader(video_path)
ocr = SubtitleOCR(language_key)
tracker = SubtitleTracker()
start_frame = video.time_to_frame(start_time)
end_frame = video.time_to_frame(end_time)
for frame_idx in tqdm(range(start_frame, end_frame)):
frame = video.get_frame(frame_idx)
if frame is None:
continue
crop = crop_subtitle(frame, subtitle_box)
text = ocr.recognize(crop)
current_time = frame_idx / video.fps
tracker.update(text, current_time)
tracker.finalize(end_time)
return tracker.subtitles
# =============================
# Export SRT
# =============================
def export_srt(subtitles, output_path):
srt = pysrt.SubRipFile()
for i, sub in enumerate(subtitles, start=1):
srt.append(
pysrt.SubRipItem(
index=i,
start=pysrt.SubRipTime(seconds=sub.start),
end=pysrt.SubRipTime(seconds=sub.end),
text=sub.text
)
)
srt.save(output_path, encoding="utf-8")
# =============================
# Example Run
# =============================
if __name__ == "__main__":
video_path = "input.mp4"
subtitle_box = SubtitleBox(
x=200, # Horizontal Position
y=800, # Vertical Position
width=1500,
height=200
)
start_time = 10.0
end_time = 120.0
language = "chinese_simplified"
subtitles = process_video(
video_path,
subtitle_box,
start_time,
end_time,
language
)
export_srt(subtitles, "output.srt")
print("OCR Finished → output.srt") |