搬运视频 srt 字幕转为符合 B站 srt 格式

搬运视频 srt 字幕转为符合 B站 srt 格式

1. 差异

# 默认格式:秒与毫秒间隔为:.
# B站格式:秒与毫秒间隔为:,
00:17:05.102 --> 00:17:08,102

2. 正则表达式

替换

public StringBuilder replace(int startIndex, int endIndex, String str)
//  1. startIndex是替换的起始索引位置(包括该位置)
//  2. endIndex是替换的结束索引位置(不包括该位置)
//  3. str 用于替换的字符串

正则表达式

 String reg = "\\d+:\\d+\\d+\\.\\d+";
        StringBuilder sb = new StringBuilder(ss);
        Matcher matcher = Pattern.compile(reg).matcher(ss);
        while (matcher.find()){
            int start = matcher.start();  //  每个符合条件的出现的位置
            int index = matcher.group().indexOf(".");
            sb.replace(start + index, start + index + 1, ",");
        }
        System.out.println(sb);

3. 输入输出流

package com;

public class trans {
    static FileReader fileReader = null;
    static FileWriter fileWriter = null;
    static int readLen = 0;
    static char[] buf = new char[8];
    static String infilePath = "src/com/resource/[中字]The Impossible 1v2 that Gumayusi Outplayed (1).srt";
    static String outfilePath = "src/com/resource/";
    static StringBuilder sb = new StringBuilder();

    public static void main(String[] args) {

        //  1.  创建FileReader 对象
        try {
            fileReader = new FileReader(infilePath);
            //  循环读取 使用 read(buf),返回的是实际读取到的字符数
            //  如果返回 -1,说明到文件结束,
            while ((readLen = fileReader.read(buf)) != -1) {
               sb.append(new String(buf, 0, readLen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //  2. 创建 FileWriter 对象
        outfilePath = outfilePath + System.currentTimeMillis() + ".srt";
        System.out.println(outfilePath);

        try {
            fileWriter = new FileWriter(outfilePath);
            fileWriter.write(getResult());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //  对于 FileWriter,一定要关闭流。或者 flush 才能真正地把数据写入到文件
            //  后面看源码就知道为什么了
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
   //  3. 正则表达式
    public static String getResult(){
        String reg = "\\d+:\\d+\\d+\\.\\d+";
        Matcher matcher = Pattern.compile(reg).matcher(sb.toString());
        while (matcher.find()){
            int start = matcher.start();  //  每个符合条件的出现的位置
            int index = matcher.group().indexOf(".");
            sb.replace(start + index, start + index + 1, ",");
        }
        return sb.toString();
    }
}

4. 测试

起初为:.


image.png

经过程序处理:


image.png

修改后:
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容