Detailed explanation of Android development MP4 file to GIF file

I. basic implementation principle

Before introducing the specific implementation process, first briefly describe the basic principles and implementation steps. In solving relatively complex problems, I am used to sorting out the main principles and steps first. Don't get stuck by cumbersome details at the beginning, and then break through them one by one when the specific implementation is carried out. Here are the main steps:

1. Video file reading: including recording and local file reading

2. Parse the video part to be converted into a bitmap sequence

3. Encode the parsed bitmap sequence to generate GIF file

II. Reading of video files

The reading of video files is relatively simple. There is nothing special to say. Here, simply post the core code of video reading. You can google it in detail.

III. analysis of video files

After the video file is read successfully, the next thing to do is to parse the video file, select the video clips to be converted, and extract the bitmap sequence. Let's take a look at the specific implementation. Extracting the bitmap sequence is to obtain the corresponding bitmap from the video file according to the given start time, end time and frame rate. This paper is mainly implemented by using the API provided by mediametadataretriever. Before looking at the code, you can first look at the API document of mediametadataretriever, The core function of this class is to obtain video frames and metadata. The following is the core implementation code:

IV. generate GIF file

Get the bitmap sequence to generate GIF. The next thing to do is to encode the data in the bitmap sequence according to the GIF file format to generate the final GIF file. The goal is very clear. Next, it depends on the specific implementation process.

1. Introduction to GIF format

Before generating GIF files, it is necessary to introduce the storage format of GIF. There are many articles related to GIF format. There is no need to introduce it in detail here. Just briefly describe the aspects that will be used in the following programs.

GIF images are based on the color list (the stored data is the color of the point corresponding to the index value of the color list), and only 8 bits (256 colors) are supported at most. GIF file is divided into many storage blocks, which are used to store multiple images or control blocks to determine image performance behavior, so as to realize animation and interactive applications. Gif files also compress image data by LZW compression algorithm to reduce image size.

Gif files are divided by blocks, including control blocks and data blocks. The control block controls the behavior of the data block, and contains some different control parameters according to different control blocks; The data block only contains some 8-bit character streams. Its function is determined by the control block in front of it. Each data block has 0 to 255 bytes. The first byte of the data block indicates the size (number of bytes) of the data block. This byte is not included when calculating the size of the data block. Therefore, an empty data block has one byte, that is, the size of the data block 0x00.

2. GIF file writing

At the beginning of contact with GIF files, you will feel more complex. The storage format and coding format are much more complex than bitmap, but in fact, you can simplify the problem. The principle of generating GIF is similar to that of generating bitmap, which is to write files according to the specified format. You don't have to tangle with internal details, otherwise you will fall into cumbersome details (commonly known as drilling a bull's horn) The ultimate goal is to generate GIF files. Here are some files that need to be written:

Extract pixel value of bitmap

First, you need to extract the pixel values of the bitmap obtained above, so that you can write the pixel values into the GIF file later. While extracting the pixel values, you can generate the color table required by the GIF file. The process of generating the color table is complex. The source code is not posted here. If you are interested, you can google the color quantization algorithm. If you are not interested, you can directly use the ready-made one, The following is the specific implementation of extracting pixel values:

GIF file header

The file header consists of 6 bytes in total, including GIF signature and version number. GIF signature consists of 3 characters "GIF" and 3 bytes in total. The version number also consists of 3 bytes, which can be "87A" or "89A" (1987 and 1989 versions respectively). The implementation code is as follows:

Logical screen descriptor

The file header is followed by the logical screen descriptor, which is composed of 7 bytes and defines the size, color depth, background color of GIF image, whether there is a global color list and the index number of color list. The implementation code is as follows:

The structure of logical screen identifier is a little more complicated. If you don't know what each bit represents, you can refer to the logical screen identifier in GIF graphics file format document.

Global color table

The global color list must be immediately followed by the logical screen identifier. Each color list index entry is composed of three bytes and arranged in the order of R, G and B. the specific implementation of generating the color table can be seen in the source code. Due to the complex generation process, the code generated by the color table is not pasted here. Here is the code written into the color table:

Graphic control extension

This part is optional and only supported in version 89A. It can be placed in front of an image block (including image identifier, local color list and image data) or text extension block to control the render form of the first image (or text) following it. The following implementation code:

Image descriptor

A GIF file can contain multiple images. After the end of an image, the next is the identifier of an image. The image identifier starts with 0x2c (''), which defines the properties of the image immediately following it, including the offset of the image relative to the logical screen boundary, the image size, whether there is a local color list and the size of the color list, which is composed of 10 bytes, The following is the implementation code:

Image data

Gif image data uses LZW compression algorithm, which greatly reduces the size of image data. The specific LZW compression algorithm can be Google. The program implementation part can refer to the source code link at the bottom of the article. The following is the implementation of image data writing:

File finalizer (trailer)

This part has only one byte, which identifies the end of a GIF file. The fixed value is 0x3b. The implementation code is:

summary

So far, the implementation process of converting MP4 file into GIF file has been basically completed. If you need to cut and add watermark to GIF file, you can process bitmap accordingly before the bitmap sequence is written into GIF. If you have any problems, welcome to exchange and learn. I hope the content of this article can be helpful to everyone's study and work.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>