Using MP4V2 to synthesize h264+aac mp4 files

Source: Internet
Author: User
Tags mutex

This article was reproduced from: http://www.cnblogs.com/chutianyao/archive/2012/04/13/2446140.html

Recording program to add new features: Record CMMB TV shows, our cards are sent out of the RTP stream (H264 video and AAC audio), the recording program to do the work is:

(1) Receiving and parsing RTP packets, separating out H264 and AAC data streams;

(2) H264 video and AAC audio are encapsulated in some format and finally saved as a file for viewing by the user.

The first step is already part of the code to refer to, so it's done quickly.

In the second step, we decided to encapsulate the MP4, and after finding some information, we decided to use the Open Source Library mp4v2 to synthesize the MP4 file.

The technical route has been determined and the work is started.

(i) basic knowledge of the MP4 format.

About MP4 format, the online introduction of many, there are the following content for reference:

(1) Two ISO standards:

[ISO/IEC 14496-12]:iso base media file format--"is a general format forming the basis for a number of and more specific File formats. This format contains the timing, structure, and media information for timed sequences of media data, such as audio-visual Presentations "

[ISO/IEC 14496-14]:mp4 file Format--"This specification defines MP4 as an instance of the ISO Media file format [ISO/IEC 14496-12 and ISO/IEC
15444-12]. "

Defines the MP4 file format standard.

(2) http://wenku.baidu.com/view/673482284b73f242336c5f4c.html

Is the above two standard explanation, suggest to see this first, understand probably, concrete detail again look ISO standard document.

(ii) Technical verification. The main thing is to write the verification code to verify the technical feasibility.

To the official website to download mp4v2 source code, compile, installation process is not mentioned. All information can be found at http://code.google.com/p/mp4v2/.

The first part of the verification code, finished quickly, but the encapsulated file has a problem, unable to play.

The composition section, the code is as follows:

 1 static void* Writethread (void* Arg) 2 {3 rtp_s* P_RTP = (rtp_s*) arg;
 4 if (P_RTP = = NULL) 5 {6 printf ("error!\n");
 7 return; 8} 9 Mp4filehandle file = Mp4createex ("Test.mp4", Mp4_details_all, 0, 1, 1, 0, 0, 0, 0);//Create a MP4 file if     (File = = Mp4_invalid_file_handle) {printf ("Open File fialed.\n"); 15} 16 17
Mp4settimescale (file, 90000);                                             18 19//Add H264 track Mp4trackid video = mp4addh264videotrack (file, 90000, 90000/25, 320, 240, 21 0x64,//sps[1] avcprofileindication 0x                                             XX,//sps[2] Profile_compat 0x1f,//sps[3] avclevelindication 24 3); 4 bytes length before each NAL unit if (video = = mp4_invalid_track_id) + printf ("Add video tra
CK failed.\n "); ReturN
Mp4setvideoprofilelevel (file, 0x7F);
31 32//Add AAC Audio (Mp4trackid) to Mp4addaudiotrack (file, 48000, 1024x768, mp4_mpeg4_audio_type);     if (video = = mp4_invalid_track_id) + ("Add audio track failed.\n"); 38
} mp4setaudioprofilelevel (file, 0x2);
ncount int = 0;         while (1) frame_t* pf = NULL;//frame pthread_mutex_lock (&p_rtp->mutex); 47
PF = p_rtp->p_frame_header; if (PF = NULL) (Pf->i_type = = 1)//video {M
P4writesample (file, video, Pf->p_frame, Pf->i_frame_size, mp4_invalid_duration, 0, 1); (Pf->i_type = = 2)//audio Mp4writesample (fi
Le, audio, pf->p_frame, pf->i_frame_size, mp4_invalid_duration, 0, 1);
ncount++;          60 61   Clear frame.
p_rtp->i_buf_num--;
P_rtp->p_frame_header = pf->p_next; if (p_rtp->i_buf_num <= 0) $ {p_rtp->p_frame_buf = P_rtp->p_fr
Ame_header;
Free_frame (&AMP;PF);
-PF = NULL;         (Ncount >=) (74} 75} 76 else//printf ("BUFF EMPTY, p_rtp->i_buf_num:%d\n", P_rtp->i_buf_num); 79} 8
0 Pthread_mutex_unlock (&p_rtp->mutex);
Bayi Usleep (10000);
Mp4close (file); 85}

Phenomenon: No image, no sound, no playback at all.

So, the hard work began: tracking the cause of the search.

(1) using VLC to play the synthesized MP4 file, see the detailed output:

1 VLC-VVV test.mp4
2 [0x8e9357c] mp4 stream debug:found Box:ftyp size 
3 [0x8e9357c] mp4 stream Debug:found Box:free size 136 
4 [0x8e9357c] mp4 stream debug:skip box: "Free" 
5 [0x8e9357c] mp4 stream Debug:found Box:mda T size 985725 
6 [0x8e9357c] mp4 stream debug:skip box: "Mdat" 
7 [0x8e9357c] mp4 stream Debug:found box:moov siz E

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.