参考雷神的例子(https://blog.csdn.net/leixiaohua1020/article/details/25430425)改成了新版的ffmpeg接口,.
av_dict_set(¶m, "preset", "slow", 0); ¶m网站转码存在问题。
大部分问题参考了https://blog.csdn.net/Jason_er/article/details/102745442
flush函数也是类似的方法,刚刚开始研究ffmpeg 也不知道改的对不对。欢迎大家留言指正
flush函数还是有问题,直接屏蔽了。
参考了https://www.cnblogs.com/yinxiangpei/articles/3891827.html 解决了掉帧的问题
/**
* 最简单的基于FFmpeg的视频编码器
* Simplest FFmpeg Video Encoder
*
* 雷霄骅 Lei Xiaohua
* leixiaohua1020@126.com
* 中国传媒大学/数字电视技术
* Communication University of China / Digital TV Technology
* http://blog.csdn.net/leixiaohua1020
*
* 本程序实现了YUV像素数据编码为视频码流(H264,MPEG2,VP8等等)。
* 是最简单的FFmpeg视频编码方面的教程。
* 通过学习本例子可以了解FFmpeg的编码流程。
* This software encode YUV420P data to H.264 bitstream.
* It's the simplest video encoding software based on FFmpeg.
* Suitable for beginner of FFmpeg
*/
#include <stdio.h>
#define __STDC_CONSTANT_MACROS
#ifdef _WIN32
//Windows
extern "C"
{
#include "libavutil/opt.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/imgutils.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#ifdef __cplusplus
};
#endif
#endif
int flush_encoder(AVFormatContext *fmt_ctx, AVCodecContext *pCodecCtx, unsigned int stream_index) {
int ret;
int got_frame;
AVPacket enc_pkt;
// 用于查找FFmpeg的解码器(参考FFmpeg解码流程图)
AVCodec* pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL) {
printf("Codec not found.\n");
return -1;
}
if (!(pCodec->capabilities & AV_CODEC_CAP_DELAY))
return 0;
while (1) {
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
//ret = avcodec_encode_video2(pCodecCtx, &enc_pkt,NULL, &got_frame);
ret = avcodec_send_frame(pCodecCtx, NULL);
got_frame = avcodec_receive_packet(pCodecCtx, &enc_pkt);
if (ret < 0)
{
ret = 0;
break;
}
if (got_frame!=0) {
ret = 0;
break;
}
printf("Flush Encoder: Succeed to encode 1 frame!\tsize:%5d\n", enc_pkt.size);
/* mux encoded frame */
ret = av_write_frame(fmt_ctx, &enc_pkt);
if (ret < 0)
break;
av_frame_free(NULL);
}
return ret;
}
int main(int argc, char* argv[])
{
AVFormatContext* pFormatCtx;
AVOutputFormat* fmt;
AVStream* video_st;
AVCodecContext* pCodecCtx;
AVCodec* pCodec;
AVPacket pkt;
uint8_t* picture_buf;
AVFrame* pFrame;
int picture_size;
int y_size;
int framecnt = 0;
int ret = -1;
//FILE *in_file = fopen("src01_480x272.yuv", "rb"); //Input raw YUV data
FILE *in_file;
fopen_s(&in_file,"d:/ds_480x272.yuv", "rb"); //Input raw YUV data
int in_w = 480, in_h = 272; //Input data's width and height
int framenum = 100; //Frames to encode
//const char* out_file = "src01.h264"; //Output Filepath
//const char* out_file = "src01.ts";
//const char* out_file = "src01.hevc";
const char* out_file = "d:/ds.h264";
//av_register_all();
Method1.
//pFormatCtx = avformat_alloc_context();
Guess Format
//fmt = av_guess_format(NULL, out_file, NULL);
//pFormatCtx->oformat = fmt;
Method 2.
avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);
fmt = pFormatCtx->oformat;
//Open output URL
if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
printf("Failed to open output file! \n");
return -1;
}
video_st = avformat_new_stream(pFormatCtx, 0);
//video_st->time_base.num = 1;
//video_st->time_base.den = 25;
if (video_st == NULL) {
return -1;
}
//Param that must set
//pCodecCtx = video_st->codec;
pCodecCtx = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(pCodecCtx, video_st->codecpar);
//pCodecCtx->codec_id =AV_CODEC_ID_HEVC;
pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
pCodecCtx->width = in_w;
pCodecCtx->height = in_h;
pCodecCtx->bit_rate = 400000;
pCodecCtx->gop_size = 250;
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 25;
//H264
//pCodecCtx->me_range = 16;
//pCodecCtx->max_qdiff = 4;
//pCodecCtx->qcompress = 0.6;
pCodecCtx->qmin = 10;
pCodecCtx->qmax = 51;
//Optional Param
pCodecCtx->max_b_frames = 3;
// Set Option
AVDictionary *param = 0;
//H.264
if (pCodecCtx->codec_id == AV_CODEC_ID_H264) {
av_dict_set(¶m, "preset", "slow", 0);
av_dict_set(¶m, "tune", "zerolatency", 0);
//av_dict_set(¶m, "profile", "main", 0);
}
//H.265
if (pCodecCtx->codec_id == AV_CODEC_ID_H265) {
av_dict_set(¶m, "preset", "ultrafast", 0);
av_dict_set(¶m, "tune", "zero-latency", 0);
}
//Show some Information
av_dump_format(pFormatCtx, 0, out_file, 1);
pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (!pCodec) {
printf("Can not find encoder! \n");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, ¶m) < 0) {
printf("Failed to open encoder! \n");
return -1;
}
pFrame = av_frame_alloc();
//设置真的格式、宽度和高度,否则会出现
//1.AVFrame.format is not set
//AVFrame.width or height is not set
pFrame->format = pCodecCtx->pix_fmt;
pFrame->width = pCodecCtx->width;
pFrame->height = pCodecCtx->height;
//picture_size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
picture_size = av_image_get_buffer_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 1);
picture_buf = (uint8_t *)av_malloc(picture_size);
//avpicture_fill((AVPicture *)pFrame, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
ret = av_image_fill_arrays(pFrame->data, pFrame->linesize, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 1);
//FIX:avformat_write_header return -22
pFormatCtx->streams[0]->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
pFormatCtx->streams[0]->codecpar->width = in_w;
pFormatCtx->streams[0]->codecpar->height = in_h;
//Write File Header
int ert = avformat_write_header(pFormatCtx, NULL);
av_new_packet(&pkt, picture_size*3);
y_size = pCodecCtx->width * pCodecCtx->height;
int skipped_frame = 0;
for (int i = 0; i<framenum; i++) {
//Read raw YUV data
if (fread(picture_buf, 1, y_size * 3 / 2, in_file) <= 0) {
printf("Failed to read raw data! \n");
return -1;
}
else if (feof(in_file)) {
break;
}
pFrame->data[0] = picture_buf; // Y
pFrame->data[1] = picture_buf + y_size; // U
pFrame->data[2] = picture_buf + y_size * 5 / 4; // V
//PTS
//pFrame->pts=i;
pFrame->pts = i*(video_st->time_base.den) / ((video_st->time_base.num) * 25);
int got_picture = 0;
//Encode
//int ret = avcodec_encode_video2(pCodecCtx, &pkt, pFrame, &got_picture);
ret = avcodec_send_frame(pCodecCtx, pFrame);
got_picture = avcodec_receive_packet(pCodecCtx, &pkt);
if (ret < 0) {
printf("Failed to encode! \n");
return -1;
}
if (got_picture == 0) {
printf("Succeed to encode frame: %5d\tsize:%5d\n", framecnt, pkt.size);
framecnt++;
pkt.stream_index = video_st->index;
av_write_frame(pFormatCtx, &pkt);
av_packet_unref(&pkt);
}
else
{
skipped_frame++;
}
}
Flush Encoder
//ret = flush_encoder(pFormatCtx, pCodecCtx, 0);
//if (ret < 0) {
// printf("Flushing encoder failed\n");
// return -1;
//}
for (int i = skipped_frame; i>0; i--)
{
int got_picture = 0;
//Encode
//ret = avcodec_decode_video2(video_dec_ctx, vframe, &got_frame, &packet);
ret = avcodec_send_frame(pCodecCtx, pFrame);
got_picture = avcodec_receive_packet(pCodecCtx, &pkt);
if (got_picture==0) {
pkt.pts = av_rescale_q(pkt.pts, pFormatCtx->streams[0]->time_base, pFormatCtx->streams[0]->time_base);
av_write_frame(pFormatCtx, &pkt);
}
}
//Write file trailer
av_write_trailer(pFormatCtx);
//Clean
if (video_st) {
avcodec_close(pCodecCtx);
av_free(pFrame);
av_free(picture_buf);
}
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);
fclose(in_file);
system("pause");
return 0;
}