创建一个转换All()函数,用于转换工作目录中的所有.filetype



我似乎不知道正确的bash语法;尽管如此,我还是尝试创建一个工具来更改工作目录中ffmpeg接受的文件类型的所有文件的尺寸,并将其转换为另一种ffmpeg接受的文件类型。在这种情况下,此工具会将超过 1080x720 的所有.webm文件转换为 1080x-1 或 -1x720 .mp4文件。如果.webm文件小于 1080x720,则新的.mp4文件将具有相同的尺寸。

但是,该工具中有一个扳手。

convertAll () {
local wantedWidth = 1080
local wantedHeight = 720
for i in *.webm; do
local newWidth = $i.width
local newHeight = $i.height
until [$newWidth <= $wantedWidth && $newHeight <= $wantedHeight]; do
if [$videoWidth > $wantedWidth]; then
newHeight = $newWidth*($wantedWidth/$newWidth)
newWidth = $newWidth*($wantedWidth/$newWidth)
fi
if [$videoHeight > $wantedHeight]; then
newWidth = $newWidth*($wantedHeight/$newHeight)
newHeight = $newHeight*($wantedHeight/$newHeight)
fi
done
ffmpeg -i "$i" -vf scale=$newWidth:$newHeight "${i%.*}.mp4";
done
echo "All files have been converted."
}

这将返回一堆如下所示的行:

bash: [: missing ']'
bash: [: missing ']'
bash: =: No such file or directory

我最好的猜测是 BASH 不会做数学,而且我声明和编辑变量不正确。

我想对此提出一些意见---因为我缺乏经验真的让我来到这里。

最重要的部分是检测输入视频的尺寸并根据情况计算所需的尺寸。 ffmpeg为您做到。
请尝试:

covertAll() {
    for i in *.webm; do
        ffmpeg -i "$i" -vf "scale='min(1080,iw)':'min(720,ih)':force_original_aspect_ratio=decrease" "${i%.*}.mp4"
    done
}

它将按如下方式缩放维度(示例):

1080x720 => 1080x720
1440x720 => 1080x540
720x1080 =>  480x720
720x480  =>  720x480
etc.

最新更新