Convert MKV to MP4 in Bulk with Find and FFmpeg
This guide provides a straightforward solution for batch converting
multiple MKV video files into the MP4 format on Linux using a
combination of the find command and ffmpeg. By
leveraging these powerful command-line tools, you can automate the
conversion process across an entire directory tree while preserving
original video quality.
To convert your files efficiently, you will combine find
to locate the files and ffmpeg to handle the transcoding.
Because MKV and MP4 are both container formats that often hold the same
underlying video and audio codecs (like H.264 and AAC), you can usually
copy the streams without re-encoding them. This process is incredibly
fast and preserves the exact quality of the original file.
The Batch Conversion Command
Run the following command in the terminal from the root directory containing your MKV files:
find . -type f -name "*.mkv" -exec sh -c 'for f; do ffmpeg -i "$f" -c copy "${f%.mkv}.mp4"; done' _ {} +How the Command Works
find . -type f -name "*.mkv": This searches the current directory (.) and all of its subdirectories for files (-type f) that end with the.mkvextension.-exec sh -c '...' _ {} +: This safely passes the list of found MKV files to an inline shell script, allowing you to manipulate the filenames properly.for f; do ... done: This loops through each matched MKV file, assigning the current filename to the variable$f.ffmpeg -i "$f": Opens the current MKV file as the input.-c copy: This is the magic flag. It instructs FFmpeg to copy the video and audio streams directly into the new container without re-encoding them, saving immense amounts of time and CPU power."${f%.mkv}.mp4": This takes the original filename, strips the.mkvextension from the end, and appends.mp4to create the output file name.
Re-encoding the Video (When Stream Copying Fails)
If your MKV files contain older or unsupported codecs that do not play well inside an MP4 container, you will need to re-encode the video. Use the modified command below to compress the video using the widely compatible H.264 codec:
find . -type f -name "*.mkv" -exec sh -c 'for f; do ffmpeg -i "$f" -c:v libx264 -crf 23 -c:a aac "${f%.mkv}.mp4"; done' _ {} +In this variation, -c:v libx264 encodes the video to
H.264, -crf 23 manages the visual quality balance, and
-c:a aac converts the audio to standard AAC format. This
method takes longer than stream copying but ensures maximum
compatibility across all media players and devices.