当前位置: 首页 > news >正文

vue3+ts webVTT与JSON之前格式互转

vue


interface Cue {startTime: stringendTime: stringcontent: string// 可以根据需要添加其他属性,如样式、行号等
}
/** @desc WebVtt转 JSON */
export function parseWebVTT(vttContent: any): Cue[] {const cues: Cue[] = []if (!vttContent) {return []}const lines = vttContent.split(/\r?\n/)let currentCue: Cue | null = nulllet inCue = falsefor (const line of lines) {// 跳过空行和注释行if (line.trim() === '' || line.startsWith('NOTE')) {continue}// 检查是否是新 Cue 的开始if (line.startsWith('WEBVTT')) {continue // 忽略文件头部} else if (line.match(/^\d+:\d+:\d+\.\d+ --> \d+:\d+:\d+\.\d+$/)) {// 开始一个新的 Cueif (currentCue) {cues.push(currentCue)}const times = line.split(' --> ')currentCue = {startTime: parseTime(times[0]),endTime: parseTime(times[1]),content: ''}inCue = true} else if (inCue) {// 在 Cue 中,累加文本行if (line.trim() !== '') {currentCue.content += line.trim() + '\n' // 保留换行符} else {// 空行表示 Cue 结束inCue = falseif (currentCue) {cues.push(currentCue)currentCue = null}}}}// 处理最后一个 Cue(如果有的话)if (currentCue) {cues.push(currentCue)}return cues
}
function parseTime(timeString: string): any {const times = timeString.split('.')return times[0]
}
/** @desc json 转 WebVtt */
export function cuesToWebVTT(cues: Cue[]): string {let webvttContent = 'WEBVTT\n\n'cues.forEach((cue, index) => {// 将时间戳转换为 WebVTT 格式const startTime = cue.startTime + '.000'const endTime = cue.endTime + '.000'// 构造 WebVTT 文本行const cueTextLines = cue.content.split('\n').map((line) => `  ${line}`)const cueText = cueTextLines.join('\n')// 添加编号(可选,但通常 WebVTT 文件包含编号)webvttContent += `${index + 1}\n`// 添加时间戳和文本webvttContent += `${startTime} --> ${endTime}\n${cueText}\n\n`})return webvttContent
}

相关文章:

  • 前端预览pdf文件(后端返回pdf文件流)
  • Spring Cloud、Spring Cloud LoadBalancer、Nacos 和 OpenFeign整合
  • [ 网络通信基础 ]——网络的传输介质(双绞线,光纤,标准,线序)
  • hnust 湖南科技大学 2022 软件测试报告+代码
  • 宏集Panorama SCADA:个性化定制,满足多元角色需求
  • Apple开发者应用商店(AppStore)描述文件及ADHOC描述文件生成
  • 深拷贝、浅拷贝、引用拷贝
  • 台湾合泰原装BS66F360 封装LQFP-44 电容触摸按键 AD+LED增强型触控
  • 【Mac】Downie 4 for Mac(视频download工具)兼容14系统软件介绍及安装教程
  • 算法课程笔记——蓝桥第17次直播云课
  • window.clearInterval(timer) 清除定时器
  • React Native采集数据离线存储、网络状态监控、加密上传、鉴权
  • 4.通用编程概念
  • 4、js数据类型,检测数据类型
  • webrtc vp8/9视频编解码介绍
  • 〔开发系列〕一次关于小程序开发的深度总结
  • Brief introduction of how to 'Call, Apply and Bind'
  • ES6 学习笔记(一)let,const和解构赋值
  • GitUp, 你不可错过的秀外慧中的git工具
  • Java比较器对数组,集合排序
  • JAVA多线程机制解析-volatilesynchronized
  • PV统计优化设计
  • React系列之 Redux 架构模式
  • SSH 免密登录
  • Vue 重置组件到初始状态
  • 浮动相关
  • 聊聊directory traversal attack
  • 免费小说阅读小程序
  • 区块链技术特点之去中心化特性
  • 深度学习中的信息论知识详解
  • 线性表及其算法(java实现)
  • nb
  • 《码出高效》学习笔记与书中错误记录
  • # Redis 入门到精通(九)-- 主从复制(1)
  • ###STL(标准模板库)
  • (003)SlickEdit Unity的补全
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (2020)Java后端开发----(面试题和笔试题)
  • (3)选择元素——(17)练习(Exercises)
  • (zt)基于Facebook和Flash平台的应用架构解析
  • (回溯) LeetCode 77. 组合
  • (三)mysql_MYSQL(三)
  • (十一)图像的罗伯特梯度锐化
  • (游戏设计草稿) 《外卖员模拟器》 (3D 科幻 角色扮演 开放世界 AI VR)
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .net mvc部分视图
  • .Net 基于MiniExcel的导入功能接口示例
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:Bluetooth组件
  • .NETCORE 开发登录接口MFA谷歌多因子身份验证
  • .NET中的十进制浮点类型,徐汇区网站设计
  • .net中生成excel后调整宽度
  • @param注解什么意思_9000字,通俗易懂的讲解下Java注解
  • [ Linux Audio 篇 ] 音频开发入门基础知识
  • [ vulhub漏洞复现篇 ] struts2远程代码执行漏洞 S2-005 (CVE-2010-1870)