JavaScript音视频之通过输入的文字自动计算机读时长
作者:博客zhu虎康
这篇文章主要介绍了一个计算文本转语音时长的JavaScript函数,该函数支持中英文混合文本,通过区分中文(按字数)和英文(按单词数)分别计算时长,感兴趣的小伙伴可以了解下
需求
通过输入的文字,自动计算机读时长

分析
核心设计思路
- 区分中文(单音节字) 和英文(多音节词) 的语速逻辑(中文按 “字 / 分钟”,英文按 “词 / 分钟”);
- 提供默认语速(自然舒适的中速),支持自定义调整;
- 计算结果保留两位小数,符合实际使用场景;
- 兼容空字符串、全空格等边界情况。
- 时长 = 字符数 × (60 ÷ 语速值)(中文按字,英文按词)
- 预设了最贴合实际的语速值(中速 200 字 / 分钟),开箱即用
- 支持双语、自定义语速,适配不同 TTS 工具和业务场景
封装函数
/**
* method: calculateTtsDuration
* description: 计算文本转机读的时长
*/
/**
* 计算字符串机读时长(支持中文/英文,适配不同语速)
* @param {string} text - 要朗读的字符串
* @param {Object} [options] - 可选配置项
* @param {number} [options.speedType='normal'] - 语速类型:slow(慢速)/normal(中速)/fast(快速),也可传自定义语速值
* @param {Object} [options.speedConfig] - 自定义语速配置(字/分钟,中文)|(词/分钟,英文)
* @param {number} [options.speedConfig.zhSlow=135] - 中文慢速(默认135字/分钟)
* @param {number} [options.speedConfig.zhNormal=200] - 中文中速(默认200字/分钟,最舒适)
* @param {number} [options.speedConfig.zhFast=270] - 中文快速(默认270字/分钟)
* @param {number} [options.speedConfig.enSlow=120] - 英文慢速(默认120词/分钟)
* @param {number} [options.speedConfig.enNormal=150] - 英文中速(默认150词/分钟)
* @param {number} [options.speedConfig.enFast=180] - 英文快速(默认180词/分钟)
* @returns {number} 朗读时长(秒),保留两位小数
*/
export function calculateTtsDuration(text, options = {}) {
// 1. 处理边界:空字符串/全空格直接返回0
if (!text || text.trim() === '') {
return 0
}
// 2. 默认配置
const defaultConfig = {
speedType: 'normal',
speedConfig: {
zhSlow: 135, // 中文慢速:≈0.44秒/字(60/135)
zhNormal: 200, // 中文中速:≈0.3秒/字(60/200)
zhFast: 270, // 中文快速:≈0.22秒/字(60/270)
enSlow: 120, // 英文慢速:0.5秒/词
enNormal: 150, // 英文中速:0.4秒/词
enFast: 180 // 英文快速:≈0.33秒/词
}
}
// 合并配置
const config = { ...defaultConfig, ...options }
const { speedType, speedConfig } = config
// 3. 区分中文/英文,计算有效长度
// 中文:匹配中文字符(包括繁体),统计字数;非中文按英文单词统计
const zhCharRegex =
/[\u4e00-\u9fa5\u3400-\u4dbf\u{20000}-\u{2a6df}\u{2a700}-\u{2b73f}\u{2b740}-\u{2b81f}\u{2b820}-\u{2ceaf}\u{2ceb0}-\u{2ebef}\u{30000}-\u{3134f}]/gu
const zhChars = text.match(zhCharRegex) || [] // 中文字符数组
const zhLength = zhChars.length // 中文字数
// 英文:提取非中文字符,按空格分割成单词(过滤空字符串)
const nonZhText = text.replace(zhCharRegex, ' ').trim()
const enWords = nonZhText.split(/\s+/).filter((word) => word !== '') // 英文单词数组
const enLength = enWords.length // 英文单词数
// 4. 获取对应语速值
let zhSpeed, enSpeed
// 支持直接传自定义语速值(如传 250 表示中文250字/分钟)
if (typeof speedType === 'number') {
zhSpeed = speedType
enSpeed = speedType * 0.75 // 英文语速≈中文的75%(经验值)
} else {
zhSpeed =
speedConfig[`zh${speedType.charAt(0).toUpperCase() + speedType.slice(1)}`] ||
speedConfig.zhNormal
enSpeed =
speedConfig[`en${speedType.charAt(0).toUpperCase() + speedType.slice(1)}`] ||
speedConfig.enNormal
}
// 5. 计算总时长:中文时长 + 英文时长(秒)
const zhDuration = zhLength * (60 / zhSpeed) // 中文字数 × 单字时长
const enDuration = enLength * (60 / enSpeed) // 英文单词数 × 单词时长
const totalDuration = zhDuration + enDuration
// 6. 保留两位小数返回
return Number(totalDuration.toFixed(2))
}
使用示例
// // -------------------------- 使用示例 --------------------------
// // 示例1:纯中文,默认中速
// const text1 = '今天天气很好'
// console.log(`"${text1}" 朗读时长:${calculateTtsDuration(text1)} 秒`)
// // 输出:"今天天气很好" 朗读时长:1.5 秒(5字 × 0.3秒/字)
// // 示例2:纯中文,慢速
// console.log(`"${text1}" 慢速朗读时长:${calculateTtsDuration(text1, { speedType: 'slow' })} 秒`)
// // 输出:"今天天气很好" 慢速朗读时长:2.22 秒(5字 × 0.44秒/字)
// // 示例3:中英混合,快速
// const text2 = 'Hello 世界,今天吃了吗?'
// console.log(`"${text2}" 快速朗读时长:${calculateTtsDuration(text2, { speedType: 'fast' })} 秒`)
// // 中文:5字 × 0.22秒/字 = 1.1秒;英文:1词 × 0.33秒/词 = 0.33秒;总计≈1.43秒
// // 示例4:自定义语速(250字/分钟)
// console.log(`"${text1}" 自定义语速时长:${calculateTtsDuration(text1, { speedType: 250 })} 秒`)
// // 输出:"今天天气很好" 自定义语速时长:1.2 秒(5字 × 0.24秒/字)
// // 示例5:空字符串
// console.log(`空字符串时长:${calculateTtsDuration('')} 秒`) // 输出:0 秒
到此这篇关于JavaScript音视频之通过输入的文字自动计算机读时长的文章就介绍到这了,更多相关JavaScript计算文本转语音时长内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
