Linux: Batch Convert .avi files to .mp4/.mkv

I’ve been trying to clean up my video library since building my latest NAS. In the process, I found a number of .avi files, which is an older file format that isn’t widely used these days. While every time a file is converted it loses some of its quality, I was willing to risk the slight quality reduction to convert the remaining few files I had to convert into more modern file formats.

I initially tried converting the files using HandBrake. But given the number I needed to convert, I decided pretty quickly that I needed a faster method for this. Enter stackoverflow.

Assuming you have all of your .avi video files in a single directory, navigate to that directory in a terminal and you can use the following single line of code to iterate through all of the .avi files and convert them to .mp4 files:

for i in *.avi; do ffmpeg -i "$i" "${i%.*}.mp4"; done

In case you’re interested, the code is a loop. The first part starts the loop (“for i in *.avi;”), telling the computer to look for every file with the file extension .avi. The second part tells the computer what to do with every file with that extension – convert it to a .mp4 file with the same name. The last piece indicates what to do when the loop is completed – done.

Of course, this code could also be used to convert any other file format into a different format by replacing the .avi or .mp4 file extensions in the appropriate places. For instance, to convert all the .avi files to .mkv, the code would look like this:

for i in *.avi; do ffmpeg -i "$i" "${i%.*}.mkv"; done

Or if you wanted to convert a bunch of .mp4 files into .mkv files, you could do this:

for i in *.mp4; do ffmpeg -i "$i" "${i%.*}.mkv"; done


If you have .avi files in a number of subfolders, you’ll want to use this script:

find . -exec ffmpeg -i {} {}.mp4 \;

To use it, navigate in a terminal to the top-level folder, then execute this command. It will search through all the subfolders, find all the files in those subfolders, and convert them all to .mp4 files.

Of course, if you have a mixture of file types in the folders, you’ll want a variation of this command that searches for just the files of a certain type. To do that, use this command:

find . -name *.avi -exec ffmpeg -i {} {}.mp4 \;

This command will find all the files with the extension .avi and convert them all to .mp4 files using ffmpeg.

And, if you’re feeling particularly adventurous, you could search for multiple file types and convert them all:

find . -name *.avi -or -name *.mkv -exec ffmpeg -i {} {}.mp4 \;

This code would find every file with the extension .avi or .mkv and convert it to .mp4 using ffmpeg.

NOTE: This command uses the default conversion settings of ffmpeg. If you want more fine-tuned conversions, you can always check the options available in converting video files using ffmpeg.


If you want to specify the codec to use in converting the files, that is a little more complicated. For instance, if I want to use H265 instead of H264 as my codec, I could use the following code to convert all of my .avi files in a folder into .mkv files with H265 encoding:

for i in *.avi; do ffmpeg -i "$i" -c:v libx265 -crf 26 -preset fast -c:a aac -b:a 128k "${i%.*}.mkv"; done

The default setting in ffmpeg for audio is to pass it through. Thus, if you wanted to just convert the video to a new codec but leave the audio, you could use the following command:

for i in *.avi; do ffmpeg -i "$i" -c:v libx265 -crf 26 -preset fast "${i%.*}.mkv"; done

This will convert the video to H265 but retain whatever audio was included with the video file (the default is to take the audio with the highest number of channels).

Additional information on the various settings for H.265 is available here. Some quick notes: the number after “-crf” is basically an indicator of quality, but is inverted. Lower numbers improve the quality; higher numbers reduce the quality. Depending on what I’m encoding, I vary this from 24 (higher quality) to 32 (lower quality). This will affect the resulting file size. If time is not a concern, you can also change the variable after “-preset.” The options are ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, and veryslow. Slower encodes will result in better quality output but it will take substantially longer to do the encode.

If you run into the problem where you are trying to do bulk conversions but the names of the videos you are converting have spaces in them, you may get the error: “find: paths must precede expression.” The solution is to put the pattern in single quotes, like this:

find . -name '*.mkv' -exec ffmpeg -i {} -c:v libx265 -crf 26 -preset fast {}-new.mkv \;

 3,521 total views,  16 views today

Linux – 360 degree video editing on Linux

I recently purchased a 360-degree camera that I have used a few times. It takes 360-degree spherical photos and also films in 360 degrees. It’s not the most expensive such camera, but it does a decent job shooting 360-degree panoramic videos.

However, I have run into a couple of problems with the resulting footage on Linux.

First, as is the case with pretty much any footage shot on your phone, it can be bumpy and really needs to be stabilized. I addressed this to some degree by purchasing a 3-axis gimbal, which minimizes the need for stabilization. However, the software I typically use on Linux for editing videos – Kdenlive – doesn’t do a great job stabilizing 360-degree video. Additionally, Kdenlive doesn’t have a profile for 360-degree video and simply recognizes it as an equilateral rectangular clip (1920×960, 27.90fps). It’s still possible to edit the video using Kdenlive, but it treats it as just a wide angle clip and not as 360-degree video. Otherwise, Kdenlive edits the video as if it was any normal video clip.

Second, when you render an edited 360-degree video file in Kdenlive, it loses the tag necessary to tell youtube (or a desktop player; see below) that the video is 360-degree footage. As a result, it’s necessary to re-add that tag before the video can be played back on your computer or uploaded to youtube. There is a python script that can do this but it only works under Windows or Mac. It was released by Google and the GUI version is available here. I run it on a virtual machine and it works fine. It would be nice to be able to simply edit the necessary tags in the video using something like VLC or FFMPEG or a command line in Linux, but I have been unable to find directions that explain how to do that at this point.

Third, there are issues with playback. Until just recently (as in the end of 2017), playback of 360-degree videos on Linux was not really possible. There wasn’t a video player that had this option. However, as of the 3.0.0 version of VLC, 360-degree video playback is now possible. (As a bonus, VLC can also open 360-degree images as well. I love VLC.) Of course, VLC 3.0.0 doesn’t ship with most current Linux distributions, so you’ll have to install it from their PPA nightly branch. With VLC 3.0.0, it is now possible to video 360-degree video on Linux.

What is really needed to streamline this process on Linux? If the awesome folks running Kdenlive could create a 360-degree video profile (at least as a rendering option) that includes the necessary spherical tag, that would mean I could skip step 2 above entirely. That would be awesome. I’d happily donate some money to them if they could make that happen.


 4,766 total views,  5 views today