简体   繁体   English

如何将代码应用于给定扩展名的文件

[英]How to apply code to a file of a given extension

I've been messing around with Apple's Automator & Quick Actions, and have run into a snag.我一直在使用 Apple 的 Automator & Quick Actions,但遇到了障碍。 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.我似乎无法弄清楚如何在这种情况下使用 if 语句。 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!希望这是有道理的,并提前感谢您!

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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM