这里完全是初学者,如果这是显而易见的痛苦,我很抱歉。我正在尝试将 shell 脚本写入 ffmpeg concat protocol通过循环文件并动态添加要连接在一起的正确部分,将一堆分割的视频文件组合在一起。例如,转动这个:
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-03.ts|titleb-00001234-03.ts" -c copy titleb-00001234.mp4
但是我遇到的问题是使用 find 在 "concat:"
之后添加正确的部分。
这是我最好的尝试:
#!/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
但这会给出“没有这样的文件或目录”的错误。
编辑此解决方案非常适合我的需求 https://stackoverflow.com/a/75807616/21403800感谢@pjh 和其他人花时间提供帮助
最佳答案
一种可能是
#!/bin/bash
for file in *-[0-9][0-9].ts; do
prefix=${file%??????}
tgtfile=$prefix.mp4
[[ -e $tgtfile ]] && continue
printf -v filelst '%s|' "$prefix"-[0-9][0-9].ts
ffmpeg -i "concat:${filelst%?}" -c copy "$tgtfile"
done
关于bash - 如何使用find来匹配要连接的文件的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75806279/