Converting .cda to .mp3 in a bash script while keeping original name

522 Views Asked by At

I am trying to write a shell script that takes files from a cd drive and moves them onto an external USB drive. I am running it on windows 11 in the git shell for windows.
Part of the script is to change the .CDA files from the cd drive to a .mp3 file and I have not been able to get this to work.

This is the best answer I found but it does not work:
ffmpeg -i *.cda *.mp3
This Is the rest of my script if it helps

cd /d/
mkdir $1
cd /e/
cp *.cda /d/$1
cd /d/$1
ffmpeg -i *.cda *.mp3
ls
1

There are 1 best solutions below

1
uncletall On

You can do this simply using a for loop and variable substitutions to extract the filename without extension. See below the help for relevant output.

You loop code:

for %f in (*.cda) do ffmpeg -i %f %~nf.mp3

Here the %~nf will use the filename, without extension, in the variable f and you can append your extension

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string