如何通过命令行或批处理脚本与ffmpeg转换整个目录/文件夹?
当前回答
小PHP脚本来做它:
#!/usr/bin/env php
<?php
declare(strict_types = 1);
if ($argc !== 2) {
fprintf ( STDERR, "usage: %s dir\n", $argv [0] );
die ( 1 );
}
$dir = rtrim ( $argv [1], DIRECTORY_SEPARATOR );
if (! is_readable ( $dir )) {
fprintf ( STDERR, "supplied path is not readable! (try running as an administrator?)" );
die(1);
}
if (! is_dir ( $dir )) {
fprintf ( STDERR, "supplied path is not a directory!" );
die(1);
}
$files = glob ( $dir . DIRECTORY_SEPARATOR . '*.avi' );
foreach ( $files as $file ) {
system ( "ffmpeg -i " . escapeshellarg ( $file ) . ' ' . escapeshellarg ( $file . '.mp4' ) );
}
其他回答
Windows:
这里我正在将所有(.mp4)文件转换为(.mp3)文件。 只需打开cmd,转到所需的文件夹并键入命令。 快捷方式:(可选) 1. 转到(.mp4)文件所在的文件夹 2. 按Shift键,点击左键,选择“在这里打开PowerShell窗口” 或“此处打开命令提示窗口” 3.输入“cmd”[注意:如果它直接打开cmd而不是PowerShell,则跳过此步骤] 4. 执行命令
for %i in (*.mp4) do ffmpeg -i "%i" "%~ni.mp3"
如果你想把它放到Windows 10的批处理文件中,你需要使用%%i。
下面的脚本在Windows上的Bash中工作得很好(所以它在Linux和Mac上也应该工作得很好)。它解决了我在其他解决方案中遇到的一些问题:
处理子文件夹中的文件 用目标扩展替换源扩展,而不仅仅是附加它 适用于名称中有多个空格和多个点的文件 (详见这个答案。) 可以运行时,目标文件存在,提示之前覆盖
ffmpeg-batch-convert.sh:
sourceExtension=$1 # e.g. "mp3"
targetExtension=$2 # e.g. "wav"
IFS=$'\n'; set -f
for sourceFile in $(find . -iname "*.$sourceExtension")
do
targetFile="${sourceFile%.*}.$targetExtension"
ffmpeg -i "$sourceFile" "$targetFile"
done
unset IFS; set +f
示例调用:
$ sh ffmpeg-batch-convert.sh mp3
作为奖励,如果你想删除源文件,你可以像这样修改脚本:
sourceExtension=$1 # e.g. "mp3"
targetExtension=$2 # e.g. "wav"
deleteSourceFile=$3 # "delete" or omitted
IFS=$'\n'; set -f
for sourceFile in $(find . -iname "*.$sourceExtension")
do
targetFile="${sourceFile%.*}.$targetExtension"
ffmpeg -i "$sourceFile" "$targetFile"
if [ "$deleteSourceFile" == "delete" ]; then
if [ -f "$targetFile" ]; then
rm "$sourceFile"
fi
fi
done
unset IFS; set +f
示例调用:
$ sh ffmpeg-batch-convert.sh mp3
当然,现在PowerShell出现了,专门设计来让这样的事情变得非常简单。
是的,除了Windows, PowerShell也可以在其他操作系统上使用,但它是预装在Windows上的,所以这应该对每个人都有用。
首先,你想要列出当前目录中的所有文件,所以,我们将从:
ls
如果你想递归地转换子目录中的所有文件,也可以使用ls -Recurse。
然后,我们将这些文件过滤下来,只有我们想转换的文件类型-例如。“avi”。
ls | Where { $_.Extension -eq ".avi" }
之后,我们将通过ForEach将该信息传递给FFmpeg。
对于FFmpeg的输入,我们将使用FullName——这是文件的整个路径。对于FFmpeg的输出,我们将使用Name -但是将结尾的.avi替换为.mp3。所以,它看起来是这样的:
$_.Name.Replace(".avi", ".mp3")
所以,让我们把所有这些放在一起,这是结果:
ls | Where { $_.Extension -eq ".avi" } | ForEach { ffmpeg -i $_.FullName $_.Name.Replace(".avi", ".mp3") }
这将改变所有人。”通过FFmpeg将avi"文件转换为".mp3"文件,只需替换引号中的三个内容来决定您想要的转换类型,并随时向FFmpeg中的ForEach中添加任何其他参数。
您还可以更进一步,在末尾添加Remove-Item以自动删除旧文件。
如果ffmpeg不在您的路径中,而实际上在您当前所在的目录中,请在那里写入./ffmpeg,而不仅仅是ffmpeg。
希望这对大家有所帮助。
小PHP脚本来做它:
#!/usr/bin/env php
<?php
declare(strict_types = 1);
if ($argc !== 2) {
fprintf ( STDERR, "usage: %s dir\n", $argv [0] );
die ( 1 );
}
$dir = rtrim ( $argv [1], DIRECTORY_SEPARATOR );
if (! is_readable ( $dir )) {
fprintf ( STDERR, "supplied path is not readable! (try running as an administrator?)" );
die(1);
}
if (! is_dir ( $dir )) {
fprintf ( STDERR, "supplied path is not a directory!" );
die(1);
}
$files = glob ( $dir . DIRECTORY_SEPARATOR . '*.avi' );
foreach ( $files as $file ) {
system ( "ffmpeg -i " . escapeshellarg ( $file ) . ' ' . escapeshellarg ( $file . '.mp4' ) );
}
我在linux中使用这一行程序将文件(通常是H265)转换为我可以在Kodi上毫无问题地播放的东西:
for f in *.mkv; do ffmpeg -i "$f" -c:v libx264 -crf 28 -c:a aac -b:a 128k output.mkv; mv -f output.mkv "$f"; done
这将转换为临时文件,然后替换原始文件,以便转换后名称保持不变。
推荐文章
- 使用ffmpeg添加文本字幕
- ffmpeg输出的含义(tbc, tbn, tbr)
- 最快的方法提取帧使用ffmpeg?
- 哪个HTTP状态代码表示“尚未准备好,稍后再试”?
- Android上的FFmpeg
- 实时http流到HTML5视频客户端的最佳方法
- 如何添加一个新的音频(不混合)到一个视频使用ffmpeg?
- ffmpeg、libav和avconv之间有什么不同和相似之处?
- Ffmpeg覆盖输出文件,如果存在
- 如何安装和运行phpize
- 使用ffmpeg将音频文件转换为mp3
- FFMPEG (libx264)高度不能被2整除
- 操作系统检测生成文件
- 如何创建一个视频从图像与FFmpeg?
- 如何用ffmpeg转换整个目录?