import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.R.integer;
import android.content.Context;
import android.util.Log;
/**
* class for read and parse srt subtitle
*
* This class is used to read and parse srt subtitle
*
*/
public class VNSubtitleSRT {
private static Matcher srtMatcher = null;
public VNSubtitleSRT() {
// do something
}
// --------- read srt file contents ----------
/**
* get subtitle srt file contents
* @param filename SRT filename
* @return SRT file contents
*/
public static String getSRTContents(String filename) {
StringBuilder text = new StringBuilder();
try{
//Get the text file
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append("\r\n");
}
br.close();
return text.toString();
}catch(Exception e){
e.printStackTrace();
}
return "";
}
// --------- parse srt contents ---------------
/**
* parse srt file contents to ArrayList
* @param contents srt file contents
* @return false:parse error, true:parse correct
*/
public static boolean parseSRTContents(String contents) {
String pat = "(\\d+)\r\n(\\d{2}\\:\\d{2}\\:\\d{2},\\d{3}) --\\> (\\d{2}\\:\\d{2}\\:\\d{2},\\d{3})\r\n([\\s\\S]*?\r\n\r\n)";
Pattern p = Pattern.compile(pat);
srtMatcher = p.matcher(contents);
if (srtMatcher.find(0)){
return true;
}
return false;
}
// --------- get subtilte from CurrentPosition -------------
public static String getSubtitle(int currentPosition){
String sequence = "";
int start = 0;
int end = 0;
String text = "";
if (null == srtMatcher){
return "";
}
if (!srtMatcher.find(0)){
return "not find matcher";
}
while(srtMatcher.find()){
sequence = srtMatcher.group(1);
start = timeToInt(srtMatcher.group(2));
end = timeToInt(srtMatcher.group(3));
text = srtMatcher.group(4);
if (currentPosition > start && currentPosition < end){
return text;
}
}
return "";
}
// --------- time to int ------------
private static int timeToInt(String ctime){
int hours = Integer.valueOf(ctime.substring(0, 2));
int mins = Integer.valueOf(ctime.substring(3, 5));
int secs = Integer.valueOf(ctime.substring(6, 8));
int msec = Integer.valueOf(ctime.substring(9, 12));
return (hours*60*60 + mins*60 + secs)*1000 + msec;
}
}
星期六, 4月 27, 2013
取得外掛字幕 srt 的內容
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言