Page 1 of 1

Batch script convert to MP3 with troublesome filenames: apostrophe, comma, ampersand etc.

Posted: 31 Aug 2021 20:12
by gmontag451
Batch script processing with VLC runs into trouble converting files which contain commas, apostrophes, ampersands, and other symbols in their filenames:
  • The resulting filename might be truncated at the troublesome character:
    "Having My Cake & Yeeting It Too.flac" becomes
    "Having My Cake "

    Or in some circumstances extra characters from the batch script appear after the file extension:
    "Troublesome Filename.mp3}"
I've tried many scripts which use formulas to replace characters or add escape characters to the filename, but none of them work 100%, and many are too convoluted to easily understand.

I finally said enough is enough, and decided to figure this out once and for all. The best method I've found is to use a temporary filename and rename it after VLC is done converting. So here's the batch script I use to convert FLAC to MP3, which will also copy the FLAC tags to the MP3's ID3 tags if ffmpeg is installed:

Code: Select all

REM ---- Loop for each file ---- FOR %%A IN (%*) DO (CALL :SUB_VLC %%A) REM *** In the event of errors, remove below REM to keep the console window open after processing: REM PAUSE GOTO :eof :SUB_VLC REM Note: _TEMP_A.mp3 will be overwritten by VLC if it exists REM ---- Convert file to MP3 with VLC at 192kbps ---- REM *** Update path to VLC.exe if needed, and change 192 to 320 if desired "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" -I dummy %1 --sout=#transcode{acodec=mp3,ab=192,vcodec=dummy}:standard{access=file,mux=raw,dst="_TEMP_A.mp3"} vlc://quit REM ---- Copy FLAC tags to MP3 ID3 tags ---- REM *** ffmpeg must be installed. If it doesn't run from this script, it probably isn't "on the PATH". Either you will need to replace "ffmpeg" with the path to ffmpeg.exe (use quotes around the path if it contains spaces) *OR* add ffmpeg to the PATH System Variable. ffmpeg -y -i %1 -map_metadata 0 -id3v2_version 3 "%~dp1_TEMP_A.mp3" REM ---- Rename file ---- SET _outfile="%~n1.mp3" REN "%~dp1_TEMP_A.mp3" %_outfile% GOTO :eof REM Some code ideas borrowed from VL22
Save it as a batch file (ie. "Convert-to-MP3.bat") and read the *** remarks. Then you can drag and drop one or more FLAC files on it to convert them to MP3s. This script is pretty easy to understand so that it can be revised to work with other conversions. I apologize that I didn't keep track of where I got some of these ideas and so I can't credit the proper people.

I hope this will be helpful!
Michael

PS If you have ffmpeg installed, you could just leave VLC out entirely, and no need to worry about those pesky symbols:

Code: Select all

FOR %%A IN (%*) DO (CALL :SUB_MP3 %%A) REM *** In the event of errors, remove below REM to keep the console window open after processing: REM PAUSE GOTO :eof :SUB_MP3 SET _outfile="%~n1.mp3" REM ---- Convert file to MP3 at 192kbps, keeps FLAC metadata ---- REM *** Change 192 to 320 if desired ffmpeg -i %1 -ab 192k -map_metadata 0 -id3v2_version 3 %_outfile% GOTO :eof
Cross-posted to Reddit/vlc