I'm quite new to the world of FFmpeg and video editing tools, but I essentially need to change the hash of a large set of videos that are organized in folders and subfolders and the easiest way I've come into is remuxing and/or re-encoding the video. Currently, I am using the program HandBrake to bulk re-encode videos in each folder from a .mp4 to a .mkv and that has done the trick. However, it is practically infeasible to do this for every single subfolder with hundreds of video files in each folder.

I have used FFmpeg briefly before and I'm wondering how I can best automate FFmpeg to search through each folder and its subfolders, find each .mp4 video, re-encode it to a .mkv, and then save it under the same name and path as the original video.

I've been reading through FFmpeg documentation to determine how this can be done, but I'm wondering if anyone here is experienced enough in FFmpeg to lend me a helping hand. Thank you very much

  • ffmpeg can do rewrapping to mkv. But for telling it which files to rewrap and how to name the new output files, you would use your shell's commands or a small script. Specify your OS, since the available shell's are different in Windows or in Mac/Linux.
    – mivk
    Aug 14, 2021 at 21:55
  • Sorry about that. I'm on Windows 10! Aug 15, 2021 at 19:48
  • On Windows, you have cmd.exe and powershell. In Mac/Linux, it would be something like find . -name "*.mp4" | while read f; do ffmpeg -i "$f" .... "${f%%mp4}mkv"; done. So you have to find the equivalent syntax for one of the Windows shells.
    – mivk
    Aug 15, 2021 at 21:27

1 Answer 1


If you are on Windows use WSL (it might be Ubuntu) and then something similar as mivik mentioned.

cd TOP_DIRECTORY; find . -type f -iname "*.mp4" -exec ffmpeg -y -i '{}' -c:a copy -c:v libx264 -preset fast -profile:v baseline -pix_fmt yuv420p '{}.mkv' ;

  • 1
    I ended up using WSL and Ubuntu and used the following command after going to the correct directory: find . -name ".mp4" -exec sh -c 'ffmpeg -i "$1" -codec copy "${1%.}.mkv" -hide_banner' sh {} \; I had a problem where I was getting double extensions (filename.mp4.mkv) and found the following code online. It did the trick (I hope!) Thanks so much for your help! Aug 16, 2021 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.