Home > Back-end >  How to apply code to a file of a given extension
How to apply code to a file of a given extension

Time:08-07

I've been messing around with Apple's Automator & Quick Actions, and have run into a snag. I want to be able to engage a script that will reverse every audio file in that folder (and paste the reversed audio into a new folder). I want it to be able to work with a range of audio types, and that's my problem. I can't seem to figure out how to work with if statements in this context. I'm very new to terminal commands, so it could be that I'm doing something completely wrong. The quick action will run without throwing up any errors, but all it does is make the new folder.

for f in "$@"
do
    cd "$f"
    mkdir reversed

    if [[ f == *.m4a ]];
    then
        /opt/homebrew/bin/ffmpeg -nostdin -i "$name" -af areverse -c:a alac -c:v copy "reversed/${name%.*}.m4a"
    elif [[ f == *.aiff ]];
    then
        /opt/homebrew/bin/ffmpeg -nostdin -i "$name" -af areverse -c:a pcm_s16le -c:v copy "reversed/${name%.*}.aiff"
    elif [[ f == *.mp3 ]];
    then
        /opt/homebrew/bin/ffmpeg -nostdin -i "$name" -af areverse -c:a mp3 -c:v copy "reversed/${name%.*}.mp3"
    fi  
done

Hopefully this makes sense, and thank you in advance!

CodePudding user response:

First, you are matching a literal string against the patterns, not the value of the parameter. Second, you are trying to match the directory name, not names of files in the directory. Something like

process () {
    /opt/homebrew/bin/ffmpeg -nostdin -i "$1" -af areverse -c:a "$2" -c:v copy "reversed/${1%.*}".$3
}

for d in "$@"; do
    cd "$d"
    mkdir reversed

    for f in *; do
        case $f in
           *.m4a) process "$f" alac m4a ;;
           *.aiff) process "$f" pcm_s16le aiff ;;
           *.mp3) process "$f" mp3 mp3 ;;
        esac
    done
done  
  • Related