Skip to content

Instantly share code, notes, and snippets.

@Sarjuuk
Last active May 17, 2021 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sarjuuk/d77b203f7b71d191509afddabad5fc9f to your computer and use it in GitHub Desktop.
Save Sarjuuk/d77b203f7b71d191509afddabad5fc9f to your computer and use it in GitHub Desktop.
[WIN] audio file conversion using ffmpeg - deletes after conversion
@echo off
echo ******************************************************************************************
echo This uses FFMPEG to convert and rename all of the files required for AoWoW sounds and
echo music. FFMPEG will loop indefinitely if we output a file of the same extension (MP3 to
echo MP3), so we just append an underscore there.
echo ******************************************************************************************
echo USE: Place this convert.cmd and ffmpeg.exe in the <localeCode>\Sound folder and run.
echo ******************************************************************************************
pause
REM ** converting music files
FOR /r %%f in (*.mp3) DO (
ffmpeg.exe -hide_banner -y -i "%%f" -f mp3 -acodec libmp3lame "%~d0%%~pf%%~nf.mp3_"
if not errorlevel 1 if exist "%~d0%%~pf%%~nf.mp3_" del /q /f "%~d0%%~pf%%~nf.mp3"
)
REM ** converting sound files
FOR /r %%f in (*.wav) DO (
ffmpeg.exe -hide_banner -y -i "%%f" -acodec libvorbis "%~d0%%~pf%%~nf.wav_"
if not errorlevel 1 if exist "%~d0%%~pf%%~nf.wav_" del /q /f "%~d0%%~pf%%~nf.wav"
)
@electricmessiah
Copy link

This script is BAD. It keeps renaming the files over and over again repeatedly adding the .mp3 extension until the filename is too long.

@Sarjuuk
Copy link
Author

Sarjuuk commented Jul 20, 2017

Using windows for web-related or automated tasks is ... questionable at best. This issue is an extension of this problem. (Because apparently it works in some cases but not all cases)

@electricmessiah
Copy link

electricmessiah commented Jul 20, 2017

It's not NIX, but Powershell can do most everything. This particular process doesn't work well in a FOR loop. You need to read in the file list for each folder and iterate over that instead of reading each file in the folder. When you do it this way, and write the files back with the same extension, it also processes the newly created files (over and over) because it doesn't know when to stop. Even if the extension is different, this script will append the new extension to the old one resulting in <file.wav.ogg> or <file.mp3.mp3>.

@electricmessiah
Copy link

Feel free to commit this batch file. It gets the job done cleanly and correctly.

`
@echo off
echo ******************************************************************************************
echo This uses FFMPEG to convert and rename all of the files required for AOWOW sounds and
echo music. FFMPEG wil loop indefinitely if we output a file of the same extension (MP3 to MP3),
echo so we temporarily REM rename it to MP4 and rename it back to MP3 later. WAV files are
echo converted to OGG. All files are renamed after both loops are finished and the file
echo extensions are preserved correctly.
echo ******************************************************************************************
echo USE: Place this convert.cmd and ffmpeg.exe in the enUS\Sound folder and run.
echo ******************************************************************************************
pause

REM ** Convert the MP3 to MP3 but name differently, delete source file, rename new file later
FOR /r %%f in (*.mp3) DO (
ffmpeg.exe -hide_banner -y -i "%%f" -f mp3 -acodec libmp3lame "%~d0%%~pf%%~nf.mp4"
del /F "%~d0%%~pf%%~nf.mp3"
)

REM ** Covert th WAV to OGG
FOR /r %%f in (*.wav) DO (
ffmpeg.exe -hide_banner -y -i "%%f" -acodec libvorbis "%~d0%%~pf%%~nf.ogg"
del /F "%~d0%%~pf%%~nf.wav"
)

REM Rename the temp MP4 extension back to MP3
FOR /r %%f IN (*.mp4) DO (
ren "%%f" *.mp3
)
`

@Magnifikator
Copy link

Magnifikator commented Aug 31, 2017

The install script looks for filepattern like : *.wav.ogg and *.mp3.mp3
So the converter script by @electricmessiah must be like that:

