0

I created a batch file that will read an audio in a folder, normalize the LUFS of the audio and generate the output audio in the same folder with the modified name, keeping the original file.

The command I use to generate the normalized file, without using copy, with another name in the same folder is:
"..\Áudios LUFS\!filename!_LUFS_!vLUF!.flac"

eg.

Original file  : Music.flac
Normalized file: Music_LUFS_-10.0.flac

Note. I use the variable "vLUF" to compose the name of the normalized file.

It turns out that if I run the command again, the Music_LUFS_-10.0.flac file remains in the folder and a new file is created.
eg.
Music_LUFS_-10.0_LUFS_-10.0.flac

so in the folder are two files with the same content, but with different names:

Music_LUFS_-10.0.flac
Music_LUFS_-10.0_LUFS_-10.0.flac 

I'm trying to change the batch file so that it doesn't save the file if there is already one with the same name in the folder, but I'm having some difficulties and doubts.

Batch file changed to avoid saving the file when the same name already exists in the folder.

Start of the batch, I define variables but the ones inside the FOR command do not show the value in the ECHO, only the ones defined outside the FOR show the value correctly:

@echo off
chcp 65001
cls
ENDLOCAL
SETLOCAL ENABLEEXTENSIONS

cd\Users\%username%\Desktop\Áudios LUFS
SET vLUF=-10.0
SET vPEAK=-0.5
SET previousfile=""

FOR %%a IN (*.flac) DO ( 
  SET "file=%%a" 
  SET "filename=%%~na"
  ECHO %file%     CURRENT READ FILE
  ECHO %filename% JUST NAME CURRENT READ FILE
  
  ECHO %vLUF%   LUF VALEU
  ECHO %vPEAK%  PEAK VALUE
  ECHO %previousfile%  PREVIOUS FILE

Below, the terminal screen with the ECHO's in the variables. Note that only variables defined outside FOR have their values displayed correctly:

Echo variables

Below, comparison IF, inside FOR, to not generate the file when it already exists in the folder. Note at the end that I try to update the "previousfile" variable with the name of the generated file, before FOR reads the next file:

  If "%file%" NEQ "%previousfile%" (
  ffmpeg -hide_banner -i "%%a" -af "[0:a]loudnorm=print_format=summary" -f null NUL 2> "%%~na.log"
  @FOR /F "tokens=3" %%b IN ('FINDSTR /C:"Input Integrated" "%%~na.log"') DO (SET II=%%b)
  @FOR /F "tokens=4" %%b IN ('FINDSTR /C:"Input True Peak" "%%~na.log"') DO (SET ITP=%%b)
  @FOR /F "tokens=3" %%b IN ('FINDSTR /C:"Input LRA" "%%~na.log"') DO (SET ILRA=%%b)
  @FOR /F "tokens=3" %%b IN ('FINDSTR /C:"Input Threshold" "%%~na.log"') DO (SET IT=%%b)
  @FOR /F "tokens=3" %%b IN ('FINDSTR /C:"Target Offset" "%%~na.log"') DO (SET TO=%%b) 
  DEL "%%~na.log"
  SETLOCAL ENABLEDELAYEDEXPANSION
  ECHO !II!   Input Integrated
  ECHO !ITP!  Input True Peak
  ECHO !ILRA! Input LRA
  ECHO !IT!   Input Threshold
  ECHO !TO!   Target Offset
  FOR /F "tokens=1,2 delims=," %%b IN ('ffprobe -v 0 -select_streams a -show_entries "stream=sample_fmt,sample_rate" -of "csv=p=0" "!filename!.flac"') DO (
  ffmpeg -hide_banner -y -i "!filename!.flac" -af "loudnorm=linear=true:I=!vLUF!:LRA=11:tp=!vPEAK!:measured_I=!II!:measured_LRA=!ILRA!:measured_tp=!ITP!:measured_thresh=!IT!:offset=!TO!:print_format=summary" -c:v copy -id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" -map 0:0 -c:a flac -sample_fmt:a %%b -ar:a %%c "..\Áudios LUFS\!filename!_LUFS_!vLUF!.flac"
  SET "previousfile=!filename!_LUFS_!vLUF!.flac"
  )
 )
  ENDLOCAL
)

A note: In the command I use to generate the output file,"..\Áudios LUFS\!filename!_LUFS_!vLUF!.flac", is there any parameter to make the file that already exists in the folder be replaced and not created with another name?

1 Answer 1

1

I think you are making this more complicated than it has to be, just skip the files that already have LUFS in the name inside the for.

Instead of:

FOR %%a IN (*.flac) DO (

you use:

for /f "delims=" %%a in ('dir /b *.flac ^|find /v /i "_LUFS_"') do (
1
  • This works perfectly!... and I'm freaking out over record breaking logic!. Thanks.
    – Clamarc
    Commented Apr 7, 2022 at 22:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .