使用FFmpeg4.2.x 编码YUV序列为h264视频文件

 

/******************************
功能:编码YUV序列为h264视频文件
FFmpeg:4.0.2
******************************/
#include 
extern "C"
{
#include 
#include 
#include 
}
using namespace std;

static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt, FILE *outfile)
{
	int ret;
	if (frame)
		printf("Send frame %3lld\n", frame->pts);
	ret = avcodec_send_frame(enc_ctx, frame);
	if (ret < 0)
	{
		fprintf(stderr, "Error sending a frame for encoding\n");
		exit(1);
	}
	while (ret >= 0)
	{
		ret = avcodec_receive_packet(enc_ctx, pkt);
		if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
			return;
		else if (ret < 0)
		{
			fprintf(stderr, "Error during encoding\n");
			exit(1);
		}
		printf("Write packet %3lld(size=%5d)\n", pkt->pts, pkt->size);
		fflush(stdout);
		fwrite(pkt->data, 1, pkt->size, outfile);
		av_packet_unref(pkt);
	}
}
in

你可能感兴趣的:(音视频,YUV,YUV420P)