@echo off
echo ******************************************************************************************
echo This uses FFMPEG to convert and rename all of the files required for AOWOW sounds and
echo music. FFMPEG wil loop indefinitely if we output a file of the same extension (MP3 to MP3),
echo so we temporarily REM rename it to MP4 and rename it back to MP3 later. WAV files are
echo converted to OGG. All files are renamed after both loops are finished and the file
echo extensions are preserved correctly.
echo ******************************************************************************************
echo USE: Place this convert.cmd and ffmpeg.exe in the enUS\Sound folder and run.
echo ******************************************************************************************
pause

REM ** Convert the MP3 to MP3 but name differently, delete source file, rename new file later
FOR /r %%f in (*.mp3) DO (
ffmpeg.exe -hide_banner -y -i "%%f" -f mp3 -acodec libmp3lame "%~d0%%~pf%%~nf.mp4"
del /F "%~d0%%~pf%%~nf.mp3"
)

REM ** Covert th WAV to OGG
FOR /r %%f in (*.wav) DO (
ffmpeg.exe -hide_banner -y -i "%%f" -acodec libvorbis "%~d0%%~pf%%~nf.wav.ogg"
del /F "%~d0%%~pf%%~nf.wav"
)

REM Rename the temp MP4 extension back to MP3
FOR /r %%f IN (*.mp4) DO (
ren "%%f" *.mp3.mp3
)

@Sarjuuk
Copy link
Author

Sarjuuk commented Sep 3, 2017

%~d0%%~pf%%~nf what on earth is that variable....
whatever..

since i don't like reusing common file extensions (even if it shouldn't matter), lets just add an underscore.
and also, update the install script.

@opiums9
Copy link

opiums9 commented Feb 11, 2021

image
how fix it?

@opiums9
Copy link

opiums9 commented Feb 11, 2021

my version:

@echo off
echo ******************************************************************************************
echo This uses FFMPEG to convert and rename all of the files required for AoWoW sounds and
echo music. FFMPEG will loop indefinitely if we output a file of the same extension (MP3 to 
echo MP3), so we just append an underscore there.
echo ******************************************************************************************
echo USE: Place this convert.cmd and ffmpeg.exe in the <localeCode>\Sound folder and run.
echo ******************************************************************************************
pause

REM ** converting music files
FOR /r %%f in (*.mp3) DO (
ffmpeg.exe -hide_banner -y -i "%%f" -f mp3 -acodec libmp3lame "%~d0%%~pf%%~nf.mp3_"
if not errorlevel 1 if exist "%~d0%%~pf%%~nf.mp3_" del /q /f "%~d0%%~pf%%~nf.mp3"
)

REM ** converting sound files
FOR /r %%f in (*.wav) DO (
ffmpeg.exe -hide_banner -y -i "%%f" -acodec libvorbis "%~d0%%~pf%%~nf.ogg"
rename "%~d0%%~pf%%~nf.ogg" *.wav_
if not errorlevel 1 if exist "%~d0%%~pf%%~nf.wav_" del /q /f "%~d0%%~pf%%~nf.wav"
)

@qyh214
Copy link

qyh214 commented May 17, 2021

if use version 4.x ffmpeg ,suggest fix:

@echo off
echo ******************************************************************************************
echo This uses FFMPEG to convert and rename all of the files required for AoWoW sounds and
echo music. FFMPEG will loop indefinitely if we output a file of the same extension (MP3 to 
echo MP3), so we just append an underscore there.
echo ******************************************************************************************
echo USE: Place this convert.cmd and ffmpeg.exe in the <localeCode>\Sound folder and run.
echo ******************************************************************************************
pause

REM ** converting music files
FOR /r %%f in (*.mp3) DO (
ffmpeg.exe -hide_banner -y -i "%%f" -f mp3 -acodec libmp3lame "%~d0%%~pf%%~nf.mp3_"
if not errorlevel 1 if exist "%~d0%%~pf%%~nf.mp3_" del /q /f "%~d0%%~pf%%~nf.mp3"
)

REM ** converting sound files
FOR /r %%f in (*.wav) DO (
ffmpeg.exe -hide_banner -y -i "%%f" -f ogg -acodec libvorbis "%~d0%%~pf%%~nf.wav_"
if not errorlevel 1 if exist "%~d0%%~pf%%~nf.wav_" del /q /f "%~d0%%~pf%%~nf.wav"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment