You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

排查Python音频分章脚本运行时的报错问题

问题排查:chapterize脚本执行报错

错误根源

1. Shell语法兼容性问题

脚本中使用的|&是Bash专属语法,作用是同时将标准输出和标准错误重定向到下一个管道。但os.popen()默认调用的是/bin/sh(POSIX标准Shell),不支持|&语法,因此触发了语法错误。

2. 空字符串转浮点型失败

由于上述语法错误导致sox命令执行失败,返回的内容为空字符串,后续执行float()转换时自然抛出ValueError。

修复方案

将脚本中的|&替换为POSIX标准的2>&1(将标准错误重定向到标准输出),修改后的代码行如下:

time += float(os.popen('sox \"' + i + '\" -n stat 2>&1 | head -2 | tail -1 | grep -o \"[0-9.]\\+\"').read().strip())*1e9

额外优化建议

建议替换掉不安全的os.popen(),改用subprocess模块,它能更安全地处理命令执行,还能避免文件名含特殊字符时的风险,示例代码如下:

import subprocess

for i in tqdm.tqdm(inputAudioFiles):
    result = subprocess.run(
        ['sox', i, '-n', 'stat'],
        stderr=subprocess.PIPE,
        text=True
    )
    # 提取音频时长
    duration_line = result.stderr.splitlines()[1]
    duration = float(duration_line.split()[2])
    time += duration * 1e9
    starttimes.append([i, str(int(time))])

内容的提问来源于stack exchange,提问作者Verity

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 22:43:26