使用FFmpeg库对视频进行编码


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
#import "avformat.h"
#import "timestamp.h"
#import "avcodec.h"
#import "opt.h"
#import "samplefmt.h"
#import "swscale.h"
#import "avutil.h"

static int encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *out){
int ret = -1;

ret = avcodec_send_frame(ctx, frame);
if(ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to send frame to encoder!\n");
goto _END;
}

while( ret >= 0){
ret = avcodec_receive_packet(ctx, pkt);
if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){
return 0;
} else if( ret < 0) {
return -1; //退出tkyc
}

fwrite(pkt->data, 1, pkt->size, out);
av_packet_unref(pkt);
}
_END:
return 0;
}

- (IBAction)encodeVideoAction:(id)sender {

// 处理好的文件存储路径
NSString * dstpath = [NSString stringWithFormat:@"%@/Library/Caches/1-1.h264",NSHomeDirectory()];
char dstChar [ 1024 ];
strcpy (dstChar ,( char *)[dstpath UTF8String]);

//编码格式
NSString * type = @"libx264";
char typeChar [ 1024 ];
strcpy (typeChar ,( char *)[type UTF8String]);

char * argv[2];
argv[0] = dstChar;
argv[1] = typeChar;

NSLog(@"存储路径:%@", dstpath);

encodeVideo(2, argv);
}

#pragma mark 视频编码
void encodeVideo(int argc, char* argv[]) {
int ret = -1;

FILE *f = NULL;

char *dst = NULL;
char *codecName = NULL;

const AVCodec *codec = NULL;
AVCodecContext *ctx = NULL;

AVFrame *frame = NULL;
AVPacket *pkt = NULL;

av_log_set_level(AV_LOG_DEBUG);

//1. 输入参数
if(argc < 2){
av_log(NULL, AV_LOG_ERROR, "arguments must be more than 2\n");
goto _ERROR;
}

dst = argv[0];
codecName = argv[1];

//2. 查找编码器
codec = avcodec_find_encoder_by_name(codecName);
if(!codec){
av_log(NULL, AV_LOG_ERROR, "don't find Codec: %s", codecName);
goto _ERROR;
}

//3. 创建编码器上下文
ctx = avcodec_alloc_context3(codec);
if(!ctx){
av_log(NULL, AV_LOG_ERROR, "NO MEMRORY\n");
goto _ERROR;
}

//4. 设置编码器参数
ctx->width = 640;
ctx->height = 480;
ctx->bit_rate = 500000;// 码率
//时间基
ctx->time_base = (AVRational){1, 25};
//帧率
ctx->framerate = (AVRational){25, 1};

ctx->gop_size = 10;
ctx->max_b_frames = 1;
ctx->pix_fmt = AV_PIX_FMT_YUV420P;

if(codec->id == AV_CODEC_ID_H264){
av_opt_set(ctx->priv_data, "preset", "slow", 0);
}

//5. 编码器与编码器上下文绑定到一起
ret = avcodec_open2(ctx, codec , NULL);
if(ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Don't open codec: %s \n", av_err2str(ret));
goto _ERROR;
}

//6. 创建输出文件
f = fopen(dst, "wb");// w写内容,b二进制
if(!f){
av_log(NULL, AV_LOG_ERROR, "Don't open file:%s", dst);
goto _ERROR;
}

//7. 创建AVFrame
frame = av_frame_alloc();
if(!frame){
av_log(NULL, AV_LOG_ERROR, "NO MEMORY!\n");
goto _ERROR;
}
frame->width = ctx->width;
frame->height = ctx->height;
frame->format = ctx->pix_fmt;

ret = av_frame_get_buffer(frame, 0);
if(ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Could not allocate the video frame \n");
goto _ERROR;
}

//8. 创建AVPacket
pkt = av_packet_alloc();
if(!pkt){
av_log(NULL, AV_LOG_ERROR, "NO MEMORY!\n");
goto _ERROR;
}

//9. 生成视频内容
for(int i=0; i<25; i++){
ret = av_frame_make_writable(frame);
if(ret < 0) {
break;
}

//Y分量
for(int y = 0; y < ctx->height; y++){
for(int x=0; x < ctx->width; x++){
frame->data[0][y*frame->linesize[0]+x] = x + y + i * 3;
}
}

//UV分量
for(int y=0; y< ctx->height/2; y++){
for(int x=0; x < ctx->width/2; x++){
frame->data[1][y * frame->linesize[1] + x ] = 128 + y + i * 2;
frame->data[2][y * frame->linesize[2] + x ] = 64 + x + i * 5;
}
}

frame->pts = i;

//10. 编码
ret = encode(ctx, frame, pkt, f);
if(ret == -1){
goto _ERROR;
}
}
//10. 编码
encode(ctx, NULL, pkt, f);
_ERROR:
//ctx
if(ctx){
avcodec_free_context(&ctx);
}

//avframe
if(frame){
av_frame_free(&frame);
}

//avpacket
if(pkt){
av_packet_free(&pkt);
}

//dst
if(f){
fclose(f);
}
}