gpt4 book ai didi

Shell 脚本 - 遍历目录?

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:07 24 4
gpt4 key购买 nike

如果我想遍历嵌套目录列表并运行一组命令,我该怎么做?

我的目录结构是这样的:

  • 视频
    • 文件夹1-> VTS_01_1.mp4
    • 文件夹2-> VTS_01_1.mp4
    • 文件夹3-> VTS_01_1.mp4....等等

我需要遍历每个文件夹并运行下面的脚本。所有 .mp4 文件都被命名为 VTS_01_1.mp4,但我想使用 *.mp4 通配符条件来完成它,以防它们不是.每个目录中的输出文件应为“VTS_01_h264.mp4”。想法?我正在使用 CentOS 6.4。

ffmpeg -y -i "VTS_01_1.mp4" -an -pass 1 -threads 2 -vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256 \
-partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \
-me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \
-flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \
-g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10\
-qmax 51 -qdiff 4 "video_tmp.mp4"



ffmpeg -y -i "VTS_01_1.mp4" -acodec libfaac -ar 44100 -ab 96k -pass 2 -threads 2 -vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256 \
-partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \
-me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \
-flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \
-g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10\
-qmax 51 -qdiff 4 "video_tmp.mp4"



qt-faststart "video_tmp.mp4" "VTS_01_h264.mp4"

最佳答案

find 命令在这些事情上非常强大,试试:

find videos/ -name "*.mp4" -exec ffmpegScript {} \;

这会找到所有以 .mp4 结尾的文件(也在子目录中)并执行 ffmpegScript nameOfMp4File,其中 nameOfMp4File 是找到的文件,一次一个。 find 负责循环本身。

现在我们需要定义ffmpegScript:

#!/usr/bin/env bash

inputFile="$1"
outputFile="$(dirname $1)"/VTS_01_h265.mp4

ffmpeg -y -i "$inputFile" -acodec libfaac -ar 44100 -ab 96k -pass 2 -threads 2 -vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256 \
-partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 \
-me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 \
-flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 \
-g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10\
-qmax 51 -qdiff 4 "video_tmp.mp4"

qt-faststart "video_tmp.mp4" "$outputFile"

inputFile 变量设置为传递给 ffmpegscript 的第一个位置参数,outputFile 变量设置为相同的路径,但不同基本名称

注意:如果目录中有多个 *.mp4 文件,此脚本将覆盖您的输出文件。另外,我没有尝试整个脚本,因为这里没有任何可用的 *.mp4 文件。

关于Shell 脚本 - 遍历目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20148727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com