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

如何用find匹配待合并文件子串,实现FFmpeg批量合并TS文件

批量合并TS分片视频的Shell脚本问题

我是完全的新手,若问题过于浅显还请见谅。我尝试编写Shell脚本,借助ffmpeg concat协议批量合并分片视频文件,通过循环遍历文件并动态添加需合并的正确分片。

需要合并的分片文件示例:

  • titlea-00001234-01.ts
  • titlea-00001234-02.ts
  • titlea-00001234-03.ts
  • titleb-00001234-01.ts
  • titleb-00004321-02.ts
  • titleb-00004321-03.ts

合并后预期生成的文件:

  • titlea-00001234.mp4
  • titleb-00004321.mp4

手动执行的合并命令示例:

ffmpeg -i "concat:titlea-00001234-01.ts|titlea-00001234-02.ts|titlea-00001234-03.ts" -c copy titlea-00001234.mp4
ffmpeg -i "concat:titleb-00001234-01.ts|titleb-00001234-02.ts|titleb-00001234-03.ts" -c copy titleb-00001234.mp4

遇到的问题

我尝试用find命令在concat:后动态拼接对应分片,但编写的脚本报错“No such file or directory”,脚本内容如下:

#!/bin/bash
for i in /path/to/files/*.ts
    do
        if [[ "$i" =~ (-01) ]]
        then
            j="${i##*/}"
            k="${j%-0*}"
            ffmpeg -i `concat:( find /path/to/files/ -type f -name "{$k}*" -exec printf "%s\0" {} + )` -c copy /path/to/output/"{$k%.ts}.mp4"
        else
            echo "no files to process"
            exit 0
        fi
    done

补充说明

已找到完全符合需求的解决方案,感谢所有提供帮助的人。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 05:17:43