Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Introduction

A .264 file typically contains raw H.264-encoded video data without any associated metadata, audio tracks, and subtitles. The .264 files are often created by video recording devices, such as CCTV systems or digital video recorders (DVRs). Unlike MP4 files, .264 files don’t use a common container format, making them difficult to play or edit using regular media players or editing software. Therefore, to work with these files, they often need to be converted into a more widely supported format like mp4.

In this tutorial, we’ll learn how to convert a video from the .264 format to the .mp4 format in Linux.

The code in this tutorial underwent testing on a Debian 12 system using GNU Bash 5.1.16.

2. Environment Setup

Before we dive into the details, let’s ensure the necessary prerequisites are ready. They include a sample file in the .264 format and the MediaInfo tool.

2.1. Sample File

First, we create a new directory and download the .264 file to it using wget:

$ mkdir video && cd video
$ wget -q -O sample_video.264 https://www.fastvdo.com/264codec/streams/4cif/FVDO_Freeway_4cif.264

The mkdir command creates the video directory, while the cd command changes the current directory to the newly created video directory.

Next, the -q flag of the wget command suppresses the output, while -O specifies the output filename.

Thus, we have a .264 file (sample_video.264) inside the newly created video directory.

2.2. MediaInfo Installation

MediaInfo is a command-line utility that extracts and displays media file metadata. This utility isn’t pre-installed on most Linux distributions.

So, let’s proceed with the installation:

$ sudo apt install mediainfo

Subsequently, we can check the installed version of MediaInfo:

$ mediainfo --version
MediaInfo Command line, 
MediaInfoLib - v21.09

As we can see, the installed version of MediaInfo is v21.09.

At this point, we are ready with the basic setup.

3. Using FFmpeg

FFmpeg is a popular multimedia framework commonly used for audio and video editing, retrieving information about multimedia files, screen recording, and streaming.

3.1. Installation

To begin with, we verify that FFmpeg is installed on the system:

$ ffmpeg -version
ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11 (Ubuntu 11.2.0-19ubuntu1)
...

In case it’s not already available, we can usually install FFmpeg via a local package manager such as apt:

$ sudo apt install ffmpeg

This command installs the FFmpeg framework including tools like ffplay and ffprobe.

3.2. Format Conversion

The default behavior of FFmpeg to re-encode processed input media files makes it quite straightforward to convert between file formats. The FFmpeg auto-detects the input and output formats based on the file extensions.

So, we can change the output file’s extension to change the format:

$ ffmpeg -i sample_video.264 ffmpeg_output1.mp4

This command converts the input (-i) file, sample_video.264, and re-encodes it with the supported codec for the specified output file, i.e., ffmpeg_output1.mp4.

Alternatively, we can avoid re-encoding the input file by copying the video stream:

$ ffmpeg -i sample_video.264 -vcodec copy ffmpeg_output2.mp4

The -vcodec copy option copies the video stream without re-encoding it. Additionally, we can use the shorthand -c:v instead of -vcodec to specify the video codec, achieving the same result with a more concise command.

Let’s play one of the output files using ffplay:

$ ffplay -autoexit ffmpeg_output2.mp4

This command plays the ffmpeg_output2.mp4 video and exits automatically (-autoexit) once the track is finished.

3.3. Format Verification

To ensure the conversion produces valid output, we can verify the format of one of the resulting files:

$ mediainfo ffmpeg_output2.mp4 
General
Complete name                            : ffmpeg_output2.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/iso2/avc1/mp41)
File size                                : 2.09 MiB
Duration                                 : 9 s 280 ms
Overall bit rate                         : 1 892 kb/s
Writing application                      : Lavf58.76.100

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4
Format settings                          : CABAC / 2 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 2 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 9 s 280 ms
Bit rate                                 : 1 890 kb/s
...

The output confirms the MEPG-4 (mp4) format.

Similarly, we can verify the format of other output files. Generally, the output of the mediainfo command for a video file is divided into three parts:

  • General
  • Audio
  • Video

Since the .264 file lacks an audio track, the converted file lacks any audio. Thus, the output of mediainfo is divided into only two parts for the converted files, i.e., General and Audio.

4. Using VLC

VLC (VideoLAN Client) is an open-source multimedia player supporting most multimedia file formats. Additionally, with its user-friendly interface, VLC is a versatile tool for playing and converting multimedia files.

4.1. Installation

Let’s install VLC with the APT package manager:

$ sudo apt install vlc

Now, we can check the installed version of VLC:

$ vlc --version
VLC media player 3.0.16 Vetinari (revision 3.0.13-8-g41878ff4f2)
VLC version 3.0.16 Vetinari (3.0.13-8-g41878ff4f2)
Compiled by buildd on lcy02-amd64-108.buildd (Mar 13 2022 08:00:10)
Compiler: gcc version 11.2.0 (Ubuntu 11.2.0-18ubuntu1)
...

