简体   繁体   English

持续扫描目录并在新项目上执行脚本

[英]Continuously Scan Directory and Perform Script on New Items

First, please forgive me and be easy on me if this question seems easy;首先,如果这个问题看起来很简单,请原谅我并放轻松; the first time I tried posting a question about another subject, I didn't provide enough information a few months ago.我第一次尝试发布关于另一个主题的问题时,几个月前我没有提供足够的信息。 My apologies.我很抱歉。

I'm trying to scan my incoming media folder for new audio files and convert them to my preferred format into another folder, without removing the originals.我正在尝试扫描传入的媒体文件夹以查找新的音频文件,并将它们转换为我喜欢的格式到另一个文件夹中,而不删除原始文件。

I've written the script below and while it seems to work for one-offs, I can't seem to get it to create the destination directory name based off the source directory name;我已经编写了下面的脚本,虽然它似乎可以一次性使用,但我似乎无法让它根据源目录名称创建目标目录名称; and I can't seem to figure out how to keep it looping, "scanning", for new media to arrive without processing what it's already processed.而且我似乎无法弄清楚如何让它循环,“扫描”,以便新媒体到达而不处理它已经处理的内容。

I hope this makes sense...我希望这是有道理的...

#! /bin/bash

srcExt=$1
destExt=$2

srcDir=$3
destDir=$4

opts=$5

# Creating the directory name - not currently working
#   dirName="$(basename "$srcDir")"
#   mkdir "$destDir"/"$dirName"

for filename in "$srcDir"/*.flac; do

    basePath=${filename%.*}
    baseName=${basePath##*/}
    
    ffmpeg -i "$filename" $opts "$destDir"/"$baseName"."$destExt"

done

for filename in "$srcDir"/*.mp3; do

    basePath=${filename%.*} 
    baseName=${basePath##*/}

    ffmpeg -i "$filename" $opts "$destDir"/"$baseName"."$destExt"

done

there are different ways of doing this, the easiest way might just be to look at the "modification date" of the file and seeing if it has changed, something like:有不同的方法可以做到这一点,最简单的方法可能是查看文件的“修改日期”并查看它是否已更改,例如:

#! /bin/bash

srcExt=$1
destExt=$2

srcDir=$3
destDir=$4

opts=$5

# Creating the directory name - not currently working
#   dirName="$(basename "$srcDir")"
#   mkdir "$destDir"/"$dirName"

for filename in ` find "$srcDir" \( -name '*.mp3' -o -name '*.flac' \)  -mmin -10`; do

    basePath=${filename%.*}
    baseName=${basePath##*/}
    
    ffmpeg -i "$filename" $opts "$destDir"/"$baseName"."$destExt"

done
  • Consider using mkdir -p which will a) create all necessary intermediate directories, and b) not complain if they already exist.考虑使用mkdir -p这将 a)创建所有必要的中间目录,并且 b)如果它们已经存在,则不要抱怨。

  • If you want the new items to be processesd immediately they arrive, look at inotify or fswatch on macOS .如果您希望新项目到达后立即进行处理,请查看macOS上的inotifyfswatch In general, if less urgent, schedule your job to run every 10 minutes under cron , maybe prefixing with nice so as not to be a CPU "hog" .一般来说,如果不那么紧急,请安排您的作业在cron下每 10 分钟运行一次,可能以nice为前缀,以免成为 CPU “猪”

  • Decide which files to generate by changing directory to the source directory and iterating over all files.通过将目录更改为源目录并遍历所有文件来确定要生成哪些文件。 For each file, work out what the corresponding output file should be according to your rules, test if it already exists, if not create it.对于每个文件,根据您的规则计算出相应的 output 文件应该是什么,测试它是否已经存在,如果不创建它。

Don't repeat all your for loop code like that, just do:不要像那样重复所有for循环代码,只需执行以下操作:

cd "$srcDir"
for filename in *.flac *.mp3 ; do
    GENERATE OUTPUT FILENAME
    if [ ! -f "$outputfilename" ] ; then
        mkdir -p SOMETHING
        ffmpeg -i "$filename" ... "$outputfilename"
    fi
done

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

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