Skip to content

Instantly share code, notes, and snippets.

@rhulha
Created May 17, 2016 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhulha/3017fc640fee4b06e45847aae07f3376 to your computer and use it in GitHub Desktop.
Save rhulha/3017fc640fee4b06e45847aae07f3376 to your computer and use it in GitHub Desktop.
package srt2vtt;
import java.io.*;
public class Srt2Vtt {
enum State { Number, TimeStamp, Text };
public void convert(Reader srt, Writer vtt) throws IOException {
BufferedReader br = new BufferedReader(srt);
String line;
vtt.write("WEBVTT\n\n");
State state=State.Number;
while((line = br.readLine())!=null){
switch(state) {
case Number:
state=State.TimeStamp;
break;
case TimeStamp:
vtt.write(line.replace(',', '.')+"\n");
state=State.Text;
break;
case Text:
vtt.write(line+"\n");
if( line.length()==0)
state=State.Number;
break;
}
}
vtt.close();
srt.close();
}
public static void main(String[] args) throws IOException {
Srt2Vtt s2v = new Srt2Vtt();
s2v.convert(new FileReader("test.srt"), new PrintWriter( System.out));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment