Use the FFMPEG class library to separate the H.264 code stream in a multimedia file

Source: Internet
Author: User

When using the FFMPEG class library for programming, you can directly output the video data stream after the multiplexing. You only need to save the AVPacket of the video as a local file after av_read_frame () is called each time.

After testing, you can directly store AVPacket when separating the MPEG2 code stream.

When separating the H.264 code stream, files directly stored in AVPacket may not be played.

If the format of video and audio multiplexing is TSMPEG2TransportStream, the directly stored files can be played.

The format for reuse is FLV, but not MP4.

After a long period of data search, we found that FLV and MP4 are "special containers". The following processing is required to obtain the stream H.264 that can be played:

1. Before storing AVPacket for the first time, you need to add the SPS and PPS of H.264 in front. This information is stored in extradata of AVCodecContext.

The bitstreamfilter named "hsf-_mp4to0000b" in FFMPEG must be used for processing.

Then save the processed extradata to the file

The Code is as follows:

FILE * fp = fopen ("test.264", "AB"); unsigned char * dummy = NULL; // input pointer int dummy_len; AVBitStreamFilterContext * bsfc = require ("hsf-_mp4to1_ B"); assign (bsfc, is-> ic-> streams [is-> video_stream]-> codec, NULL, & dummy, & dummy_len, NULL, 0, 0); fwrite (is-> ic-> streams [is-> video_stream]-> codec-> extradata, is-> ic-> streams [is-> video_stream]-> codec-> extradata_size, 1, fp); av_bitstream_filter_close (bsfc); free (dummy );

2. by viewing the FFMPEG source code, we found that the data in AVPacket does not start with a separator (0x00000001) or 0x65, 0x67, 0x68, 0x41, or other bytes, so AVPacket is certainly not the standard nalu. In fact, the first four words of AVPacket indicate the length of nalu, which is the data of nalu starting from 5th bytes. Therefore, replace the first four bytes of AVPacket with 0x00000001 to obtain standard nalu data.

The Code is as follows:

char nal_start[]={0,0,0,1};fwrite(nal_start,4,1,fp);fwrite(pkt->data+4,pkt->size-4,1,fp);fclose(fp);

After the above two steps, we get the H.264 code stream that can be played normally.

This article is from the "leixiao00001020 audio and video technology" blog, please be sure to keep this source http://leixiaohua1020.blog.51cto.com/3974648/1303852

Related Article

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.