-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubtitles.java
More file actions
158 lines (141 loc) · 4.55 KB
/
Copy pathSubtitles.java
File metadata and controls
158 lines (141 loc) · 4.55 KB
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
package media;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import javafx.scene.control.Label;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
/**
* Class for the audio handler
* @author - Rimas Radziunas and Cezara-Lidia Jalba
* @version - 1.1
* @date - 21/05/20
*/
public class Subtitles {
BufferedReader reader;
// store the subtitle increments to be displayed
ArrayList<SubtitleBlock> subtitleList = new ArrayList<SubtitleBlock>();
// track subtitles
private int currentSubBlock = 0;
String displayString = "";
// indicate whether to advance to the next subtitle block
Boolean advance = false;
// indicate whether to change the subtitle label
Boolean changeSubtitle = true;
// gap between the current the next subtitle block
double gapStart;
double gapEnd;
// read srt file, create subtitle block, store in the list
public Subtitles(File subFile) throws IOException {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(subFile), "UTF-8"));
String line = "";
//read until the end of the file
while(line != null) {
// first read of the number
if(reader.readLine() == null) {
break;
}
SubtitleBlock subBlock = new SubtitleBlock();
// second is the timing information
line = reader.readLine();
subBlock.setTimeFrame(line);
String displayString = "";
//read subsequent lines which is the text
while(!((line = reader.readLine()).isEmpty())) {
displayString = displayString + line + "\n";
}
subBlock.setText(displayString);
subtitleList.add(subBlock);
}
}
public double getStartTimeOfText() {
return subtitleList.get(currentSubBlock).getStartTime();
}
public double getEndTimeOfText() {
return subtitleList.get(currentSubBlock).getEndTime();
}
// seeks the correct subtitle time frame with for the current time of the video
public void seekPosition(Double currentTime) {
// if the subtitle is ahead of the current time, cycle back till correct one is reached
if(currentTime < getStartTimeOfText()) {
while(currentTime < getStartTimeOfText()) {
setGapToNextSubtitle();
if(currentTime > gapStart && currentTime < gapEnd || currentSubBlock == 0) {
break;
}
reverseSubTrack();
}
// if the subtitle is behind the current time, cycle forwards till correct one is reached
} else if(currentTime > getEndTimeOfText()) {
while(currentTime > getStartTimeOfText()) {
fowardSubTrack();
setGapToNextSubtitle();
if(currentTime > gapStart && currentTime < gapEnd) {
break;
}
fowardSubTrack();
}
}
}
public String getCurrentText() {
return subtitleList.get(currentSubBlock).getText();
}
public void fowardSubTrack() {
currentSubBlock++;
if(currentSubBlock > subtitleList.size()-1) {
currentSubBlock--;
}
}
public void reverseSubTrack() {
currentSubBlock--;
if(currentSubBlock < 0) {
currentSubBlock++;
}
}
// set the subtitle text
public void setSubtitleText(Label label, MediaPlayer mp) {
double currentTime = mp.getCurrentTime().toMillis();
// when the current time falls into current subtitle time frame, set opacity to 1 display it
// and set the text, stop the change of subtitle text and opacity, enable advancing to the next subTitle
if(currentTime > getStartTimeOfText() && currentTime < getEndTimeOfText()) {
if(changeSubtitle) {
label.setOpacity(1);
label.setText(getCurrentText());
changeSubtitle = false;
}
advance = true;
}
// when the current time leave the time frame, set label opacity to 0, if advance is true
// jump to the next subtitle, stop advancement and enable the change of the label
else if(currentTime > getEndTimeOfText() || currentTime < getStartTimeOfText()) {
label.setOpacity(0);
if(advance) {
fowardSubTrack();
advance = false;
changeSubtitle = true;
}
}
}
public void setGapToNextSubtitle() {
gapStart = (int) subtitleList.get(currentSubBlock).getEndTime();
gapEnd = (int) subtitleList.get(currentSubBlock + 1).getStartTime();
}
public String formatTime(double time) {
String string;
Duration timeD = new Duration(time);
int min = (int) timeD.toMinutes();
int sec = (int) (timeD.toSeconds() - 60 * min);
int mili = (int) (timeD.toMillis() - 1000 * sec);
if (sec < 10) {
string = min + ":0" + sec + ":" + mili;
} else {
string = min + ":" + sec + ":" + mili;
}
return string;
}
}