Link to home
Start Free TrialLog in
Avatar of IT CAMPER
IT CAMPERFlag for United States of America

asked on

Need a batch file to merge audio files using Sox

I need to use the SOX utility to loop through and merge a bunch of audio files. I have 81 unique audio files and need to merge each one with an intro audio tag that is a single file. For instance, I have audio files File1.mp3-File81.mp3 and need to merge the intro audio file Intro. mp3 with each one. The intro.mp3 needs to be placed at the beginning so likely needs to be listed first in the sox command. I think the basic sox command is "sox -m file1.wav file2.wav output_file.wav".  All 81 unique audio files and the intro file are combined into a single folder. So I am looking for a batch statement to loop each one with sox. I also need the batch to rename the output file as EPR5007.mp3, sequencing each merged file in increments of 1, so EPR5008, EPR5009, etc.
Avatar of Bill Prew
Bill Prew

So we will want to loop over ALL 81 files in the folder, but if the intro file is in the same folder then it would get processed at just another file also.  Is there a pattern that would find just the files to add it to, but not it?  Like File*.mp3?  If not, could it be moved to a different folder for this process?

Also, can the merged output files go to a new folder, that would make the most sense to me.


»bp
Avatar of IT CAMPER

ASKER

Yes, I can move the intro file and the outputs to subfolders.
If the files can be differentiated by a pattern, then here's a starting point for you.  Right now it is "test" mode, it won't do anything, but it will display to the console the SOX command lines it would execute.  If they look right, remove the ECHO from before the SOX command and run again for real.

@echo off
setlocal EnableDelayedExpansion

rem Define folders and files to work with
set BaseDir=B:\EE\EE29153650\Base
set DestDir=B:\EE\EE29153650\Dest
set IntroFile=B:\EE\EE29153650\Base\intro.mp3

rem Initialize file count
Set i=0

rem Loop through all matching files in base folder (adjust patter as needed)
for %%A in ("%BaseDir%\file*.mp3") do (
    rem Increment file number, and generate zero padded sequence for output file
    set /a i+=1
    set Seq=000!i!
    set Seq=!Seq:~-3!

    rem Merge intro and this file, save as new file with sequence number
    ECHO sox -m "%IntroFile%" "%BaseDir%\%%~nxA" "%DestDir%\EPR5!Seq!.mp3"
)

pause

Open in new window


»bp
I should also say, I'm not familiar with the SOX utility, so counting on you for the correct syntax of that command line...


»bp
Okay I have changed from mp3 to wav, not that that matters other than the batch changes for same. I have also batch renamed all of the base files to Base1.wav, Base2.wav, etc. I changed the folder locations in your batch and launched it from the root Sox folder where sox.exe exist but it doesn't launch anything. I removed Echo in front of the sox line. I also echoed on at the beginning to see what it was showing and did not see anything unusual. I can manually execute the sox statement, using the actual filenames for a single conversion and it works so we are good with the Sox syntax.

 Here is the batch I am testing.

@echo on
setlocal EnableDelayedExpansion

rem Define folders and files to work with
set BaseDir=C:\Sox\Base
set DestDir=C:\Sox\Out
set IntroFile=C:\Sox\Intro\intro.wav

rem Initialize file count
Set i=0

rem Loop through all matching files in base folder (adjust patter as needed)
for %%A in ("%BaseDir%\file*.wav") do (
    rem Increment file number, and generate zero padded sequence for output file
    set /a i+=1
    set Seq=000!i!
    set Seq=!Seq:~-3!

    rem Merge intro and this file, save as new file with sequence number
    sox --combine concatenate "%IntroFile%" "%BaseDir%\%%~nxA" "%DestDir%\EPR5!Seq!.wav"
)

pause
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worked perfectly! Another in the books! Thanks Bill!
Welcome, glad that worked out.


»bp