I have a bash script that takes 1 argument (a file) and runs an ffmpeg command to create a duplicate of the file encoded with a different codec.
#!/bin/bash
ffmpeg -i "$1" -vn -acodec aac "$(basename "${1/.wav}").aac"
I just want to modify this bash script so instead of taking an argument, it instead just checks for all files in the directory to see if the re-encoded file already exists, and if it does not, creates it. Does anyone know how to do this?
Thanks for your help
CodePudding user response:
Assuming you just want to process all files ending in "*.wav" in a directory. In your bash script you can use find
like find . -type f -name "*.wav"
and or ls
with grep
like ls -1 | grep "*.wav"
to get the specific codec file names. Then you combine this with something like sed
or cut
to drop the file extensions and just check if the same file exists with the different codec for each file name. In this case, for example, you can modify your script to:
#!/bin/bash
for file in $(ls -1 | grep ".wav" | sed -e 's/\.wav$//'); do
if [[ ! -f "${file}.aac" ]]; then
ffmpeg -i "$file" -vn -acodec aac "$(basename "${file/.wav}").aac"
fi;
done;
This will create the file with the different codec if it does not exist.
Edit:
From the feedback received in the comments, I think a better answer would be using globbing to allow the shell to expand patterns into a list of matching filenames like this:
#!/bin/bash
for file in ./*.wav; do
[ -e "$file" ] || continue # check if any file exists
if [[ ! -f "${file%????}.aac" ]]; then
ffmpeg -i "${file%????}" -vn -acodec aac "$(basename "${file}").aac"
fi;
done;
CodePudding user response:
Emm, Try
#!/bin/bash
#construct filename
filename="${1/.wav}".aac;
#check file exists
if [[ -f $filename ]];
then
echo "Em, File "$filename" Exists :)";
else
ffmpeg -i "$1" -vn -acodec aac "${1/.wav}".aac
echo "Finished Processing..$1..Done"
fi;