# How to use Bash script to find .mov files recursively and convert them to .mp4 using ffmpeg

I'm working on a Mac system in a video company where I have to clean up a lot of folders and subfolders containing large intermediate video files.

So I've been looking for a way to batch convert many .mov files to h.264 .mp4 files using ffmpeg. I've found several codes to do a batch convert using a script similar to

for f in '*.mov';
do ffmpeg -i $f -c:v libx264 -f mp4 "${%f.mov}.mp4" ;
done


However this only works on the current folder the script is run in, while I'm looking for a way to find .mov files recursively, and batch convert them all.

At the moment, the solution that works for me feels a little brute-force; I'm just using find and execute ffmpeg;

 find . -name '*.mov' -type f -exec ffmpeg -i {} -c:v libx264 -f mp4 {}.mp4


However, One of the problems I'm having, is that I can't seem to substitute the .mov for .mp4 in this formatting, so the output will add .mp4 to the {}, with files ending up as *.mov.mp4

Besides that, I'm guessing this isn't the most secure or the nicest way of going about rendering a batch.

I was wondering if there is a more secure or less terminal-intense way of creating a batch for ffmpeg, that might even solve my naming problem, or a way to make the 'for f in *mov' command find videos recursively up to 5+ folders deep.

Any help would be greatly appreciated.

• Your title is poorly phrased. That is not a recursive conversion. You want a recursive search/find to help with a batch convert script. – jiggunjer Dec 17 '15 at 8:20
• You can freely edit your own posts but for your protection, this must be done under the original user account. It looks like you have created a second account, which will also interfere with your ability to comment within your thread and to accept an answer. See Merge my accounts to get your accounts merged, which will solve the problem. – fixer1234 Dec 30 '15 at 16:33
• Thank you for the help and added edit on the title. Strangely enough, the two accounts were both created under the same emailaddress, which made it quite confusing as to why they were separate in the first place ;) – JasonD Dec 30 '15 at 16:42

Here is a simple bash script that will do what you want (I think): 1) recursively find all MOV files and convert them, 2) properly replace the .mov file extension with a .mp4 file extension.

#!/bin/bash
mp4=$(echo "$mov"|sed -e 's|\.mov$|.mp4|i') if [ "$mov" == "$mp4" ]; then echo "Failed to generate unique MP4 filename for file$mov"
exit 1
fi
echo ffmpeg -i "$mov" -c:v libx264 -f mp4 "$mp4"
done < <(find . -type f -iname '*.mov')


As written, this won't actually do the conversion, it just echoes the ffmpeg command that will be run. Once you are sure it will do what you want, simply remove the "echo" from in front of the ffmpeg command.

• Note that on OS X you have to substitute the sed binary to make it work with the following command: brew install gnu-sed --with-default-names – Josep Alsina Jun 5 '17 at 23:56

does this help?

for i in find . -name "*.mov" -print0| xargs -0; do echo ffmpeg -i $i -c:v libx264 -f mp4${i%.mov}.mp4; done

remove the echo if you are satisfied with the ffmpeg command that is generated.

this takes into account filenames containing one or more spaces (via -print0 and xargs -0)

• "does this help?" Are you posting an answer that you haven't tested? – DavidPostill Mar 30 '16 at 18:00
• indeed, the comment was posted after having tested the 'file find' and 'replace mov with mp4', but i was not sure about the ffmpeg syntax as i don't use ffmpeg. also, the OP had a better find command (using -type f and -iname which i omitted). – nagu Mar 31 '16 at 13:47