How do you convert an entire directory/folder with ffmpeg via command line or with a batch script?
This will create mp4 video from all the jpg files from current directory.
echo exec("ffmpeg -framerate 1/5 -i photo%d.jpg -r 25 -pix_fmt yuv420p output.mp4");
Getting a bit like code golf here, but since nearly all the answers so far are bash (barring one lonely cmd one), here's a windows cross-platform command that uses powershell (because awesome):
ls *.avi|%{ ffmpeg -i $_ <ffmpeg options here> $_.name.replace($_.extension, ".mp4")}
You can change *.avi to whatever matches your source footage.
For Windows:
Here I'm Converting all the (.mp4) files to (.mp3) files.
Just open cmd, goto the desired folder and type the command.
Shortcut: (optional)
1. Goto the folder where your (.mp4) files are present
2. Press Shift and Left click and Choose "Open PowerShell Window Here"
or "Open Command Prompt Window Here"
3. Type "cmd" [NOTE: Skip this step if it directly opens cmd instead of PowerShell]
4. Run the command
for %i in (*.mp4) do ffmpeg -i "%i" "%~ni.mp3"
If you want to put this into a batch file on Windows 10, you need to use %%i.
I'm using this one-liner in linux to convert files (usually H265) into something I can play on Kodi without issues:
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
This converts to a temporary file and then replaces the original so the names remain the same after conversion.
I know this might be redundant but I use this script to batch convert files.
old_extension=$1
new_extension=$2
for i in *."$old_extension";
do ffmpeg -i "$i" "${i%.*}.$new_extension";
done
It takes 2 arguments to make it more flexible :
I create an alias for it but you can also use it manually like this:
sh batch_convert.sh mkv mp4
This would convert all the mkv
files into mp4
files.
As you can see it slightly more versatile. As long as ffmpeg
can convert it you can specify any two extensions.
The following script works well for me in a Bash on Windows (so it should work just as well on Linux and Mac). It addresses some problems I have had with some other solutions:
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
Example call:
$ sh ffmpeg-batch-convert.sh mp3 wav
As a bonus, if you want the source files deleted, you can modify the script like this:
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
Example call:
$ sh ffmpeg-batch-convert.sh mp3 wav delete