This verifies a working installation of VLC on the system.

4.2. Format Conversion via the Command Line

First, let’s use the command line interface for the format conversion:

$ vlc sample_video.264 --sout='#transcode{vcodec=h264}:std{access=file,mux=mp4,dst=vlc_output1.mp4' vlc://quit

This command launches the VLC media player from the command line that reads the input media file (sample_video.264) and packages it into the MP4 container format.

Let’s take a closer look at the options used in this command:

  • –sout (stream output) defines the options to process the input file and send it to a different location like a file or stream
  • #transcode instructs the VLC media player to transcode (convert) the video with specified codec
  • {vcodec=h264} specifies h264 as the video codec to be used for the output
  • :std{…} defines the standard output parameters specifying where and how to save the output
  • access=file specifies that the output should be saved as a file on the system
  • mux=mp4 sets the multiplexing or container format of output to mp4
  • dst=vlc_output1.mp4 specifies the destination or output file name, in this case, vlc_output1.mp4
  • vlc://quit tells VLC to automatically quit after completing the conversion process

Without the vlc://quit, VLC might stay open after processing the input file.

4.3. Format Conversion via the User Interface

Alternatively, we can use the VLC interface to convert the .264 file to the .mp4 format.

Let’s open VLC from the Applications menu. Then, we click on the Media menu from the VLC application window followed by a click on the Convert/Save button:

Media menu of VLC media player

In the new window, we select the input file:

Convert menu from VLC

Next, we click on the Add button and select the input media file, in this case, sample_video.264. Then, let’s click on the Convert/Save button to specify the output details including format and filename:

Convert window with the options used

Finally, we click on the Start button to start the format conversion process.

Again, the output format can be verified with the mediainfo command.

5. Using x264 Encoder

x264 encoder program provides the functions used to encode video streams into the H.264/MPEG-4 AVC format.

5.1. Installation

Here, we again use the apt utility to install the x264 encoder:

$ sudo apt install x264

Let’s verify the installation:

$ x264 --version
x264 0.164.x
built on Sep 14 2024, gcc: 11.4.0
x264 configuration: --chroma-format=all
libx264 configuration: --chroma-format=all
x264 license: GPL version 2 or later

Thus, we see the shell environment provides access to the x264 encoder program.

5.2. Format Conversion

x264 selects the output file type from the specified output filename:

$ x264 -o x264_output1.mp4 sample_video.264

This command takes a raw .264 video file (sample_video.264), encodes it using the H.264 codec, and outputs (-o) it into an MP4 container.

Moreover, we can use the –fps option to specify the frame rate in the output video file. This is especially useful when the input file doesn’t have frame rate information, i.e., when dealing with raw .264 files. Additionally, the x264 encoder program provides several video filters including crop and resize.

6. Using FF Multi Converter

FF Multi Converter is a simple graphical application for converting audio, video, image, and document files between all popular formats. It uses other tools like FFmpeg for audio and video conversion, ImageMagick for image files, and unoconv (Universal Office Converter) for converting between document files.

6.1. Installation

Let’s start by cloning the official repository of FF Multi Converter using git:

$ git clone https://github.com/ilstam/FF-Multi-Converter.git

We can also use the FF Multi Converter without installing it, by running the launcher script from the downloaded archive:

$ cd FF-Multi-Converter
$ ./launcher

The cd command changes the current directory to the cloned repository, while the launcher script launches the FF Multi Converter application, irrespective of the installation process.

Alternatively, we can install the FF Multi Converter program from within the cloned Git repository, which requires python3 with the PyQt5 library:

$ sudo python3 setup.py install

The python3 command installs the FF Multi Converter program.

6.2. Format Conversion

Let’s open the FF Multi Converter either from the Applications menu or from the command line:

$ ./launcher ../sample_video.264

This command opens up a graphical interface with the sample_video.264 file as an input:

FF Multi Converter interface

We can select the output format from the drop-down menu of the Convert to: option, in this case, mp4. Then, we select the checkbox at the bottom left side to save the file in the same folder as the input file or provide the path for the output folder. Finally, we click on the Convert button at the bottom right corner of the window to convert the file format.

FF Multi Converter supports the conversion of multiple .264 files to a destination format in batch mode, such as mp4, mkv, ogg, and others.

7. Conclusion

In this article, we learned several ways to convert .264 files to mp4 format in Linux.

To begin with, we installed and tested MediaInfo to verify the format for both the sample input and output files. After that, we used FFmpeg to convert the .264 file to mp4 format. Next, we installed and discussed the usage of VLC to convert .264 files to mp4 format. Then, we used the x264 library to demonstrate the conversion of a .264 file to mp4 format. Finally, we employed the graphical interface of the FF Multi Converter tool and converted the .264 file to the mp4 format.