Mp4v2 recording MP4 (h264 + AAC) Videos

Source: Internet
Author: User

The MP4 recording program is modified according to the mpeg4ip-1.5.0.1 \ Server \ mp4live \ file_mp4_recorder.cpp file in mpeg4ip. The program supports h264 + AAC (raw stream) writing method, using the dynamic library mp4v2-2.0.0, do not use the older version of mpeg4ip, because it will be efficient when recording large files, the following describes some mp4v2 interfaces.

 

Mp4filehandle mp4create (const char * filename, uint32_t flags)
Function: Create an MP4 file handle.
Return: MP4 file handle.
Parameter: The MP4 file name to be recorded by filename. Flags indicates the file type to be created. If you want to create a common file, use the default value 0. If you want to record a MP4 File larger than 4 GB, set mp4_create_64bit_data.

 

Bool mp4settimescale (mp4filehandle hfile, uint32_t value)
Function: sets the time scale.
Return: True is returned for success, and false is returned for failure.
Parameter: hfile MP4 file handle. The value to be set (number of clock ticks per second ).

 

Mp4trackid mp4addh1_videotrack (mp4filehandle hfile,
Uint32_t timescale,
Mp4duration sampleduration,
Uint16_t width,
Uint16_t height,
Uint8_t avcprofileindication,
Uint8_t profile_compat,
Uint8_t avclevelindication,
Uint8_t samplelenfieldsizeminusone)
Function: adds an h264 video track.
Return: the ID of the track.
Parameters: hfile MP4 file handle, timescale video ticks per second (such as 90000), sampleduration set to mp4_invalid_duration, width height video width and height, avcprofileindication profile (baseline profile, main profile, etc. see), profile_compat compatible profile, avclevelindication levels, samplelenfieldsizeminusone
Set to 3.
Note: avcprofileindication, profile_compat, and avclevelindication are obtained in the h264 stream.

 

Mp4trackid mp4addaudiotrack (
Mp4filehandle hfile,
Uint32_t timescale,
Mp4duration sampleduration,
Uint8_t audiotype)
Function: adds an audio (AAC) track.
Return: the ID of the track.
Parameters: hfile MP4 handle, timescale audio ticks per second (for example, 16000). The following two parameters are set to mp4_invalid_duration and mp4_mpeg4_audio_type.

Bool mp4settrackesconfiguration (
Mp4filehandle hfile,
Mp4trackid trackID,
Const uint8_t * pconfig,
Uint32_t configsize );

Function: Set audio decoding information (if set incorrectly, no sound is generated ).
Return: True is returned for success, and false is returned for failure.
Parameters: hfile file handle, trackID audio track ID, pconfig record the binary stream of decoding information, and configsize decoding String Length.
Note: mpeg4ip uses faac for AAC audio encoding. During encoding, you can call the corresponding function to obtain the binary string pconfig and length configsize. However, if AAC is not encoded using faac, you need to fill in pconfig by yourself. Refer to faac implementation. The following is an example of filling structure:

 
The first five bytes are AAC object types low 2
The next four bytes are the bitrate index 16000 8.
The next four bytes are the number of channels 1.
The correct binary format to be printed is 00010 | 1000 | 0001 | 000
2 8 1

Bool mp4writesample (
Mp4filehandle hfile,
Mp4trackid trackID,
Const uint8_t * pbytes,
Uint32_t numbytes,
Mp4duration duration default (mp4_invalid_duration ),
Mp4duration renderingoffset default (0 ),
Bool issyncsample default (true ));

Function: Write a frame of video data or a piece of audio data.
Return: True is returned for success, and false is returned for failure.
Parameter: hfile file handle, trackID, audio or video track ID. pbytes is the data stream pointer to be written, and numbytes is the Data byte length, duration indicates the number of ticks between the previous video frame and the current video frame, or the ticks between the previous audio data and the current audio data. Issyncsample indicates whether the video is a key frame.
Note: 1. The duration parameter is used for audio and video synchronization. If it is set incorrectly, audio and video will not be synchronized, and even crash may occur (generally when mp4close is called ). 2. For the video stream mp4writesample function, each call is to record the data of the previous frame. The duration value is calculated using the timestamp of the current frame and the timestamp of the previous frame, save the current frame and use it for the next call to mp4writesample. The same is true for Audio Data Writing.

 

 

Void mp4addh1_sequenceparameterset (
Mp4filehandle hfile,
Mp4trackid trackID,
Const uint8_t * psequence,
Uint16_t sequencelen );
And
Void mp4addh1_pictureparameterset (
Mp4filehandle hfile,
Mp4trackid trackID,
Const uint8_t * ppict,
Uint16_t pictlen );

Function: adds a sequence parameter set and an image parameter set.
Parameters: hfile file handle, trackID video track ID, queue quence, and ppict are data pointers to the sequence image parameter set to be written, and sequencelen and pictlen are string lengths.
Note: When a sequence parameter set or image parameter set is detected, call mp4addh1_sequenceparameterset or mp4addh1_pictureparameterset to update the sequence parameter set.

 

Void mp4close (
Mp4filehandle hfile,
Uint32_t flags default (0 ));
Function: Disable the MP4 file to open.
Parameter: hfile file handle. Whether flags allows extra optimization before closing MP4 files.
Note: You can set flags as the default value when recording a small MP4 file. If you want to set flags to mp4_close_do_not_compute_bitrate for a large recording file, otherwise calling the mp4close function will take a long time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.