Using FFmpeg to extract MP4 h264 code stream Write file Flower screen

Source: Internet
Author: User
Tags sprintf

1, using ffmpeg extract MP4 h264 Code stream Write file method online There are many, do not know, please refer to the Raytheon Blog: http://blog.csdn.net/leixiaohua1020/article/details/11800877

2, but the file is a problem, the first can certainly play, but there will be a great chance to appear in the flower screen

A, first talk about the solution

In fact, it is very simple, but also use the Av_bitstream_filter_filter method to handle each avpacket, as follows:

	Avpacket Temppack;
	Av_copy_packet (&temppack, &pkt_in);
	Av_bitstream_filter_filter (BSFC, Pcodecctx, NULL, &temppack.data, &temppack.size, Temppack.data, Temppack.size, 0);  
	
b, say the reason

In FFmpeg, a avpacket may contain more than one nal, so according to Thor, the second nal in the same avpacket is not added in fact 00 00 00 01, if just this nal is other nal reference, then there will be a flower screen.

C, in talking about how nal is stored in Avpacket.data

In Avpacket.data each nal does not have a starting code of 00 00 00 01, where the structure of NAL is the NAL data length of the starting 4 bytes (excluding this 4 bytes), then the real nal data.

The following figure:

Paste the sample code:

/* * Write NAL Test * * Miu Mickel *821486004@qq.com * * This program to achieve a read H264 encoded video saved as nal file, which has 2 kinds of writing, the first is common on the Internet in each avpacket data before adding NAL header *
 Two kinds is to use the filter, this program to each avpacket are written in a single nal, you can do each frame of the nal file comparison, if you want to put all the frame * written h264 stream files, their own transformation program does not open a new file every frame is good. */#include "stdafx.h" #ifdef __cplusplus extern "C" {#endif #include "libavcodec/avcodec.h" #include "libavformat/avf Ormat.h "#pragma comment (lib," Avcodec.lib ") #pragma comment (lib," Avformat.lib ") #pragma comment (lib," Avutil.lib ") #if
Def __cplusplus};

#endif #include <vector> #include <string> avformatcontext *ifmt_ctx = NULL;

#include <stdio.h> int g_videoindex =-1;
	int openinputfile (const char* filename) {int ret = 0; Open the input if (ret = avformat_open_input (&ifmt_ctx, filename, null, NULL)) < 0) {printf ("can not open I
		Nput ");
	return ret;
		} if (ret = Avformat_find_stream_info (Ifmt_ctx, NULL)) {printf ("Can not find input stream info");
	return ret; }//open the decoder for (int i = 0; i < Ifmt_ctx-> nb_streams;
			i++) {if (Ifmt_ctx->streams[i]->codec->codec_type = = avmedia_type_video) {g_videoindex = i; ret = Avcodec_open2 (Ifmt_ctx->streams[i]->codec, Avcodec_find_decoder (ifmt_ctx->streams[i]->codec-

			>codec_id), NULL);
				if (Ret < 0) {printf ("Can not open decoder");
			return ret;
}}} return 0;
	} int _tmain (int argc, _tchar* argv[]) {if (ARGC < 2) {return-1;
	} avpacket pkt_in, pkt_out;
	Avframe *frame = NULL;
	unsigned int stream_index;

	Av_register_all ();
		if (Openinputfile (argv[1) < 0) {printf ("Failed to open input file");
	Goto end;
	} if (G_videoindex = =-1) {return-1;
	} FILE *fp_sps=fopen ("testnal/testnal/sps.h264", "wb+");

	FILE *fp_f_sps=fopen ("testnal/testfilternal/sps.h264", "wb+");
	FILE *testmafp = fopen ("testmaual.h264", "wb+");
	Avcodeccontext *pcodecctx= ifmt_ctx->streams[g_videoindex]->codec;   unsigned char *dummy=null;
	The input pointer int dummy_len; Fwrite (pcodecctx-&GT;EXTRADATA,PCODECCTX-&GT;EXTRADATA_SIZE,1,FP_SPS);    
	avbitstreamfiltercontext* BSFC = Av_bitstream_filter_init ("H264_mp4toannexb");
	Av_bitstream_filter_filter (BSFC, pcodecctx, NULL, &dummy, &dummy_len, NULL, 0, 0);
	
	Fwrite (Pcodecctx->extradata,pcodecctx->extradata_size,1,fp_f_sps);
	Fclose (Fp_sps);
	
	Fclose (Fp_f_sps);

	Fwrite (PCODECCTX-&GT;EXTRADATA,PCODECCTX-&GT;EXTRADATA_SIZE,1,TESTMAFP);
	int totol_frame_index = 0;
		while (1) {if (Av_read_frame (Ifmt_ctx, &pkt_in) < 0) {break; } if (Ifmt_ctx->streams[pkt_in.stream_index]->codec->codec_type = = Avmedia_type_video) {//The first notation, in front of each frame
			Add nal head char tmpname[100];
			sprintf (Tmpname, "testnal/testnal/frame_%d.h264", Totol_frame_index);
			FILE *fp=fopen (Tmpname, "wb+");  
				if (FP) {char nal_start[]={0,0,0,1};  
				Fwrite (NAL_START,4,1,FP);
				Fwrite (PKT_IN.DATA+4,PKT_IN.SIZE-4,1,FP);
			Fclose (FP); }//The second notation, with Av_bitstream_filter_filter sprintf (Tmpname, "Testnal/testfilternal/frame_%d.h264 ", Totol_frame_index);
			FILE *fp_filter=fopen (Tmpname, "wb+");
				if (fp_filter) {Avpacket temppack;
				Av_copy_packet (&temppack, &pkt_in); Av_bitstream_filter_filter (BSFC, Pcodecctx, NULL, &temppack.data, &temppack.size, Temppack.data,  
				Temppack.size, 0); 
				Fwrite (Temppack.data,pkt_in.size,1,fp_filter);
			Fclose (Fp_filter);
		} totol_frame_index++;
	} continue;    
	} av_bitstream_filter_close (BSFC); 

	Free (dummy);
Fclose (TESTMAFP);

	End:avformat_close_input (&AMP;IFMT_CTX);
	GetChar ();
return 0;
 }


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.