Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

bash - 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...