[FFmpeg-user] Get sample rate in mp3 files

Mark Filipak markfilipak.noreply at gmail.com
Mon Feb 14 11:56:11 EET 2022


On 2022-02-06 20:25, CMG DiGiTaL wrote:
> Hi,
> I'm creating a bat file that will access a folder with several mp3 files. I
> would like to be able to select in this folder only the mp3s with a sample
> rate of 48000 khz and copy them to another folder where I will change from
> 48000 khz to 44100 khz. What command can I use to select only 48000kHz mp3
> from original folder to destination folder?  I will use ffmpeg in a batch
> file.
> 
> thanks
> clamarc

Clamarc and I have been corresponding off-list...

I learned something:
  If you include SETLOCAL ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS, then the exclamation point 
becomes poison. That's why the command script (aka batch program) failed if the MP3 had an 
exclamation point in its name.

You should be able to modify the simplified script below to suit your needs.

@ECHO OFF
CHCP 65001
SETLOCAL ENABLEEXTENSIONS
FOR %%a IN (*.mp3) DO (
   FOR /F "tokens=1,* usebackq" %%b IN (`ffprobe -report -show_error -i "%%a" -v error -show_entries 
stream^=sample_rate -of default^=noprint_wrappers^=1:nokey^=1`) DO (
   ffmpeg -report -i "%%a" -map 0:0 -acodec mp3 -b:a 320k -ar:a 44100 "%%~na(was_%%b)%%~xa")
   )
)
GOTO :EOF


Note: For 'Cuidado!.mp3', "%%~na(was_%%b)%%~xa" adds "(was_48000)" to the name of the new MP3.
'Cuidado!.mp3' is 48000 samples/second
'Cuidado!(was_48000).mp3' is 44100 samples/second.
If you create the new MP3 in another (target) directory, you won't need "%%~na(was_%%b)%%~xa".

Regards,
Mark Filipak.



More information about the ffmpeg-user mailing list