Windows Batch file: How to copy and convert folders recursively with ffmpeg
I needed to copy out my entire collection of music to a pen-drive to plug-in while driving. Now, being the sort of car that doesn’t play but mp3 I was in for a world of pain. I downloaded ffmpeg. Converted a couple of files. Things went smoothly and I was really happy. Only, I realized later that ffmpeg will not convert folders recursively. Search reveals quite a few scripts — bash, perl, etc. But I was on Windows and I didn’t have but DOS. I could recurse over the folders on my disk and dump them all in one folder on the pen-drive but then I am a stickler for keeping my files in folders. So, no joy whatsoever with just laziness.
It is almost always such things as bad-luck that probably ever forces a programmer to write code at home. So, dug I in, my hoofs. There were two things that needed to be done: 1) recreate the directory structure and 2) convert each file within the directories and sub-directories. And I knew with a bit more searching I’d be there 😉
There’s a little-known parameter to the ubiquitous xcopy command that copies only the directory hierarchy (you can optionally tell whether you want empty directories to be copied or not!) — whoohoohoo! That there is a piece of cake.
xcopy /T source-dir-hierarchy target-dir-hierarchy
Then next part was easy. Just shut ffmpeg up with the -v quite option (you don’t want so much information when you’re doing a batch job, do you?). And of course, loop it over using the swiss-army knife of DOS — the FOR command (yep, a favorite of mine).
for /R %1 %%v in (*.mp4) do ffmpeg -v quiet -i “%%v” “%2\%%~nv.mp3”
Note, the awesome thing about the (file-set) parameter of FOR allows multiple comma separated wild-card based entries. (Whoever thinks of these options must be a freaking genius!) So you can, instead of just converting mp4s, say:
for /R %1 %%v in (*.mp4, *.aac, *.flv, *.m4a, *.mp3) do ffmpeg -v quiet -i “%%v” “%2\%%~nv.mp3”
It really doesn’t make a lot of sense to convert a mp3 file to mp3 again but I left it in to avoid having to jump through all the hoops just for a plain-old file copy.
And finally, wrap it all up in nice little batch file. There, you go, then!
@echo off REM keep a counter for files converted set /A nfile=0 REM do not copy empty folders or any files @echo Copying directory structure from %0 to %1 ... xcopy /T %1 %2 REM walk directory structure and convert each file in quiet mode for /R %1 %%v in (*.mp4, *.aac, *.flv, *.m4a, *.mp3) do ( echo converting "%%~nxv" ... ffmpeg -v quiet -i "%%v" "%2\%%~nv.mp3" set /A nfile+=1 ) echo Done! Converted %nfile% file(s)
To use it simply fire up a command prompt and type in:
rconvert G:\Music Z:\Fun\
Note that the trailing ‘\’ is necessary to indicate that Fun is a directory and not a file.
PS: I threw in a counter too just so you know how many files you’ve copied!
About this entry
You’re currently reading “Windows Batch file: How to copy and convert folders recursively with ffmpeg,” an entry on Mostly Buggy
- September 25, 2012 / 4:54 pm