字幕文件srt格式解析

解析srt文件,封装为list返回

  1. 首先新建个class,表示单个字幕数据的实体类
public class SrtEntity {
    /**
     * 字幕序号
     */
    public int number;
    /**
     * 开始时间
     */
    public String bg;
    /**
     * 结束时间
     */
    public String ed;
    /**
     * 字幕内容
     */
    public String content;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getBg() {
        return bg;
    }

    public void setBg(String bg) {
        this.bg = bg;
    }

    public String getEd() {
        return ed;
    }

    public void setEd(String ed) {
        this.ed = ed;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

2.编写srt转list的方法

	/**
     * 解析srt文件,封装为list并返回
     * @param srtPath
     */
    public List<SrtEntity> getSrtInfoList(String srtPath){

        List<SrtEntity> srtList = new ArrayList<>();

        try {

            InputStreamReader read = new InputStreamReader(new FileInputStream(srtPath), "utf-8");

            BufferedReader bufferedReader = new BufferedReader(read);

            String lineTxt;

            int index = 0;

            SrtEntity entity = new SrtEntity();

            while ((lineTxt = bufferedReader.readLine()) != null){
                index ++;
                switch (index % 4){
                    case 1 :
                        entity.setNumber(Integer.parseInt(lineTxt));
                        break;
                    case 2 :
                        String[] timeArray = lineTxt.split(" --> ");
                        entity.setBg(timeArray[0]);
                        entity.setEd(timeArray[1]);
                        break;
                    case 3 :
                        entity.setContent(lineTxt);
                        break;
                    case 0 :
                        srtList.add(entity);
                        entity = new SrtEntity();
                        break;
                }
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return srtList;
    }

3.测试方法

public static void main(String[] args) {

        long begin = System.currentTimeMillis();

        FileOperateServiceImpl impl = new FileOperateServiceImpl();
        // 解析srt获取list
        List<SrtEntity> list = impl.getSrtInfoList("E:\\111.srt");
        long end = System.currentTimeMillis();
        System.out.println(end - begin);
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值