How do you convert an entire directory/folder with ffmpeg via command line or with a batch script?
-
Run from batch file. I found a solution that was close on reddit and then used chatGTB to clean it up and make it work. It converts mp4 to WebP. I made this a comment as I could not find a way to answer. Start code: @echo off REM Loop through each MP4 file in the current directory for %%a in (*.mp4) do ffmpeg -i "%%~a" -vcodec libwebp -filter:v fps=fps=20 -lossless 1 -loop 0 -preset default -an -vsync 0 "%%~na.webp" – Starblight Jun 12 '23 at 20:08
-
@Starblight What do you mean that you couldn't find a way to answer? There is a button that says "Answer This Question" at the bottom of this page (click Ctrl+End to jump to the end) – FFmpegEnthusiast Jun 12 '23 at 20:23
-
@TheRandomGuyNamedJoe12 Thanks for the feedback. I looked again including using your hotkey and I still am not seeing "answer this question" option. I even tried opening it in Chrome, disabled ad block, etc. no luck.Maybe I am just blind? I looked again and I think it might be this line at the end that explains it " The reputation requirement helps protect this question from spam and non-answer activity." – Starblight Jun 12 '23 at 22:27
-
@Starblight I have less reputation than you and I have the ability to answer – FFmpegEnthusiast Jun 13 '23 at 01:23
36 Answers
For Linux and macOS this can be done in one line, using parameter expansion to change the filename extension of the output file:
for i in *.avi; do ffmpeg -i "$i" "${i%.*}.mp4"; done

- 32,039
- 22
- 142
- 171

- 121,796
- 28
- 232
- 243
-
6This is perfect, thank you! Here's full command that ended up working great for me: for i in *.avi; do ffmpeg -i "$i" -c:a aac -b:a 128k -c:v libx264 -crf 20 "${i%.avi}.mp4"; done – Phil Kulak Apr 28 '17 at 03:29
-
I am getting `i was unexpected at this time.` error in cmd, windows 10. I used following line: `for i in *.mp3; do ffmpeg -i "$i" -map_metadata -1 -c:v copy -c:a copy "${i%.mp3}.mp3"; done` – Junaid May 17 '17 at 07:13
-
1@Junaid 1) Make sure to use a different name for the output file than the input, or output to another directory, because `ffmpeg` can't input and output to the same file. 2) I'm not sure if Bash commands work on Windows 10 natively. Maybe I should add to the answer that it is targeted towards systems that can natively use Bash such as Linux and macOS. lxs provided an Windows answer for this question. – llogan May 17 '17 at 17:25
-
Install git on Windows, it comes with bash shell (git-bash.exe). – Audrius Meškauskas May 20 '19 at 12:00
-
3This should be the top answer. Admittedly, the explanation for why {$i%.*} is not simple, but if you can put that aside and just "use it" you can quickly modify to suit. For example, I converted hundreds of .mp4's (with filenames having spaces and special characters) to a smaller format using: for i in *.mp4; do ffmpeg -i "$i" -s 512x288 -c:a copy "${i%.*}.m4v"; done – Tony M Jun 26 '19 at 15:56
-
It works great. On my case, upgrading from FLV to MP4, a plain copy thru thousands of files, this is the full command: `for i in *.flv; do ffmpeg -i "$i" -codec copy "${i%.*}.mp4"; done` – Luis H Cabrejo Jul 30 '19 at 07:35
-
3To take it one step further, you could use [Bash parameter substitution](https://www.cyberciti.biz/tips/bash-shell-parameter-substitution-2.html) if you would like to replace the string "x265" with "x264" if you're transcoding from H.265 to H.264 which is a common use case. `for f in *.mkv; do ffmpeg -i "$f" -map 0 -movflags faststart -c:v libx264 -c:a copy -c:s copy "${f/x265/x264}"; done` – Travis Runyard Dec 06 '19 at 16:36
-
Thank you, your solution is perfect. This slightly modified command worked for me `for i in *.MOV; do ffmpeg -i "$i" -vcodec libx265 -crf 20 "${i%.MOV}.mp4"; done` – Giorgi Aptsiauri Jul 18 '20 at 19:01
-
I am trying to use this answer with the additional `-map_metadata 0` flag, but it doesn't copy the metadata to the converted file. `for i in *.MOV; do ffmpeg -i "$i" -map_metadata 0 "${i%.*}.mp4"; done` – RHPT May 10 '22 at 03:01
Previous answer will only create 1 output file called out.mov. To make a separate output file for each old movie, try this.
for i in *.avi;
do name=`echo "$i" | cut -d'.' -f1`
echo "$name"
ffmpeg -i "$i" "${name}.mov"
done
-
29If you're like me and have lots of spaces (and a few other problematic characters) in your file names, I'd suggest addding double quotes : ffmpeg -i "$i" "$name.mov"; – Pif Dec 17 '12 at 22:36
-
9
-
3
-
-
-
3@Jazuly No, this is `sh` syntax. You can install Bash on Windows if you aren't yet prepared to ditch Windows entirely. – tripleee Jun 25 '19 at 16:07
-
2This seems to be trimming up to the first full stop and not last for me. So the filenames aren't the same but with .mov on the end. – Matt Dec 18 '19 at 12:45
-
1
-
1Fails for names like `Song name - feat. Artist.mp4` :). Probably because `cut` will stop at the first fullstop. llogan's answer resolves this issue – aggregate1166877 Jul 15 '21 at 23:15
-
And on Windows:
FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec mp3 "%~nG.mp3"

