help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] help - optimization wanted for little script - batch con


From: Greg Wooledge
Subject: Re: [Help-bash] help - optimization wanted for little script - batch converting audio files
Date: Thu, 10 Jul 2014 14:02:55 -0400
User-agent: Mutt/1.4.2.3i

On Thu, Jul 10, 2014 at 04:28:52PM +0200, Luigi Rensinghoff wrote:
> What do i need to do / where to read to adapt it in a way, that it can
> 
> a) search multiple folders and subfolders
> 
> b)  handle folder - and filenames with spaces or other special characters....

The second question is the most critical one.  Your script as written
has a MASSIVE problem with missing quotes.

> #!/bin/sh
> 
> for f in *C.aif;
> do
> #echo "Processing $f"
> prefix=${f%}
> pre=`echo $prefix | sed 's/.\{6\}$//'`
> L=`echo $pre-\(L\).aif`
> R=`echo $pre-\(R\).aif`
> C=`echo $pre-C.aif`
> LFE=`echo $pre-\LFE\.aif`
> Ls=`echo $pre-S\(L\).aif`
> Rs=`echo $pre-S\(R\).aif`
> out=`${pre}_6ch.wav`
> 
> ffmpeg -i $L -acodec pcm_s24le -i $R -acodec pcm_s24le -i $C -acodec 
> pcm_s24le -i $LFE -acodec pcm_s24le -i $Ls -acodec pcm_s24le -i $Rs -acodec 
> pcm_s24le -filter_complex 
> "[0:a][1:a][2:a][3:a][4:a][5:a]amerge=inputs=6[aout]" -map "[aout]"  
> ${f%-C.aif}_6ch_24bit.wav

All of those parameters expansions (like $L and $R) should be "double
quoted".  Without double quotes, bash splits their contents into multiple
words, and then does filename expansion (globbing) on each word.  This is
not what you want.

Try this:

pre="${f%-C.aif}"
L="$pre-(L).aif"
...
ffmpeg -i "$L" -acodec ...

Now, the first question is less important, but harder to answer:

> a) search multiple folders and subfolders

What I would personally recommend is to put the per-file processing
code into a function, and then call that function for each file.
Use GNU/BSD find with -print0 to feed the filenames to bash for
processing.

process() {
    pre="${1%-C.aif}"
    L="$pre-(L).aif"
    ...
    ffmpeg -i "$L" -acodec ...
}

while IFS= read -r -d '' file; do
    process "$file"
done < <(find . -type f -name '*-C.aif' -print0)

Of course you could skip the function, and simply drop the processing
code directly into the while loop, if you prefer.  Or you could skip the
while loop and write the entire processing code as a great big string
which you call with -exec bash -c '...' _ {} \;

But I think the function + while loop + process substitution is the
cleanest approach.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]