Question

I have a bash script that converts *.mkv files to *.avi files. Here's what it looks like:

#!/bin/bash
for f in $(ls *mkv | sed ‘s/\(.*\)\..*/\1/’)
do
ffmpeg -i $f.mkv -sameq $f.avi
done

What I need this script to do however, is it needs to search recurssively in all folders for *.mkv files and then run the ffmpeg command and save the output to the same directory.

PLEASE can someone help me? :-)

Was it helpful?

Solution

Try like this:

find <file_path> -name '*.mkv' -exec sh -c 'mv "$0" "${0%%.mkv}.avi"' {} \;

OTHER TIPS

find /some/path -name '*.mkv' | while read f
do
    ffmpeg -i "$f" -sameq "${f:0:-4}.avi"
done
#!/bin/bash
find . -name "*.mkv" -exec ffmpeg -i {} -sameq `basename {} .mkv`.avi \;

Thanks to @Raul this is what worked for me and is the solution to what I wanted to do which is run recursively through directories and run the ffmpeg command on mkv files:

#!/bin/bash
find <file_path> -name '*.mkv' -exec sh -c 'ffmpeg -i "$0" -sameq "${0%%.mkv}.avi"' {} \;
exit;

Instead of ls *.mkv use find . -name "*.mkv".

This assumes no funny filenames (no spaces, newlines). Another possibility is using find in conjunction with xargs. The xargs manual makes for an instructive reading which will save your scripting life one day :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top