简体   繁体   English

在 bash 中跨多个文件夹递归运行 ffmpeg concat 脚本

[英]Recursively running ffmpeg concat script in bash across multiple folders

I wrote a bash script that concatenates all video files in a folder using ffmpeg.我编写了一个 bash 脚本,该脚本使用 ffmpeg 连接文件夹中的所有视频文件。 I would like to be able to run this script recursively on multiple folders.我希望能够在多个文件夹上递归地运行这个脚本。 My problem has been that I am unable to change into the directory of every new folder to run the script.我的问题是我无法切换到每个新文件夹的目录来运行脚本。 This is required for my script to work.这是我的脚本工作所必需的。 Does anyone know what I could accomplish this?有谁知道我能做到这一点?

    #!/bin/bash

for f in *; do echo "file '$f'" >> files.txt; done
for f in *; do echo "'$f'" >> filesdelete.txt; done
ffmpeg -f concat -safe 0 -i files.txt -c copy "${PWD##*/}".MP4
xargs -I{} rm -r "{}" < filesdelete.txt
rm files.txt
rm filesdelete.txt

I start with the file structure below.我从下面的文件结构开始。 The script runs in each subdirectory (dir1, dir2, dir3) and combines the files in each subdirectory into one video.该脚本在每个子目录(dir1、dir2、dir3)中运行,并将每个子目录中的文件组合成一个视频。 For the script to run, it needs to cd into each directory.要运行脚本,它需要 cd 进入每个目录。

root
├── dir1
│   ├── video1.mp4
│   ├── video2.mp4
│   └── video3.mp4
├── dir2
│   ├── video1.mp4
│   └── video2.mp4
└── dir3
    ├── video1.mp4
    └── video2.mp4

The end result should look like the structure below.最终结果应该类似于下面的结构。

root
├── dir1
│   └── concat.mp4
├── dir2
│   └── concat.mp4
└── dir3
    └── concat.mp4

Use Find.使用查找。

#!/bin/sh

cat << EOF >> edpop
1d
wq
EOF

find . -name "*.mp4" > stack

next () {
[[ -s stack ]] && main
rm -v ./stack
rm -v ./edpop
exit 0
}

main () {
file=$(echo "1p" | ed -s stack)
ffmpeg -f concat -safe 0 -i "${file}"
rm -v "${file}"
ed stack < edpop
next
}

next

You will need to partly edit this yourself.您需要自己进行部分编辑。 It isn't intended to be bulletproof, because I don't have ffmpeg, find isn't currently working on my system, and I also don't completely understand your ffmpeg command line.它不是防弹的,因为我没有 ffmpeg,find 目前无法在我的系统上运行,而且我也不完全理解您的 ffmpeg 命令行。 However, the basic point is that using Find will give you the full path of your files, and not just the basename, which you can operate on without having to change directories inside a running script.但是,基本的一点是,使用 Find 将为您提供文件的完整路径,而不仅仅是基本名称,您无需更改正在运行的脚本中的目录即可对其进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM