bash - 创建一个 convertAll() 函数来转换工作目录中的所有 .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.
推荐阅读
- regex - 如何通过正则表达式使用精确的 gmail 或 yahoo 字词验证电子邮件地址?
- flutter - 未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'text'
- cmd - 如何在 Windows 10 命令提示符下将 * 字符作为命令行参数传递?
- acumatica - 如何向移动应用程序添加新操作
- r - 将所有图像保存在 R 中
- c++ - 使用 WinApi 禁用\启用音频设备
- javascript - Tippyjs - 如何将单击按钮上的数据值传递给模板
- angular - Angular 语言服务不适用于使用 cli 生成的新组件的 vs 代码
- ios - 泛型类型“结果”专用于类型参数太少(得到 1,但预期为 2)- swift 单元测试中的错误
- string - 字符串突然变空颤动