开发者

Run FFmpeg from Shell Script

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 i开发者_如何学Pythons 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


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.


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' \;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