如何通过命令行或批处理脚本与ffmpeg转换整个目录/文件夹?


当前回答

另一个简单的解决方案是使用xargs:

ls *。Avi | xargs -i -n1 ffmpeg -i {} "{}.mp4"

一个小缺陷是输出文件的尴尬命名(例如input.avi.mp4)。一个可能的解决方法是:

ls *。Avi | xargs -i -n1 bash -c "i={};Ffmpeg -i {} "\${i%.*}.mp4""

其他回答

我知道这可能是多余的,但我使用这个脚本批量转换文件。

old_extension=$1
new_extension=$2

for i in *."$old_extension";
  do ffmpeg -i "$i" "${i%.*}.$new_extension";
done

它需要2个参数使其更灵活:

要转换的扩展名 要转换的新扩展名

我为它创建了一个别名,但你也可以手动使用它,像这样:

sh batch_convert.sh mkv mp4

这将把所有的mkv文件转换为mp4文件。

正如你所看到的,它更多功能。只要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,这是行不通的

FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec mp3 "%~nG.mp3"

即使我做了双倍的%。

我甚至建议:

-acodec ***libmp3lame***

另外:

FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec libmp3lame "%~nG.mp3"

这是我用来批量转换avi到1280x mp4

FOR /F "tokens=*" %%G IN ('dir /b *.avi') DO "D:\Downloads\ffmpeg.exe" -hide_banner -i "%%G" -threads 8 -acodec mp3 -b:a 128k -ac 2 -strict -2 -c:v libx264 -crf 23 -filter:v "scale=1280:-2,unsharp=5:5:1.0:5:5:0.0" -sws_flags lanczos -b:v 1024k -profile:v main -preset medium -tune film -async 1 -vsync 1 "%%~nG.mp4"

作为一个cmd文件,运行它,循环找到该文件夹中的所有avi文件。

调用MY(更改为您的)ffmpeg,传递输入名称,设置为缩放锐化。我可能不需要CRF和“-b:v 1024k”…

输出文件是减去扩展名的输入文件,mp4作为新的ext。

前面的答案只会创建一个名为out.mov的输出文件。要为每一部老电影创建一个单独的输出文件,可以试试这个方法。

for i in *.avi;
  do name=`echo "$i" | cut -d'.' -f1`
  echo "$name"
  ffmpeg -i "$i" "${name}.mov"
done