- 8,847
- 4
- 20
- 19
-
31if you run this command in a batch (.bat) file you need to double the % signs => %% – hB0 May 17 '15 at 14:24
-
1Any idea how to run this command but copy to a new file that includes the original file's metadata? – Duderino9000 Feb 26 '16 at 17:53
-
@Barryman9000 this was a long time ago but I think there's an output file option you could pass – lxs Mar 21 '16 at 15:00
-
@lxs thanks for the follow up. I ended up doing it with Powershell by changing the new file's name to be the original file's date created http://stackoverflow.com/a/35671099/197472 – Duderino9000 Mar 21 '16 at 23:08
-
Used it for removing metadata. But it is giving me Access denied error. So I changed output file name with an extra space to make it new file. `FOR /F "tokens=*" %G IN ('dir /b *.mp3') DO ffmpeg -i "%G" -map_metadata -1 -c:v copy -c:a copy "%~nG .mp3"` – Junaid May 17 '17 at 07:27
-
9For **PowerShell**: `Get-ChildItem *.ogg -recurse | % { ffmpeg.exe -i $_.FullName -map_metadata -1 -c:v copy -c:a copy ("NewPath" + "\" +$_.Name) } ` Where `NewPath` = new directory path. – Junaid Nov 30 '18 at 19:55
-
-
Based on this code, here is how to convert AVI to MP4, making sure with AAC audio: `FOR /F "tokens=*" %G IN ('dir /b *.avi') DO ffmpeg -i "%G" -c:v copy -c:a aac -y "%~nG.mp4"` – pmdci Jan 23 '22 at 23:08
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)
- Goto the folder where your (.mp4) files are present
- Press Shift and Right click and Choose "Open PowerShell Window Here"
or "Open Command Prompt Window Here" - Type "cmd" [NOTE: Skip this step if it directly opens cmd instead of PowerShell]
- 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.

- 39
- 1
- 2
- 10

- 1,226
- 14
- 22
-
2This works on command prompt for me, but not for powershell Missing opening '(' after keyword 'for'. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword – Katastic Voyage Nov 11 '20 at 06:39
-
5
-
powershell has a completely different syntax that's more like an actual programming language. I'm sure it'd be possible to do this in powershell but it'd be different – reticivis Jul 24 '22 at 04:34
A one-line bash script would be easy to do - replace *.avi
with your filetype:
for i in *.avi; do ffmpeg -i "$i" -qscale 0 "$(basename "$i" .avi)".mov ; done

- 729
- 7
- 10
-
The default encoder for .mov is libx264 (if available), but this encoder ignores `-qscale`. Remove it and use the default settings, or use `-crf` instead (default is `-crf 23`). – llogan Aug 14 '16 at 19:29
To convert with subdirectories use e.g.
find . -exec ffmpeg -i {} {}.mp3 \;

