字幕文件srt格式解析

字幕文件srt格式解析解析srt文件,封装为list返回首先新建个class,表示单个字幕数据的实体类publicclassSrtEntity{/***字幕序号*/publicintnumber;/***开始时间*/publicStringbg;/***结束时间*/publicStringed;/***字幕内容*/publ

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

解析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;
    }
}

Jetbrains全家桶1年46,售后保障稳定

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);
    }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/200678.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Render,RenderChildren,RenderControl

    Render,RenderChildren,RenderControlprotectedinternalvirtualvoidRender(HtmlTextWriterwriter){this.RenderChildren(writer);}protectedinternalvirtualvoidRenderChildren(HtmlTextWriterwriter){ICollectionchildren=(th…

    2022年7月20日
    12
  • 软件安装管家:

    软件安装管家:

    2020年11月8日
    185
  • 数据结构线性表笔记_数据结构实验报告线性表

    数据结构线性表笔记_数据结构实验报告线性表1.线性表的定义若将线性表记为(a1,…,ai1,ai,ai+1,…,an),则表中ai1领先于ai,ai领先于ai+1,称ai1是ai的直接前驱元素,ai+1是ai的直接后继元素。

    2022年8月6日
    6
  • 查看linux执行的命令记录_history命令用法

    查看linux执行的命令记录_history命令用法前言我们每次敲打linux命令的时候,有时候想用之前用过的命令,一般情况下,我们都会按↑↓箭头来寻找历史的命令记录,那如果我想用1天前执行的某条命令,难道还要按↑100次?显示这样是不现实的,我们可

    2022年7月29日
    5
  • 电子签名的制作和使用方法_如何使用电子签名

    电子签名的制作和使用方法_如何使用电子签名电子签名的制作和使用一、在Word文档中插入手写签名生成电子签名1、准备好签名图片。2、用Word打开需要签名的文件,点菜单中的“插入>插图>图片”来选择已经写好的签名图片,插入后可以调整图片大小。3、点击菜单“格式>颜色>黑白”。4、点击菜单“格式>颜色>重新着色>设置透明色”,鼠标变成一支笔后,点图片中白色部分,这样就自动把白底抠除了,设置图片格式为“浮于文字上方”,最后拖动图片到签字处,调整大小即可。相关链接一、在Word文档中插入手写签名生成电子签名1、

    2022年10月16日
    2
  • js什么是匿名函数_js函数返回值

    js什么是匿名函数_js函数返回值js匿名函数的代码如下:(function(){ // 这里忽略jQuery 所有实现 })();半年前初次接触jQuery 的时候,我也像其他人一样很兴奋地想看看源码是什么样的。然而,在看到源码的第一眼,我就迷糊了。为什么只有一个匿名函数又没看到运行(当然是运行了…… ),就能有jQuery 这么个函数库了?于是,我抱着疑问来到CSDN 。结果相信现在很多人都很清楚了(因为在

    2022年9月27日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号