mp4


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";
done;

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

for FILE in *.mp4; do
...
done;

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.

Conclusion:

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


Bash/FFMPEG: Batch resize .mp4 videos to fixed resolution 2

We needed to shrink a bunch of mp4 videos so that they would have the same size as the screen of an android device.
We did that both to save space on the internal memory of the device and to make the device perform as efficient as possible as it would not have to shrink the video on the fly.

The command we used was the following:

find . -type f -name "*.mp4" -exec bash -c 'FILE="$1"; ffmpeg -i "${FILE}" -s 1280x720 -acodec copy -y "${FILE%.mp4}.shrink.mp4";' _ '{}' \;

What this command does is the following:

  • Find all files in current folder (and sub-folders) that have the extension .mp4
  • For each file, create a new bash instance in which it will call ffmpeg taking as first parameter the filename that matched
  • -i "${FILE}"ffmpeg will take as input the filename we matched
  • -s 1280x720 – Then change the video size to 1280x720
  • -acodec copy – It will keep the audio as is
  • -y "${FILE%.mp4}.shrink.mp4 – Finally, create a new file (or overwrite existing) that has the extension .shrink.mp4 in the same folder

ffmpeg: Extract audio from .MP4 to .MP3

The following command will find all mp4 files that are in the current directory and in all sub-folders and extract the audio to mp3 format.

find . -type f -iname "*.mp4" -exec bash -c 'FILE="$1"; ffmpeg -i "${FILE}" -vn -y "${FILE%.mp4}.mp3";' _ '{}' \;

The filename of the audio file will be the same as the mp4 video with the correct extension. The mp4 extension will be removed and replaced by the mp3 extension e.g hi.mp4 will become hi.mp3


ffmpeg: Extract audio from .MP4 to .OGG 1

If you need to extract the audio from an .MP4 movie file to an .OGG audio file you can  execute the following:

FILE="the-file-you-want-to-process.mp4";
ffmpeg -i "${FILE}" -vn -acodec libvorbis -y "${FILE%.mp4}.ogg"

The first command will assign the file name to a variable, we do this to avoid typing errors in the second command where we might want to use the same name for the audio file.

The second command, will use ffmpeg to extract the audio. The -i flag, indicates the file name of the input. We used the flag -vn that will instruct ffmpeg to disable video recording. The -acodec flag will set the output audio codec to vorbis. The -y flag will overwrite output file without asking, so be careful when you use it.

In case we want to automatically process (batch process) all .MP4 video files in a folder we can use the following:

for FILE in *.mp4;
do
    echo -e "Processing video '\e[32m$FILE\e[0m'";
    ffmpeg -i "${FILE}" -vn -acodec libvorbis -y "${FILE%.mp4}.ogg";
done

The above script will find all .MP4 files in the folder and process them one after the other.

 

UPDATE:

The following command will find all mp4 files that are in the current directory and in all sub-folders and extract the audio to ogg format.

find . -type f -iname "*.mp4" -exec bash -c 'FILE="$1"; ffmpeg -i "${FILE}" -vn -acodec libvorbis -y "${FILE%.mp4}.ogg";' _ '{}' \;

The filename of the audio file will be the same as the mp4 video with the correct extension. The mp4 extension will be removed and replaced by the ogg extension e.g hi.mp4 will become hi.ogg