- 1,543
- 12
- 13
-
1I used this, combined with [this answer](https://superuser.com/a/1093206/) to convert VTT to SRT, to great effect. `find -name "*.vtt" -exec ffmpeg -i {} {}.srt \;` – grooveplex Jun 07 '18 at 19:40
-
I this command, with slight modif to convert all mp4 to mp3: `find *.mp4 -exec ffmpeg -i {} {}.mp3 \;` – swdev Jun 15 '18 at 23:28
-
3Or, if you want to convert multiple file types: `find . -name *.ogg -or -name *.wma -exec ffmpeg -i {} {}.mp3 \;` – bonh Sep 26 '18 at 18:36
-
Convert all wma files to mp3 and after delete them: `find . -name *.wma -exec ffmpeg -i {} {}.mp3 \; -exec rm {} \;` – Panagiotis Dec 21 '19 at 17:35
-
-
How can I specify a new directory for the output files with this method? – user2028856 Mar 26 '23 at 00:19
@Linux To convert a bunch, my one liner is this, as example (.avi to .mkv) in same directory:
for f in *.avi; do ffmpeg -i "${f}" "${f%%.*}.mkv"; done
please observe the double "%%" in the output statement. It gives you not only the first word or the input filename, but everything before the last dot.

- 129,424
- 31
- 207
- 592

- 441
- 4
- 3
-
In my case I had to use single `%`. `{string%%substring}` deletes the longest match of substring from string - giving you the part before the first period whereas `{string%substring}` deletes the shortest match - deleting only the extension. – nikhilweee Dec 17 '21 at 22:18
-
For anyone who wants to batch convert anything with ffmpeg but would like to have a convenient Windows interface, I developed this front-end:
https://sourceforge.net/projects/ffmpeg-batch
It adds to ffmpeg a window fashion interface, progress bars and time remaining info, features I always missed when using ffmpeg.

- 460
- 5
- 7
-
Hello @Eibel and thanks for the cool app! I was wondering if there's a way to batch create mp4s or mkvs from a list of mp3 or m4a files and a single png image as background with your app? I've looked into this answer https://superuser.com/questions/845644/batch-converting-mp3-and-still-image-png-to-mp4-videos/1705469#1705469 and came up with this `for %i in (*.m4a) do ffmpeg -f image2 -loop 1 -framerate 5 -thread_queue_size 10080 -i "C:\Users\Head Rule\Desktop\foldera\isp.png" ^ -thread_queue_size 10080 -i "%i" -c:v libx264 -crf 47 -preset ultrafast -tune stillimage -c:a copy "%~ni.mp4"` – Lod Feb 17 '22 at 15:20
-
but the problem is that it outputs a single .mp4 file instead of the multiple .mp4 expected. And also it's quite slow and I couldn't find an answer to make it faster yet. My goal is to have the smallest file size possible with the fastest file creation possible (i don't mind the video quality as the .mp4 files are just to upload and then listen to. I need it to be video so I can comment below the video). I've tried the from image to video feature of your app but so far I haven't found the way to add the still image nor the way to batch the process. Thanks for your help! – Lod Feb 17 '22 at 15:25
-
Sorry I just noticed with my code above it keeps encoding past the end of the 1st m4a file with no sound/image and doesn't stop. So that it doesn't go to next m4a file when the previous ends. – Lod Feb 17 '22 at 15:39
-
1Hi, you can use an "Image to video wizard" available in latest versions, in case no other solution comes up. – Eibel Feb 18 '22 at 18:24
-
Hi, thanks for the tip. I got it working by loading first a .png then the m4a from "Image to video wizard > Create a video from every image > Add Audio Track". But now I'm wondering how to batch the process. Is it currently possible to create the "image to Video" mp4 files for multiple audio m4a files, with the same image? I'd like if possible to batch the entire folder of m4a files to be converted into mp4 with the same image for each mp4. The wizard allows for only one audio file at a time it seems. Is there another batch way? Thanks again. – Lod Feb 18 '22 at 23:06
-
1Hi, well, you're right, wizard do not allow that. I found a workaround, you may try it, following these instructions (change image file path accordingly): - Add your audio files to file list. - On pre-input box write: -loop 1 -i "C:\flac\Test.jpg" -r 10 - On parameters box: -map 0:v:0 -c:v libx264 -preset ultrafast -r 10 -crf 23 -tune stillimage -pix_fmt yuv420p -vf scale=1280:720 -map 1:a:0 -c:a aac -b:a 128K -shortest If your audio files are mp3 or aac, you can use -c:a copy – Eibel Feb 20 '22 at 16:16
-
1Hi @Eibel and many thanks for the reply. It's working perfectly! I've made a step by step demo here: https://superuser.com/a/1706004/1105013 Thanks again and be well! On pre-input : `-loop 1 -i "
"` On parameters box (for mp3s or aacs): `-map 0:v:0 -c:v libx264 -preset ultrafast -r 10 -crf 23 -tune stillimage -pix_fmt yuv420p -vf scale=1280:720 -map 1:a:0 -c:a copy -b:a 128K -shortest` On parameters box (for m4a tested): `-map 0:v:0 -c:v libx264 -preset ultrafast -r 10 -crf 23 -tune stillimage -pix_fmt yuv420p -vf scale=1280:720 -map 1:a:0 -c:a copy -b:a 128K -shortest` – Lod Feb 20 '22 at 18:34 -
1Hi @Lod I've tuned the parameters to make process much faster. These would be the ones: -Pre input: -loop 1 -r 1/1 -i "[Youri Image Path]" ---Parameters: -map 0:v:0 -c:v libx264 -preset veryfast -tune stillimage -pix_fmt yuv420p -vf fps=1 -map 1:a:0 -c:a aac -b:a 128K -shortest (Image size should be video standard, like 1280x720, 1920x1080). – Eibel Feb 23 '22 at 14:52
-
Many thanks again @Eibel for the kind follow up. Added to the other thread https://superuser.com/a/1706712/1105013 Be well! – Lod Feb 24 '22 at 11:14
-
Hi @Eibel . I'm getting this error when trying to convert mp3 to mp4 : https://i.imgur.com/CNxprSc.png Any fix suggestion? Thanks! – Lod Feb 20 '23 at 14:23
-
Ah, I resized the image to 1036 x 560 and now it seems to work. Any way to make the resizing automatic when the app detects its ratio is not divisible by 2? Many thanks again! – Lod Feb 20 '23 at 14:33
-
1Hi @Lod, it is not possible to make that automatically yet, but you could add this parameter: -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" and iti should remove the error message. https://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2 – Eibel Feb 21 '23 at 18:59
-
Ah, ok! Thanks for the workaround. I'll test it asa. Thank again for the great app. Be well! – Lod Feb 21 '23 at 19:26
Of course, now PowerShell has come along, specifically designed to make something exactly like this extremely easy.
And, yes, PowerShell is also available on other operating systems other than just Windows, but it comes pre-installed on Windows, so this should be useful to everyone.
First, you'll want to list all of the files within the current directory, so, we'll start off with:
ls
You can also use ls -Recurse
if you want to recursively convert all files in subdirectories too.
Then, we'll filter those down to only the type of file we want to convert - e.g. "avi".
ls | Where { $_.Extension -eq ".avi" }
After that, we'll pass that information to FFmpeg through a ForEach
.
For FFmpeg's input, we will use the FullName
- that's the entire path to the file. And for FFmpeg's output we will use the Name
- but replacing the .avi
at the end with .mp3
. So, it will look something like this:
$_.Name.Replace(".avi", ".mp3")
So, let's put all of that together and this is the result:
ls | Where { $_.Extension -eq ".avi" } | ForEach { ffmpeg -i $_.FullName $_.Name.Replace(".avi", ".mp3") }
That will convert all ".avi" files into ".mp3" files through FFmpeg, just replace the three things in quotes to decide what type of conversion you want, and feel free to add any other arguments to FFmpeg within the ForEach
.
You could take this a step further and add Remove-Item
to the end to automatically delete the old files.
If ffmpeg
isn't in your path, and it's actually in the directory you're currently in, write ./ffmpeg
there instead of just ffmpeg
.
Hope this helps anyone.

- 489
- 6
- 22
-
2PowerShell is great, thanks! In case anyone else runs into trouble running this: put this command in a `.ps1` file, not a `.bat` file. You'll have to run `Set-ExecutionPolicy RemoteSigned` as administrator if you've never run a PS script before. – starpause May 20 '20 at 15:04
-
-
@imba-tjd Sure, in place of the `.Replace` I can see that. **However,** I think this example could be even better anyway, by doing `$_.FullName.Replace` instead of `$_.Name.Replace` so absolute paths are handled more elegantly. If it were *that*, which I think is much better, *then* it'd get quite cumbersome to use `Split-Path`, especially for something that my intention was you'd type on the spot. Even if it does better support the very unlikely scenario that the replace approach doesn't work on – ABPerson Aug 24 '23 at 16:40
Using multiple cores, this is the fastest way, (using parallel):
parallel "ffmpeg -i {1} {1.}.mp4" ::: *.avi

- 1,613
- 1
- 21
- 30
-
Note that ffmpeg already uses all available cores when encoding each file, so you might not get much (or any) real performance improvement by asking it to process multiple files in parallel. – Luke Taylor Jan 09 '23 at 01:21
-
2@Luke, for video files yes, but not for all files e.g. sound files (FLAC) or some image formats, you can still speed up by using parallel. – Janghou Jan 10 '23 at 10:46
-
@LukeTaylor I can confirm what Janghou said - it is a tremendous speedup if you have 20+ files. – Agile Bean Jul 03 '23 at 23:51
If you have GNU parallel you could convert all .avi files below vid_dir
to mp4 in parallel, using all except one of your CPU cores with
find vid_dir -type f -name '*.avi' -not -empty -print0 |
parallel -0 -j -1 ffmpeg -loglevel fatal -i {} {.}.mp4
To convert from/to different formats, change '*.avi'
or .mp4
as needed. GNU parallel is listed in most Linux distributions' repositories in a package which is usually called parallel
.

- 111
- 3
- 4
-
couldn't you could do the same by adding `!` to the end of any of the bash one-liners? – stib Jan 22 '19 at 07:38
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 :
- the extension you want to convert from
- the new extension you want to convert to
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.

- 5,261
- 4
- 30
- 36
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:
- Processes files in subfolders
- Replaces the source extension with the target extension instead of just appending it
- Works with files with multiple spaces and multiple dots in the name (See this answer for details.)
- Can be run when the target file exists, prompting before overwriting
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

- 4,448
- 4
- 37
- 48
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.

- 3,346
- 2
- 30
- 38
I use this for add subtitle for Tvshows or Movies on Windows.
Just create "subbed" folder and bat file in the video and sub directory.Put code in bat file and run.
for /R %%f in (*.mov,*.mxf,*.mkv,*.webm) do (
ffmpeg.exe -i "%%~f" -i "%%~nf.srt" -map 0:v -map 0:a -map 1:s -metadata:s:a language=eng -metadata:s:s:1 language=tur -c copy ./subbed/"%%~nf.mkv"
)

- 51
- 1
- 2
-
This works well in a batch file. For anyone trying to use this on the normal command line, use only one percent `%` symbol. – Brad Dec 12 '20 at 00:27
Also if you want same convertion in subfolders. here is the recursive code.
for /R "folder_path" %%f in (*.mov,*.mxf,*.mkv,*.webm) do (
ffmpeg.exe -i "%%~f" "%%~f.mp4"
)

- 108
- 8
Alternative approach using fd
command (repository):
cd directory
fd -d 1 mp3 -x ffmpeg -i {} {.}.wav
-d
means depth
-x
means execute
{.}
path without file extension

- 1,558
- 3
- 16
- 22
windows:
@echo off
for /r %%d in (*.wav) do (
ffmpeg -i "%%~nd%%~xd" -codec:a libmp3lame -c:v copy -qscale:a 2 "%
%~nd.2.mp3"
)
this is variable bitrate of quality 2, you can set it to 0 if you want but unless you have a really good speaker system it's worthless imo

- 1,849
- 24
- 38
Only this one Worked for me, pls notice that you have to create "newfiles" folder manually where the ffmpeg.exe file is located.
Convert . files to .wav audio Code:
for %%a in ("*.*") do ffmpeg.exe -i "%%a" "newfiles\%%~na.wav"
pause
i.e if you want to convert all .mp3 files to .wav change ("*.*")
to ("*.mp3")
.
The author of this script is :
https://forum.videohelp.com/threads/356314-How-to-batch-convert-multiplex-any-files-with-ffmpeg
hope it helped .

- 45
- 1
- 9
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.

- 21
- 2
For giggles, here's solution in fish-shell:
for i in *.avi; ffmpeg -i "$i" (string split -r -m1 . $i)[1]".mp4"; end

- 2,238
- 1
- 21
- 16
-
this is exactly what I was looking for! Do you know how to do it for either avi or mov files in a folder? – mesqueeb Dec 30 '21 at 07:52
I developed a python package for this case.
https://github.com/developer0hye/BatchedFFmpeg
You can easily install and use it.
pip install batchedffmpeg
batchedffmpeg * -i folder * output_file

- 183
- 3
- 8
Another simple solution that hasn't been suggested yet would be to use xargs
:
ls *.avi | xargs -i -n1 ffmpeg -i {} "{}.mp4"
One minor pitfall is the awkward naming of output files (e.g. input.avi.mp4
). A possible workaround for this might be:
ls *.avi | xargs -i -n1 bash -c "i={}; ffmpeg -i {} "\${i%.*}.mp4"
"

- 884
- 6
- 18
-
[shellcheck.net](http://shellcheck.net/) has a few suggestions regarding your examples. – llogan Nov 27 '18 at 18:59
-
http://mywiki.wooledge.org/ParsingLs though you can simply replace `ls` with `printf '%s\n'` here. – tripleee Jun 25 '19 at 16:11
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");

- 26
- 4
I needed all the videos to use the same codec for merging purposes
so this conversion is mp4 to mp4
it's in zsh but should easily be convertible to bash
for S (*.mp4) { ffmpeg -i $S -c:v libx264 -r 30 new$S }

- 4,743
- 5
- 48
- 45
Bash is terrible to me, so under Linux/Mac, I prefer Ruby script:
( find all the files in a folder and then convert it from rmvb/rm
format to mp4
format )
# filename: run.rb
Dir['*'].each{ |rm_file|
next if rm_file.split('.').last == 'rb'
command = "ffmpeg -i '#{rm_file}' -c:v h264 -c:a aac '#{rm_file.split('.')[0]}.mp4'"
puts "== command: #{command}"
`#{command}`
}
and you can run it with: ruby run.rb

- 19,858
- 7
- 75
- 95
if you don't want to convert the file and just copy codec use the code bellow
for %i in (*.mkv) do ffmpeg -i "%i" -codec copy "%~ni.mp4"
like this you will reduce the convertion time.

- 494
- 6
- 23
Copying off of @HiDd3n, you can also do this if you want to recursively search through the directory of files in case you have numerous folders:
for /r %i in (*.webm) do "C:\Program Files\ffmpeg\bin\ffmpeg.exe" -i "%i" "%~ni.mp3"

- 35
- 4
This one script finds and converts any files that ffmpeg supports (no need to specify only one type):
find . \( -name "*.3dostr" -o -name "*.3g2" -o -name "*.3gp" -o -name "*.aa" -o -name "*.aac" -o -name "*.ac3" -o -name "*.acm" -o -name "*.act" -o -name "*.adf" -o -name "*.adp" -o -name "*.ads" -o -name "*.adts" -o -name "*.afc" -o -name "*.aiff" -o -name "*.aix" -o -name "*.alp" -o -name "*.amr" -o -name "*.amrnb" -o -name "*.amrwb" -o -name "*.anm" -o -name "*.apc" -o -name "*.ape" -o -name "*.apm" -o -name "*.apng" -o -name "*.aptx" -o -name "*.aqtitle" -o -name "*.asf" -o -name "*.asf_o" -o -name "*.asf_stream" -o -name "*.ass" -o -name "*.ast" -o -name "*.au" -o -name "*.av1" -o -name "*.avi" -o -name "*.avisynth" -o -name "*.avm2" -o -name "*.avr" -o -name "*.avs" -o -name "*.avs2" -o -name "*.bethsoftvid" -o -name "*.bfi" -o -name "*.bfstm" -o -name "*.bin" -o -name "*.bink" -o -name "*.bit" -o -name "*.bmv" -o -name "*.boa" -o -name "*.brstm" -o -name "*.c93" -o -name "*.caf" -o -name "*.cavsvideo" -o -name "*.cdg" -o -name "*.cdxl" -o -name "*.cine" -o -name "*.codec2" -o -name "*.codec2raw" -o -name "*.concat" -o -name "*.crc" -o -name "*.dash" -o -name "*.data" -o -name "*.daud" -o -name "*.dcstr" -o -name "*.dds_pipe" -o -name "*.derf" -o -name "*.dfa" -o -name "*.dhav" -o -name "*.dirac" -o -name "*.dnxhd" -o -name "*.dsf" -o -name "*.dshow" -o -name "*.dsicin" -o -name "*.dss" -o -name "*.dts" -o -name "*.dtshd" -o -name "*.dv" -o -name "*.dvbsub" -o -name "*.dvbtxt" -o -name "*.dvd" -o -name "*.dxa" -o -name "*.ea" -o -name "*.eac3" -o -name "*.epaf" -o -name "*.f32be" -o -name "*.f32le" -o -name "*.f4v" -o -name "*.f64be" -o -name "*.f64le" -o -name "*.ffmetadata" -o -name "*.fifo" -o -name "*.filmstrip" -o -name "*.fits" -o -name "*.flac" -o -name "*.flic" -o -name "*.flv" -o -name "*.framecrc" -o -name "*.framehash" -o -name "*.framemd5" -o -name "*.frm" -o -name "*.fsb" -o -name "*.fwse" -o -name "*.g722" -o -name "*.g723_1" -o -name "*.g726" -o -name "*.g726le" -o -name "*.g729" -o -name "*.gdigrab" -o -name "*.gdv" -o -name "*.genh" -o -name "*.gif" -o -name "*.gif_pipe" -o -name "*.gsm" -o -name "*.gxf" -o -name "*.h261" -o -name "*.h263" -o -name "*.h264" -o -name "*.hash" -o -name "*.hca" -o -name "*.hcom" -o -name "*.hds" -o -name "*.hevc" -o -name "*.hls" -o -name "*.hnm" -o -name "*.ico" -o -name "*.idcin" -o -name "*.idf" -o -name "*.iff" -o -name "*.ifv" -o -name "*.ilbc" -o -name "*.image2" -o -name "*.image2pipe" -o -name "*.ingenient" -o -name "*.ipmovie" -o -name "*.ipod" -o -name "*.ircam" -o -name "*.ismv" -o -name "*.iss" -o -name "*.iv8" -o -name "*.ivf" -o -name "*.ivr" -o -name "*.j2k_pipe" -o -name "*.jacosub" -o -name "*.jpeg_pipe" -o -name "*.jpegls_pipe" -o -name "*.jv" -o -name "*.kux" -o -name "*.kvag" -o -name "*.latm" -o -name "*.lavfi" -o -name "*.libopenmpt" -o -name "*.live_flv" -o -name "*.lmlm4" -o -name "*.loas" -o -name "*.lrc" -o -name "*.lvf" -o -name "*.lxf" -o -name "*.m4v" -o -name "*.matroska" -o -name "*.webm" -o -name "*.mcc" -o -name "*.md5" -o -name "*.mgsts" -o -name "*.microdvd" -o -name "*.mjpeg" -o -name "*.mjpeg_2000" -o -name "*.mkvtimestamp_v2" -o -name "*.mlp" -o -name "*.mlv" -o -name "*.mm" -o -name "*.mmf" -o -name "*.mov" -o -name "*.mp4" -o -name "*.m4a" -o -name "*.3gp" -o -name "*.3g2" -o -name "*.mj2" -o -name "*.mp2" -o -name "*.mp3" -o -name "*.mp4" -o -name "*.mpc" -o -name "*.mpc8" -o -name "*.mpeg" -o -name "*.mpeg1video" -o -name "*.mpeg2video" -o -name "*.mpegts" -o -name "*.mpegtsraw" -o -name "*.mpegvideo" -o -name "*.mpjpeg" -o -name "*.mpl2" -o -name "*.mpsub" -o -name "*.msf" -o -name "*.msnwctcp" -o -name "*.mtaf" -o -name "*.mtv" -o -name "*.mulaw" -o -name "*.musx" -o -name "*.mv" -o -name "*.mvi" -o -name "*.mxf" -o -name "*.mxf_d10" -o -name "*.mxf_opatom" -o -name "*.mxg" -o -name "*.nc" -o -name "*.nistsphere" -o -name "*.nsp" -o -name "*.nsv" -o -name "*.null" -o -name "*.nut" -o -name "*.nuv" -o -name "*.oga" -o -name "*.ogg" -o -name "*.ogv" -o -name "*.oma" -o -name "*.opus" -o -name "*.paf" -o -name "*.pam_pipe" -o -name "*.pbm_pipe" -o -name "*.pcx_pipe" -o -name "*.pgm_pipe" -o -name "*.pgmyuv_pipe" -o -name "*.pgx_pipe" -o -name "*.pictor_pipe" -o -name "*.pjs" -o -name "*.pmp" -o -name "*.png_pipe" -o -name "*.pp_bnk" -o -name "*.ppm_pipe" -o -name "*.psd_pipe" -o -name "*.psp" -o -name "*.psxstr" -o -name "*.pva" -o -name "*.pvf" -o -name "*.qcp" -o -name "*.qdraw_pipe" -o -name "*.r3d" -o -name "*.rawvideo" -o -name "*.realtext" -o -name "*.redspark" -o -name "*.rl2" -o -name "*.rm" -o -name "*.roq" -o -name "*.rpl" -o -name "*.rsd" -o -name "*.rso" -o -name "*.rtp" -o -name "*.rtp_mpegts" -o -name "*.rtsp" -o -name "*.s16be" -o -name "*.s16le" -o -name "*.s24be" -o -name "*.s24le" -o -name "*.s32be" -o -name "*.s32le" -o -name "*.s337m" -o -name "*.s8" -o -name "*.sami" -o -name "*.sap" -o -name "*.sbc" -o -name "*.sbg" -o -name "*.scc" -o -name "*.sdl" -o -name "*.sdl2" -o -name "*.sdp" -o -name "*.sdr2" -o -name "*.sds" -o -name "*.sdx" -o -name "*.segment" -o -name "*.ser" -o -name "*.sgi_pipe" -o -name "*.shn" -o -name "*.siff" -o -name "*.singlejpeg" -o -name "*.sln" -o -name "*.smjpeg" -o -name "*.smk" -o -name "*.smoothstreaming" -o -name "*.smush" -o -name "*.sol" -o -name "*.sox" -o -name "*.spdif" -o -name "*.spx" -o -name "*.srt" -o -name "*.stl" -o -name "*.stream_segment" -o -name "*.ssegment" -o -name "*.streamhash" -o -name "*.subviewer" -o -name "*.subviewer1" -o -name "*.sunrast_pipe" -o -name "*.sup" -o -name -o -name "*.svag" -o -name "*.svcd" -o -name "*.svg_pipe" -o -name "*.swf" -o -name "*.tak" -o -name "*.tedcaptions" -o -name "*.tee" -o -name "*.thp" -o -name "*.tiertexseq" -o -name "*.tiff_pipe" -o -name "*.tmv" -o -name "*.truehd" -o -name "*.tta" -o -name "*.tty" -o -name "*.txd" -o -name "*.ty" -o -name "*.u16be" -o -name "*.u16le" -o -name "*.u24be" -o -name "*.u24le" -o -name "*.u32be" -o -name "*.u32le" -o -name "*.u8" -o -name "*.uncodedframecrc" -o -name "*.v210" -o -name "*.v210x" -o -name "*.vag" -o -name "*.vc1" -o -name "*.vc1test" -o -name "*.vcd" -o -name "*.vfwcap" -o -name "*.vidc" -o -name "*.vividas" -o -name "*.vivo" -o -name "*.vmd" -o -name "*.vob" -o -name "*.vobsub" -o -name "*.voc" -o -name "*.vpk" -o -name "*.vplayer" -o -name "*.vqf" -o -name "*.w64" -o -name "*.wav" -o -name "*.wc3movie" -o -name "*.webp_pipe" -o -name "*.webvtt" -o -name "*.wsaud" -o -name "*.wsd" -o -name "*.wsvqa" -o -name "*.wtv" -o -name "*.wv" -o -name "*.wve" -o -name "*.xa" -o -name "*.xbin" -o -name "*.xmv" -o -name "*.xpm_pipe" -o -name "*.xvag" -o -name "*.xwd_pipe" -o -name "*.xwma" -o -name "*.yop" -o -name "*.yuv4mpegpipe" \) -exec sh -c 'ffmpeg -n -i "$0" -vn -q:a 0 "${0%.*}.mp3"' {} \;
Replace the last command with 'ffmpeg -n -i "$0" -vn -q:a 0 "${0%.*}.mp3" && rm "$0"'
to delete the original after the conversion
P.S: for the record, that list is as follows:
*.3gp, *.aa, *.aac, *.ac3, *.acm, *.act, *.adf, *.adp, *.ads, *.adts, *.afc, *.aiff, *.aix, *.alp, *.amr, *.amrnb, *.amrwb, *.anm, *.apc, *.ape, *.apm, *.apng, *.aptx, *.aqtitle, *.asf, *.asf_o, *.asf_stream, *.ass, *.ast, *.au, *.av1, *.avi, *.avisynth, *.avm2, *.avr, *.avs, *.avs2, *.bethsoftvid, *.bfi, *.bfstm, *.bin, *.bink, *.bit, *.bmv, *.boa, *.brstm, *.c93, *.caf, *.cavsvideo, *.cdg, *.cdxl, *.cine, *.codec2, *.codec2raw, *.concat, *.crc, *.dash, *.data, *.daud, *.dcstr, *.dds_pipe, *.derf, *.dfa, *.dhav, *.dirac, *.dnxhd, *.dsf, *.dshow, *.dsicin, *.dss, *.dts, *.dtshd, *.dv, *.dvbsub, *.dvbtxt, *.dvd, *.dxa, *.ea, *.eac3, *.epaf, *.f32be, *.f32le, *.f4v, *.f64be, *.f64le, *.ffmetadata, *.fifo, *.filmstrip, *.fits, *.flac, *.flic, *.flv, *.framecrc, *.framehash, *.framemd5, *.frm, *.fsb, *.fwse, *.g722, *.g723_1, *.g726, *.g726le, *.g729, *.gdigrab, *.gdv, *.genh, *.gif, *.gif_pipe, *.gsm, *.gxf, *.h261, *.h263, *.h264, *.hash, *.hca, *.hcom, *.hds, *.hevc, *.hls, *.hnm, *.ico, *.idcin, *.idf, *.iff, *.ifv, *.ilbc, *.image2, *.image2pipe, *.ingenient, *.ipmovie, *.ipod, *.ircam, *.ismv, *.iss, *.iv8, *.ivf, *.ivr, *.j2k_pipe, *.jacosub, *.jpeg_pipe, *.jpegls_pipe, *.jv, *.kux, *.kvag, *.latm, *.lavfi, *.libopenmpt, *.live_flv, *.lmlm4, *.loas, *.lrc, *.lvf, *.lxf, *.m4v, *.matroska, *.webm, *.mcc, *.md5, *.mgsts, *.microdvd, *.mjpeg, *.mjpeg_2000, *.mkvtimestamp_v2, *.mlp, *.mlv, *.mm, *.mmf, *.mov, *.mp4, *.m4a, *.3gp, *.3g2, *.mj2, *.mp2, *.mp3, *.mpc, *.mpc8, *.mpeg, *.mpeg1video, *.mpeg2video, *.mpegts, *.mpegtsraw, *.mpegvideo, *.mpjpeg, *.mpl2, *.mpsub, *.msf, *.msnwctcp, *.mtaf, *.mtv, *.mulaw, *.musx, *.mv, *.mvi, *.mxf, *.mxf_d10, *.mxf_opatom, *.mxg, *.nc, *.nistsphere, *.nsp, *.nsv, *.null, *.nut, *.nuv, *.oga, *.ogg, *.ogv, *.oma, *.opus, *.paf, *.pam_pipe, *.pbm_pipe, *.pcx_pipe, *.pgm_pipe, *.pgmyuv_pipe, *.pgx_pipe, *.pictor_pipe, *.pjs, *.pmp, *.png_pipe, *.pp_bnk, *.ppm_pipe, *.psd_pipe, *.psp, *.psxstr, *.pva, *.pvf, *.qcp, *.qdraw_pipe, *.r3d, *.rawvideo, *.realtext, *.redspark, *.rl2, *.rm, *.roq, *.rpl, *.rsd, *.rso, *.rtp, *.rtp_mpegts, *.rtsp, *.s16be, *.s16le, *.s24be, *.s24le, *.s32be, *.s32le, *.s337m, *.s8, *.sami, *.sap, *.sbc, *.sbg, *.scc, *.sdl, *.sdl2, *.sdp, *.sdr2, *.sds, *.sdx, *.segment, *.ser, *.sgi_pipe, *.shn, *.siff, *.singlejpeg, *.sln, *.smjpeg, *.smk, *.smoothstreaming, *.smush, *.sol, *.sox, *.spdif, *.spx, *.srt, *.stl, *.stream_segment, *.ssegment, *.streamhash, *.subviewer, *.subviewer1, *.sunrast_pipe, *.sup, *.svag, *.svcd, *.svg_pipe, *.swf, *.tak, *.tedcaptions, *.tee, *.thp, *.tiertexseq, *.tiff_pipe, *.tmv, *.truehd, *.tta, *.tty, *.txd, *.ty, *.u16be, *.u16le, *.u24be, *.u24le, *.u32be, *.u32le, *.u8, *.uncodedframecrc, *.v210, *.v210x, *.vag, *.vc1, *.vc1test, *.vcd, *.vfwcap, *.vidc, *.vividas, *.vivo, *.vmd, *.vob, *.vobsub, *.voc, *.vpk, *.vplayer, *.vqf, *.w64, *.wav, *.wc3movie, *.webp, *.webp_pipe, *.webvtt, *.wsaud, *.wsd, *.wsvqa, *.wtv, *.wv, *.wve, *.xa, *.xbin, *.xmv, *.xpm_pipe, *.xvag, *.xwd_pipe, *.xwma, *.yop, *.yuv4mpegpipe

- 261
- 2
- 11
If you want a graphical interface to batch process with ffmpegX, try Quick Batcher. It's free and will take your last ffmpegX settings to convert files you drop into it.
Note that you can't drag-drop folders onto Quick Batcher. So select files and then put them through Quick Batcher.

- 193
- 1
- 3
little php script to do it:
#!/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' ) );
}

- 19,904
- 4
- 43
- 89
-
3A very limited scope answer as a user has to have PHP installed on their machine. The command line and batch file answers are much easier, and much less complex. – ProfK Nov 10 '17 at 09:06
-
3@ProfK [PHP is 1 of the most popular languages on SO](https://insights.stackoverflow.com/survey/2017) - second, Isaac's answer for sh is somewhat unreliable, in that it might rename your files to something else than the original, for example, it doesn't preserve newlines in the filename. lyx's bat script is even worse, it __COMPLETELY IGNORES__ any file with newlines in the name. not sure why, not even a syntax error or anything, but it does (tested on win10). my php script has neither problems, thanks to `escapeshellarg()`, and works both on windows and linux. i agree its a edge-case though. – hanshenrik Nov 12 '17 at 12:43
This is what I use to batch convert avi to 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"
Works well as a cmd file, run it, the loop finds all avi files in that folder.
calls MY (change for yours) ffmpeg, passes input name, the settings are for rescaling up with sharpening. I probs don't need CRF and "-b:v 1024k
"...
Output file is input file minus the extension, with mp4 as new ext.

- 129,424
- 31
- 207
- 592

- 1
And for Windows, this does not work
FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec mp3 "%~nG.mp3"
even if I do double those %
.
I would even suggest:
-acodec ***libmp3lame***
also:
FOR /F "tokens=*" %G IN ('dir /b *.flac') DO ffmpeg -i "%G" -acodec libmp3lame "%~nG.mp3"

- 129,424
- 31
- 207
- 592
-
4Is this an answer, or an attempt to post a new question as an answer? In the latter case, please create a completely new question instead, though possibly linking back here for background. – tripleee Jun 12 '20 at 05:59
On Windows, for files in subdirectories, open a command prompt window and enter the following commands.
cd "C:\temp"
FOR /F "tokens=*" %G IN ('dir /s /b /o:gn *.mp4') DO ffmpeg -i "%G" -c copy "%~pG%~nG.mkv"
This converts all mp4 files to mkvs including files in subdirectories and places the mkv file where the mp4 file is i.e. a file C:\temp\test.mp4
will have it corresponding mkv in C:\temp\test.mkv

- 353
- 2
- 7
Convert all .wav
files in the folder to mp3 on Windows using a batch file.
- Create file
convertToMp3.bat
- Put inside this line:
for %%i in (*.wav) do ffmpeg -i "%%i" "%%~ni.mp3"
- Save the file in the same folder where
.wav
files are. - Double click
convertToMp3.bat

- 4,936
- 2
- 38
- 31