Lithiumion
Lithiumion

Reputation: 13

Encoding audio files then renaming them with a bash script

I'm writing a Bash script that will run on Linux to convert/encode audio files

The conversion works fine so far, but when it comes to renaming the produced files according to a specific pattern that I have to stick with it produces errors.

A file with the name Audio-001.flac has to be renamed to 2552011-Unit05-Fil001.flac following the pattern: [Recording Date-Recording Unit ID-File ID]

The code I'm using for rename:

echo "Enter Recording Date, Recording Unit-ID, and File-ID Separated by SPACE"

read RD RU FID

echo "Your input is: $RD, $RU, $FD " >> /home/user-name/Music/User-Input.txt #This is for logging

mv $FLACS/*.flac $FLACS/${RD}-${RU}-${FID}.$flac

I'm getting an error: mv: target 22112011-Unit05-File0. is not a directory

Can I use find and sed?

find bar -iname "*.wav" -printf 'mv %p %p\n' \

   | sed 's/*\.wav$/${RD}-${RU}-${FID}\.flac/' \
   | while read l; do eval $l; done`

Upvotes: 0

Views: 433

Answers (2)

clt60
clt60

Reputation: 63902

You're trying mv more files into "one file".

mv $FLACS/*.flac $FLACS/${RD}-${RU}-${FID}.$flac

*.flac is expanded to more files. You cant move more files into one. When moving more files, the mv want as his last argument a directory.

You probably want:

mv $FLACS/some_one_file.flac  $FLACS/${RD}-${RU}-${FID}.flac

Upvotes: 0

clt60
clt60

Reputation: 63902

Nice breakdown of needed work, so:

1 - Parse a folder tree (full of .WAV files)

This can be done with several ways, form:

find WHERE -print | grep -i PATTERN #or
find WHERE -name ... print0 | xargs -0 -I% ....  #or the simple
echo ./**/*.wav

2 - use an open source tool such as flac and ffmpeg to convert all the .WAV files in that folder tree to .FLAC

Yes, youre right. The general syntax is

flac [<general-options>] [<format-options>] [<encoding options>] [inputfile [...]]

3- Place the .FLAC files in a newly created folder tree that is identical to the original

Not a hard task. In Linux you can make a directory with the mkdir command. You can get the identical tree from the find command (1). Probably will need to use the dirname command too. (here is other ways too, but dirname is nice)

4- when naming the new .FLAC and their respective folders it will add a unique identifier to the file names i.e folder named 2582001 contains the files 2582001-1.wav and 2582001-2.wav will be recreated and will contain two .FLAC files with the names 2582001-REC05-FIL1.FLAC and 2582001-REC05-FIL2.FLAC. This structure of the new file names is [Recording Date - Recording Unit ID - File ID].

Again not a hard task, you can use the following:

command > "${RecordingDate} - ${RecordingUnitID} - ${FileID}"

but not recommening this. Sure will be better name the files without spaces, for example:

command > "${RecordingDate}-${RecordingUnitID}-${FileID}"

if you want rename the file use the mv command, like:

mv oldfilename.ext "${RecordingDate}-${RecordingUnitID}-${FileID}.$newext"

Ps: if you want some more precise answer, try ask more precise. :)

Upvotes: 4

Related Questions