views:

238

answers:

2

Hello all,

I have found a useful shell script that shows all files in a directory recursively.

Where it prints the file name echo "$i"; #Display File name.

I would instead like to run an ffmpeg command on non MP3 files, how can I do this? I have very limited knowledge of shell scripts so I appreciate if I was spoon fed! :)

//if file is NOT MP3

ffmpeg -i [the_file] -sameq [same_file_name_with_mp3_extension]

//delete old file

Here is the shell script for reference.

DIR="."

function list_files()
{
    if !(test -d "$1")  
    then echo $1; return;
    fi

    cd "$1"
    echo; echo `pwd`:; #Display Directory name

    for i in *
    do
               if test -d "$i"  #if dictionary
                then  
                          list_files "$i" #recursively list files
                  cd ..
                else
            echo "$i"; #Display File name
                fi

    done
}

if [ $# -eq 0 ]
then list_files .
exit 0
fi

for i in $*
do
    DIR="$1"
    list_files "$DIR"
    shift 1 #To read next directory/file name
done
+2  A: 

You can do the same with a find one-liner. Assuming the files you want to process are all wav:

find /path/ -type f -name "*wav" -exec ffmpeg -i {} -sameq {}.mp3 \;

If you want to find "rm" files, and delete them after conversion:

find /path/ -type f -name "*.rm" -exec ffmpeg -i {} -sameq {}.mp3 && rm {} \;

That said, if you want to do it with the shell script you showed, take the line that says

echo "$i";

replace it with this:

ffmpeg -i "$i" -sameq "$i".mp3

$i is a variable. A few lines up, you have:

for i in *

this basically means "for every element in * (which in turn stands for all files in the current directory, it's what's called a "shell expansion"), put the name of the element/file in the variable i, and then execute all the code between "do" and "done" ". So for each iteration, i will contain the name of one of the files in this directory.

There's also a section that tests whether i is a directory and if so, it recursively lists its contents.

A quick final note: the \; at the end of the find command IS significant and it NEEDS to have a space before the backslash, otherwise it won't work.

Roadmaster
They are actually all rm files. Does that one line find the file, converts it and deletes the old one?!!
Abs
I've updated the answer to do what you ask here. Please test it with a few, disposable files before running the batch on your production directory - I take no responsibility for deleted files :)
Roadmaster
@Roadmaster - is that `rm` command deleting the old file (the .rm file)? Btw, I really appreciate your explanations, its making things really clear.
Abs
Roadmaster
That's brilliant! Thanks Roadmaster, I'll try it on a few files first and test before using on the real files!
Abs
A: 

Your shell script seems to be essentially ls -1R, so it's probably easier to just use that. As for running ffmpeg on non-MP3 files, it's probably easier to use find instead of writing a whole shell script to do it. Assuming you're identifying MP3 files by their extension:

find your-path -not -name "*.mp3" -exec ffmpeg -i '{}' -sameq '{}.mp3' \;
Michael Mrozek