2008-07-01
修改电影字幕时间轴实例代码
闲来无事,写了一段整体修改电影字幕时间轴的代码
如下:
如下:
Date time = null;
Matcher matcher = null;
String line = null;
String replaceTime = null;
String originalTime = null;
//时间格式
String timeFormat = "HH:mm:ss,SSS";
//正则时间格式
String timeFormatRegex = "\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d";
//读入字幕文件
BufferedReader input = new BufferedReader(new
InputStreamReader(new FileInputStream("D:/old.srt")));
//写入字幕文件
BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(new FileOutputStream("D:/new.srt")));
//日历
Calendar calendar = Calendar.getInstance();
//格式化器
DateFormat dateFormat = new SimpleDateFormat(timeFormat);
Pattern pattern = Pattern.compile(timeFormatRegex);
while((line = input.readLine())!=null)
{
matcher = pattern.matcher(line);
//找到匹配
while(matcher.find()){
originalTime = matcher.group();
time = dateFormat.parse(originalTime);
calendar.setTime(time);
//秒字段 减三
calendar.add(Calendar.SECOND, -3);
replaceTime = dateFormat.format(calendar.getTime());
//替换回原行
line = line.replaceAll(originalTime, replaceTime);
//清空
calendar.clear();
}
//写回
out.write(line);
out.write("\r\n");
}
//回收
input.close();
out.close();







评论排行榜