Recursively convert mkv to mp4 with avconv

3

1

I need to create a script that can convert all .mkv files to .mp4 in a starting directory and all sub-directories under it.

I've been able to convert files one at a time by using:

avconv -i input.mkv -codec copy output.mp4

and converting all files in the current directory wouldn't be too difficult. But after hours of searching, I can't find a way to do this recursively.

I've gathered that I need to use the find command, but I'm relatively new to Linux and I get completely lost in combining find and avconv to accomplish what I need.

Brandon Hood

Posted 2014-10-27T06:35:03.913

Reputation: 45

Answers

4

You need to enable recursive globbing in Bash:

shopt -s globstar

Then, a simple loop, replacing the output filename:

for f in **/*.mkv; do avconv -i "$f" -c copy "${f%.mkv}.mp4"; done

Note that this may fail on videos that contain codecs MP4 containers cannot handle. MP4 is more restrictive than MKV, which can basically include all codecs.

slhck

Posted 2014-10-27T06:35:03.913

Reputation: 182 472