闲来无事,写了一段整体修改电影字幕时间轴的代码
如下:

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();
评论
jlusdy 2008-07-04   回复
对呀,有些字幕比较难找

时间轴对不上,本来可以在播放器里面调整的
但每次都调整还是很麻烦
所以干脆把字幕文件修正了吧
抛出异常的爱 2008-07-02   回复
正是我需要的。
有的字幕实在是太难找了。
发表评论

您还没有登录,请登录后发表评论

jlusdy
搜索本博客
博客分类
最近加入圈子
存档
最新评论