ffmpeg to convert MP4 files to MKV files with the libx265 video codec

When working with video files, there are times when you need to convert them from one format to another or modify them in some other way. One of the most popular tools for this is ffmpeg. This command-line tool can do a lot of things related to video processing, including conversion, resizing, cropping, and more. In this blog post, we will explain a script that uses ffmpeg to convert MP4 files to MKV files with the libx265 video codec.

The Script:

Here’s the script that we will be explaining:

for FILE in *.mp4; do
  echo -e "Processing video '\e[32m$FILE\e[0m'";
  ffmpeg -i "${FILE}" -analyzeduration 2147483647 -probesize 2147483647 -c:v libx265 -an -x265-params crf=0 "${FILE%.mp4}.mkv";

Let’s break this script down line by line to understand what it does.

for FILE in *.mp4; do

This line starts a loop that goes through all the files in the current directory that have the “.mp4” extension. The loop will execute the commands inside the “do” and “done” keywords for each file that matches this pattern.

echo -e "Processing video '\e[32m$FILE\e[0m'";

This line uses the “echo” command to print a message to the console. The message includes the file’s name being processed, which is stored in the $FILE variable. The “\e[32m” and “\e[0m” are escape sequences that change the color of the text to green. This is just a way to make the message more visually appealing.

ffmpeg -i "${FILE}" -analyzeduration 2147483647 -probesize 2147483647 -c:v libx265 -an -x265-params crf=0 "${FILE%.mp4}.mkv";

This line runs the ffmpeg command to convert the current file from MP4 to MKV format using the libx265 video codec. Let’s break down each of the options:

  • “-i ${FILE}” specifies the input file. “${FILE}” is the name of the file being processed, which is stored in the $FILE variable.
  • “-analyzeduration 2147483647” and “-probesize 2147483647” are options that tell ffmpeg to analyze the entire file before starting the conversion process. This can help avoid some issues that can occur when processing large files.
  • “-c:v libx265” specifies the video codec for the output file. libx265 is a popular video codec that provides good quality at a smaller file size.
  • “-an” specifies that there should be no audio in the output file.
  • “-x265-params crf=0” sets the quality level for the video. A value of 0 means lossless compression, which is the highest quality possible.
  • “${FILE%.mp4}.mkv” specifies the name of the output file. “${FILE%.mp4}” removes the “.mp4” extension from the input file name, and “.mkv” adds the “.mkv” extension to the end.


The script we’ve just explained is a simple example of how you can use ffmpeg to convert MP4 files to MKV files with the libx265 video codec. It uses a loop to process all the files in the current directory that match the “.mp4” pattern. The script also prints a message to the console for each file

Compiling ffmpeg with NVIDIA GPU Hardware Acceleration on Ubuntu 20.04LTS

Please note that the following commands were executed on a system that already had CUDA support so we might be missing a step or two to enable NVIDIA CUDA support.

Install necessary packages

sudo apt-get install build-essential yasm cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-dev nvidia-cuda-toolkit;

Clone and install ffnvcodec

git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git;
cd nv-codec-headers;
sudo make install;
cd -;

Clone and compile FFmpeg’s public GIT repository with NVIDIA GPU hardware acceleration

git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/;
cd ffmpeg;
./configure --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --disable-static --enable-shared;
make -j 8;
sudo make install;


After performing the above steps, we were able to process media using ffmpeg without stressing our CPU! The workload was transferred to the GPU!

FFmpeg tiny cheat sheet

We assume that the user has set the video filename to the variable named $video in the following commands.

FFmpeg export audio from any video to mp3

ffmpeg -i "$video" -vn -c:a libmp3lame -y "$audio";

FFmpeg export frames from video to images

ffmpeg -i "$video" "$frames_folder/%08d.ppm";

Retrieve the frame rate from the input video

#To view it on screen
ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "$video";
#To assign it to a variable use the following
frame_rate=`ffprobe -v 0 -of csv=p=0 -select_streams v:0 -show_entries stream=r_frame_rate "$video"`;

To create a video out of a folder with frames/images and an audio file.

ffmpeg -framerate "$frame_rate" -i "$frames_folder/%08d.ppm" -i "$audio" -pix_fmt yuv420p -acodec copy -y "$output_video";
#To set a custom starting index for the frames you can use the -start_number argument
ffmpeg -start_number 62 -framerate "$frame_rate" -i "$frames_folder/%08d.ppm" -i "$audio" -pix_fmt yuv420p -acodec copy -y "$output_video";
#To use the MP4 coded use -vcodec libx264
ffmpeg -framerate "$frame_rate" -i "$frames_folder/%08d.ppm" -i "$audio" -vcodec libx264 -pix_fmt yuv420p -acodec copy -y "$output_video";

To merge an audio less video with an audio file

ffmpeg -i "$no_audio_video" -i "$audio" -shortest -vcodec copy -acodec copy "$output_video";

To change the frame rate of a video

ffmpeg -i "$video" -filter:v fps=20 "$output_video";

To merge two videos side by side

ffmpeg -i "$left_video" -i "$right_video" -filter_complex hstack "$output_video";

Concatenate multiple videos into one

The easiest way without writing huge commands is the following: First, create a file named parts.txt and add content similar to what we list below:

#Lines starting with # will be ignored
file 'part00-03.mp4'
file 'part04.mp4'
file 'part05-07.mp4'
file 'part08-09.mp4'
file 'part10.mp4'
file 'part11-13.mp4'

Then execute the following command to concatenate all those videos into one:

ffmpeg -f concat -safe 0 -i parts.txt -c copy "$output_video";

Speed up a video

Using the following command, you can speed up a video by dropping excess frames:

ffmpeg -i "$video" -filter:v "setpts=0.5*PTS" "$output_video";

The above example will double the speed (the value 0.5 controls it.)

To speed the video up without losing frames, you can increase the FPS value of the output video. To retrieve the frame rate, please see the command that was posted earlier.

ffmpeg -i "$video" -r 80 -filter:v "setpts=0.25*PTS" "$output_video";

In the second example, we assumed that the input video had 20 frames per second. Using the 0.25 value, we decided to speed the video up by a factor of 4. To preserve the input frames, we increased the frame rate from 20 to 80 using the parameter -r.

FFmpeg: Could find no file with path ‘%08d.ppm’ and index in the range 0-4 %08d.ppm: No such file or directory

During some work that we were doing, we used the following command to export the frames of a video using FFmpeg:

ffmpeg -v quiet -i "$video" "$input_frames_folder/%08d.ppm";

The above command exported the video frames into the selected folder and using eight digits zero-padding it named all the images in an increasing order starting from the number 00000001 (00000001.ppm).

Later on, we processed those frames and deleted some of the first ones (specifically, we deleted the first 61 frames, so the first available frame was named 00000062.ppm). When we tried to rebuild the video using the command below, we got the error that is listed after the command:

ffmpeg -framerate "25/1" -i "$input_frames_folder/%08d.ppm" -pix_fmt yuv420p -y processed.mkv;
Could find no file with path '%08d.ppm' and index in the range 0-4 %08d.ppm: No such file or directory

To fix the issue, we used the -start_number parameter with the value 62. The parameter sets the file starting index for the matched image files.

ffmpeg -start_number 62 -framerate "25/1" -i "$input_frames_folder/%08d.ppm" -pix_fmt yuv420p -y processed.mkv;

Please note that we also used -pix_fmt yuv420p as we were getting a video with black frames only, so we had to define the format of the pixels to use manually.