您的位置:首页 > 其它

srt字幕解析

2011-08-05 09:33 176 查看
原版外语片要是没有中文字幕怎么办?

到射手网去下载字幕文件。常见的是SRT扩展名的字幕,暴风影音和超级解霸都能很好地加载。

如果找到的字幕不同步怎么办?

暴风影音里面有“同步”设置,看完后记得要设置再改回来,其它软件不详;

最彻底的办法就是改字幕。

SRT字幕的格式如下:

************************************

......

1027

01:54:13,332 --> 01:54:15,163

许个愿吧,小虎

1028

01:54:17,770 --> 01:54:19,601

一起回新疆!

************************************

程序流程:

1:从源文件中读出每一行

2:用正则表达式匹配时间描述行,如“01:54:13,332 --> 01:54:15,163”

相应的正则表达式为\d\d:\d\d:\d\d,\d\d\d --> \d\d:\d\d:\d\d,\d\d\d

3:通过格式转换和数学运算得到新的时间描述行

如如“01:54:13,332 --> 01:54:15,163” 延迟3秒后应转换为 “01:54:16,332 --> 01:54:18,163”

4:写到新文件

//使用正则表达式的是为了精确匹配,保证时间描述字符行在解析和运算过程中不会抛出异常

import java.io.*;
import java.util.regex.Pattern;

public class Main {

       public static void main(String[] args) {
           File file=new File("d:/whcl.srt");     //源字幕文件位置
           File file2=new File("d:/new.srt");     //新的字幕文件保存位置
           int timeError=3;       //在此设定需要调整的时间秒数,正数为延迟,负数为提早
        
           FileWriter fw=null;
           try {
               file2.createNewFile();     //创建新文件
               fw = new FileWriter(file2);        //创建文件输出流
           } catch (IOException ex) {
               ex.printStackTrace();
           }
           PrintWriter pw=new PrintWriter(fw); //包装文件输出流,方便整行写入
           try {
               FileReader fr=new FileReader(file); //创建文件输入流
               BufferedReader in=new BufferedReader(fr);      //包装文件输入流,方便整行读取
            
               String line;
               StringBuffer newLine=new StringBuffer();
            
               //正则表达式,用于匹配类似于“01:54:16,332 --> 01:54:18,163”的时间描述字符行
               String regex="\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d --> \\d\\d:\\d\\d:\\d\\d,\\d\\d\\d";
            
               //以下while循环逐行读取字幕源文件
               while((line=in.readLine()) != null) {

                   if (! Pattern.matches(regex,line)){     //使用静态方法进行正则式的匹配。
                       pw.println(line);      //如果读到的不是时间描述字符行,则原样写入新文件
                       continue;              //提早结束本次循环继续读取下一行
                   }
                
                   //以下对时间描述字符行进行格式转换和数学运算
                   int times;
                   int second=Integer.parseInt(line.substring(6,8));
                   int minute=Integer.parseInt(line.substring(3,5));
                   int hour=Integer.parseInt(line.substring(0,2));
                   times=timeError      + second + minute*60 + hour*3600;
                   String shour= "0" + (times/3600);
                   String sminute="0" + ((times % 3600)/60);
                   String ssecond="0" + (times % 60);
                   int second2=Integer.parseInt(line.substring(23,25));
                   int minute2=Integer.parseInt(line.substring(20,22));
                   int hour2=Integer.parseInt(line.substring(17,19));
                   times=timeError     + second2 + minute2*60 + hour2*3600;
                   String shour2= "0" + (times/3600);
                   String sminute2="0" + ((times % 3600)/60);
                   String ssecond2="0" + (times % 60);
                   newLine.setLength(0);
                   newLine.append(shour.substring(shour.length()-2) + ":") ;
                   newLine.append(sminute.substring(sminute.length()-2)+ ":");
                   newLine.append(ssecond.substring(ssecond.length()-2)     + ",");
                   newLine.append(line.substring(9,12) + " --> ");
                   newLine.append(shour2.substring(shour2.length()-2) + ":") ;
                   newLine.append(sminute2.substring(sminute2.length()-2)+ ":");
                   newLine.append(ssecond2.substring(ssecond2.length()-2)     + ",");
                   newLine.append(line.substring(26,29));
                
                   //最后把得到的时间描述字符行写入新文件
                   pw.println(newLine.toString());
                
               }
               pw.close();
           } catch (FileNotFoundException ex) {
               ex.printStackTrace();
           }catch (IOException ex) {
                   ex.printStackTrace();
           }
       }
    
}


稍作改进,本程序还可用于单字幕文件的分解和双字幕文件的合并

(只要加入对字幕序号行进行解析和运算即可)愿与有兴趣的朋友交流。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: