参考雷神的例子(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 解决了掉帧的问题
解决libx265的问题 参考了https://blog.csdn.net/guoyunfei123/article/details/105611539
cannot find libx265 encoder的问题坑了几天,找了最新版的20200510的库,控制台有详细信息才找到上面博主的文章。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333/** * 最简单的基于FFmpeg的视频编码器 * Simplest FFmpeg Video Encoder * * 雷霄骅 Lei Xiaohua * leixiaohua1020@126.com * 中国传媒大学/数字电视技术 * Communication University of China / Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * * 本程序实现了YUV像素数据编码为视频码流(HEVC(H.265),H264,MPEG2,VP8等等)。 * 是最简单的FFmpeg视频编码方面的教程。 * 通过学习本例子可以了解FFmpeg的编码流程。 * This software encode YUV420P data to HEVC(H.265) bitstream (or * H.264, MPEG2, VP8 etc.). * It's the simplest video encoding software based on FFmpeg. * Suitable for beginner of FFmpeg */ #include <stdio.h> extern "C" { #include "libavutilopt.h" #include "libavcodecavcodec.h" #include "libavformatavformat.h" #include "libswscaleswscale.h" #include "libavutilimgutils.h" }; // //int flush_encoder(AVFormatContext *fmt_ctx, unsigned int stream_index) //{ // int ret; // int got_frame; // AVPacket enc_pkt; // if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities & // AV_CODEC_CAP_DELAY)) // return 0; // while (1) { // printf("Flushing stream #%u encodern", stream_index); // //ret = encode_write_frame(NULL, stream_index, &got_frame); // enc_pkt.data = NULL; // enc_pkt.size = 0; // av_init_packet(&enc_pkt); // ret = avcodec_encode_video2(fmt_ctx->streams[stream_index]->codec, &enc_pkt, // NULL, &got_frame); // av_frame_free(NULL); // if (ret < 0) // break; // if (!got_frame) { // ret = 0; // break; // } // printf("Succeed to encode 1 frame! 编码成功1帧!n"); // /* mux encoded frame */ // ret = av_write_frame(fmt_ctx, &enc_pkt); // if (ret < 0) // break; // } // return ret; //} //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:%5dn", 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; uint8_t* picture_buf; AVFrame* picture; int size; //FILE *in_file = fopen("src01_480x272.yuv", "rb"); //Input YUV data 视频YUV源文件 FILE *in_file; fopen_s(&in_file, "d:/ds_480x272.yuv", "rb"); //Input YUV data 视频YUV源文件 int in_w = 480, in_h = 272;//宽高 //Frames to encode int framenum = 100; //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.hevc"; int ret = -1; //av_register_all(); //Method1 方法1.组合使用几个函数 //pFormatCtx = avformat_alloc_context(); Guess Format 猜格式 //fmt = av_guess_format(NULL, out_file, NULL); //pFormatCtx->oformat = fmt; //Method 2 方法2.更加自动化一些 avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file); fmt = pFormatCtx->oformat; //Output Format 注意输出路径 if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0) { printf("Failed to open output file! 输出文件打开失败"); return -1; } video_st = avformat_new_stream(pFormatCtx, NULL); video_st->time_base.num = 1; video_st->time_base.den = 25; if (video_st == NULL) { printf("can't allocate new streamn"); return -1; } //Param that must set //pCodecCtx = video_st->codec; pCodecCtx = avcodec_alloc_context3(NULL); ret = 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->time_base.num = 1; pCodecCtx->time_base.den = 25; pCodecCtx->bit_rate = 400000; pCodecCtx->gop_size = 250; //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 = NULL; //H.264 if (pCodecCtx->codec_id == AV_CODEC_ID_H264) { av_dict_set(¶m, "preset", "slow", 0); av_dict_set(¶m, "tune", "zerolatency", 0); } //H.265 //解决办法1:关闭B帧 CodecCtx->max_b_frames = 0; // //解决办法2:不设置zero-latency //av_dict_set(¶m, "tune", "zero-latency", 0); if (pCodecCtx->codec_id == AV_CODEC_ID_H265) { //av_dict_set(¶m, "x265-params", "qp=20", 0); av_dict_set(¶m, "preset", "ultrafast", 0); //av_dict_set(¶m, "tune", "zero-latency", 0); } //Dump 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; } ret = avcodec_open2(pCodecCtx, pCodec, ¶m); if (ret < 0) { printf("Failed to open encoder! 编码器打开失败!n"); return -1; } picture = av_frame_alloc(); //设置帧的格式、宽度和高度,否则会出现 //1.AVFrame.format is not set //2.AVFrame.width or height is not set picture->format = pCodecCtx->pix_fmt; picture->width = pCodecCtx->width; picture->height = pCodecCtx->height; size = av_image_get_buffer_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 1); picture_buf = (uint8_t *)av_malloc(size); //avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); ret = av_image_fill_arrays(picture->data, picture->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 写文件头 ret = avformat_write_header(pFormatCtx, NULL); AVPacket pkt; int y_size = pCodecCtx->width * pCodecCtx->height; av_new_packet(&pkt, y_size * 3); //fix:lost some frames int skipped_frame = 0; for (int i = 0; i<framenum; i++) { //Read YUV 读入YUV if (fread(picture_buf, 1, y_size * 3 / 2, in_file) < 0) { printf("Failed to read YUV data! 文件读取错误n"); return -1; } else if (feof(in_file)) { break; } picture->data[0] = picture_buf; // 亮度Y picture->data[1] = picture_buf + y_size; // U picture->data[2] = picture_buf + y_size * 5 / 4; // V //PTS picture->pts = i; int got_picture = 0; //Encode 编码 //int ret = avcodec_encode_video2(pCodecCtx, &pkt, picture, &got_picture); int ret = avcodec_send_frame(pCodecCtx, picture); 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 1 frame! 编码成功1帧!n"); pkt.stream_index = video_st->index; ret = av_write_frame(pFormatCtx, &pkt); av_packet_unref(&pkt); } else { skipped_frame++; } } Flush Encoder will lost same frames //ret = flush_encoder(pFormatCtx, pCodecCtx, 0); //if (ret < 0) { // printf("Flushing encoder failedn"); // return -1; //} //Fix: lost frame 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, picture); 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); av_packet_unref(&pkt); } } //Write file trailer 写文件尾 av_write_trailer(pFormatCtx); //Clean 清理 if (video_st) { avcodec_close(pCodecCtx); av_free(picture); av_free(picture_buf); } avio_close(pFormatCtx->pb); avformat_free_context(pFormatCtx); fclose(in_file); system("pause"); return 0; }
最后
以上就是负责鸵鸟最近收集整理的关于simplest_ffmpeg_video_encoder_pu(新版ffmpeg接口+libx265)的全部内容,更多相关simplest_ffmpeg_video_encoder_pu(新版ffmpeg接口+libx265)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复