iOS FFmpeg实现本地录像MP4
2018-04-24 本文已影响69人
Goning
本文介绍利用FFmpeg在iOS上将H.264与AAC合成MP4文件,从而实现直播中本地录像的功能。
写MP4文件只需三步:
1.写文件头
2.填充数据
3.写文件尾
用到的api大致如下:
1.avformat_alloc_output_context2();
2.avformat_new_stream();
2.avio_open();
3.avformat_write_header();
4.av_interleaved_write_frame();
5.av_write_trailer();
下面是示例代码(此代码基于kxmovie源码)
1.写头:
- (void)startRecordAction {
//在沙盒中创建用于保存mp4的文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *homePath = [paths objectAtIndex:0];
NSCalendar *curCalendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *dateComponents = [curCalendar components:unitFlags fromDate:[NSDate date]];
filePath = [homePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld%02ld%02ld%02ld%02ld%02ld.mp4",(long)dateComponents.year,(long)dateComponents.month,(long)dateComponents.day,(long)dateComponents.hour, (long)dateComponents.minute, (long)dateComponents.second]];
const char *filename = [filePath cStringUsingEncoding:NSASCIIStringEncoding];
// find first video stream
if (!_formatCtx) {
return;
}
for (unsigned i=0; i<_formatCtx->nb_streams; i++) {
if (_formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
i_video_stream = _formatCtx->streams[i];
}
else if (_formatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
i_audio_stream = _formatCtx->streams[i];
}
}
if (i_video_stream == NULL) {
return;
}
else if (i_audio_stream == NULL) {
_isNoAudio = YES;
}
if (i_video_stream->codec->codec_id == AV_CODEC_ID_HEVC) {
_isStartWriteFile = NO;
return;
}
avformat_alloc_output_context2(&o_fmt_ctx, NULL, "mp4", filename);//初始化一个默认的AVFormatContext,用于输出文件
//设置参数
o_video_stream = avformat_new_stream(o_fmt_ctx, NULL);
AVCodecContext *v;
AVCodecContext *i;
v = o_video_stream->codec;
i = i_video_stream->codec;
v->bit_rate = 400000;
v->codec_id = i->codec_id;
v->codec_type = i->codec_type;
v->time_base.num = i->time_base.num;
v->time_base.den = i->time_base.den;
o_video_stream->time_base.num = i_video_stream->time_base.num;
o_video_stream->time_base.den = i_video_stream->time_base.den;
v->width = i->width;
v->height = i->height;
v->pix_fmt = i->pix_fmt;
v->flags = i->flags;
v->flags |= CODEC_FLAG_GLOBAL_HEADER;
v->extradata = i->extradata;
v->extradata_size = i->extradata_size;
if (!_isNoAudio) {
//音频参数
o_audio_stream = avformat_new_stream(o_fmt_ctx, NULL);
AVCodecContext *a;
AVCodecContext *i;
a = o_audio_stream->codec;
i = i_audio_stream->codec;
a->codec_id = AV_CODEC_ID_AAC;
a->codec_type = AVMEDIA_TYPE_AUDIO;
a->channel_layout = i->channel_layout;
a->audio_service_type = i->audio_service_type;
a->sample_rate = i->sample_rate;
a->channels = i->channels;
a->sample_fmt = i->sample_fmt;
a->bit_rate = i->bit_rate;
a->flags = i->flags;
a->flags |= CODEC_FLAG_GLOBAL_HEADER;
a->time_base.num = i->time_base.num;
a->time_base.den = i->time_base.den;
o_audio_stream->time_base.num = i_audio_stream->time_base.num;
o_audio_stream->time_base.den = i_audio_stream->time_base.den;
a->extradata = i->extradata;
a->extradata_size = i->extradata_size;
}
avio_open(&o_fmt_ctx->pb, filename, AVIO_FLAG_WRITE);//打开输出文件
avformat_write_header(o_fmt_ctx, NULL);//写文件头
_isStartWriteFile = YES;
}
2.写包:
AVPacket packet;
BOOL finished = NO;
while (!finished) {
if (av_read_frame(_formatCtx, &packet) < 0) {
_isEOF = YES;
break;
}
if (packet.size > 0 && _isStartWriteFile == YES) {
for (int i=0; i<packet.size; i++) {
if (packet.data[i]== (0x00)&&packet.data[i+1]==0x00&&packet.data[i+2]==0x00&&packet.data[i+3]==0x01&&((packet.data[i+4])&0x1f)==5) {//i帧
isIFrame = YES;
}
}
if (isIFrame==YES) {
AVPacket i_pkt;
av_init_packet(&i_pkt);
av_copy_packet(&i_pkt, &packet);
//这里要重新计算pts、dts
if (packet.stream_index==0) {
if (isvYES) {
i_pkt.pts -= lastv_pts;
i_pkt.dts -= lastv_dts;
}
else {
lastv_pts = packet.pts;
lastv_dts = packet.dts;
i_pkt.pts = packet.pts-lastv_pts;
i_pkt.dts = packet.dts-lastv_dts;
isvYES = YES;
}
}
else if (packet.stream_index==1) {
if (isaYES) {
i_pkt.pts -= lasta_pts;
i_pkt.dts -= lasta_dts;
}
else {
lasta_pts = packet.pts;
lasta_dts = packet.dts;
i_pkt.pts = packet.pts-lasta_pts;
i_pkt.dts = packet.dts-lasta_dts;
isaYES = YES;
}
}
av_interleaved_write_frame(o_fmt_ctx, &i_pkt);//写包
}
}
av_free_packet(&packet);
}
3.写尾:
- (void)stopRecordAction {
if (_isStartWriteFile == YES) {
av_write_trailer(o_fmt_ctx);//写文件尾
_isStartWriteFile = NO;
}
}