JS读取字幕srt文件内容

首先上传srt字幕文件并转化一个blob链接

//上传字幕
uploadSrt(file) {
    console.log(file)
    let srtUrl = URL.createObjectURL(file)
    console.log(srtUrl)
    this.GetSrtInfo(srtUrl)
},
//获取字幕内容,发送一个get请求
GetSrtInfo(srtUrl) {
   let that = this;
   axios
      .get(`${srtUrl}`)
        .then(function(response) {
          let textList = response.data
            .split(/\n\s\n/)
            .filter(item => item != "")
            .map((item, index) => {
              let textItem = item.split(/\n/);
              return {
                index: index,
                sort: textItem[0],
                text: textItem[2],
                startTime: that.ToSeconds(textItem[1].split(" --> ")[0]),
                endTime: that.ToSeconds(textItem[1].split(" --> ")[1]),
                timeLine: textItem[1],
              };
            });
          that.srtInfoList = textList;
          console.log("解析之后的字幕内容", that.srtInfoList);
        })
        .catch(function(error) {
          console.log(error);
        });
},
//将时间转化为秒
    ToSeconds(t) {
      var s = 0.0;
      if (t) {
        var p = t.split(":");
        for (let i = 0; i < p.length; i++) {
          s = s * 60 + parseFloat(p[i].replace(",", "."));
        }
      }
      return s;
    },

你可能感兴趣的:(js)