README.md (view raw)
1# videocr
2
3Extract hardcoded subtitles from videos using the [Tesseract](https://github.com/tesseract-ocr/tesseract) OCR engine with Python.
4
5Input video with hardcoded subtitles:
6
7<p float="left">
8 <img width="430" alt="screenshot" src="https://user-images.githubusercontent.com/10210967/56873658-3b76dd00-6a34-11e9-95c6-cd6edc721f58.png">
9 <img width="430" alt="screenshot" src="https://user-images.githubusercontent.com/10210967/56873659-3b76dd00-6a34-11e9-97aa-2c3e96fe3a97.png">
10</p>
11
12```python
13import videocr
14
15print(videocr.get_subtitles('video.avi', lang='HanS'))
16```
17
18Output:
19
20```
210
2200:00:00,000 --> 00:00:02,711
23-谢谢 … 你 好 -谢谢
24Thank you...Hi. Thanks.
25
261
2700:00:02,794 --> 00:00:04,879
28喝 点 什么 ?
29What can I get you?
30
312
3200:00:05,046 --> 00:00:12,554
33休闲 时 光 …
34For relaxing times, make it...
35
363
3700:00:12,804 --> 00:00:14,723
38三 得 利 时 光
39Bartender, Bob Suntory time.
40
414
4200:00:16,474 --> 00:00:19,144
43Un, I'll have a vodka tonic.
44
455
4600:00:19,394 --> 00:00:20,687
47谢谢
48Laughs Thanks.
49
50```
51
52## API
53
54```python
55videocr.get_subtitles(
56 video_path: str, lang='eng', time_start='0:00', time_end='',
57 conf_threshold=65, sim_threshold=90, use_fullframe=False)
58```
59Return the subtitles string in SRT format.
60
61
62```python
63
64videocr.save_subtitles_to_file(
65 video_path: str, file_path='subtitle.srt', lang='eng', time_start='0:00',
66 time_end='', conf_threshold=65, sim_threshold=90, use_fullframe=False)
67```
68Write subtitles to `file_path`. If the file does not exist, it will be created automatically.
69
70### Parameters
71
72- `lang`
73
74 Language of the subtitles in the video. Besides `eng` for English, all language codes on [this page](https://github.com/tesseract-ocr/tessdata_best/tree/master/script) are supported.
75
76- `time_start` and `time_end`
77
78 Extract subtitles from only a part of the video. The subtitle timestamps are still calculated according to the full video length.
79
80- `conf_threshold`
81
82 Confidence threshold for word predictions. Words with lower confidence than this threshold are discarded. The default value is fine for most cases.
83
84 Make it closer to 0 if you get too few words from the predictions, or make it closer to 100 if you get too many excess words.
85
86- `sim_threshold`
87
88 Similarity threshold for subtitle lines. Neighbouring subtitles with larger [Levenshtein](https://en.wikipedia.org/wiki/Levenshtein_distance) ratios than this threshold will be merged together. The default value is fine for most cases.
89
90 Make it closer to 0 if you get too many duplicated subtitle lines, or make it closer to 100 if you get too few subtitle lines.
91
92- `use_fullframe`
93
94 By default, only the bottom half of each frame is used for OCR. You can explicitly use the full frame if your subtitles are not within the bottom half of each frame.