FFmpeg学习之开发Mac播放器(一):解码视频

  • 最近项目中涉及到视频处理,所以从头开始学习FFmpeg。期间遇到了很多问题,踩了许多的坑,把我学习的经历记录下来,方便自己日后查看,也方便给需要的人提供帮助。由于刚开始学习,只重视功能的实现,代码像流水一样写下来,没有封装更别提优雅,所以请看的人多多包含。

  • 为了方便调试,我使用的mac OS平台,没有使用iOS平台,但是代码基本上差不多,所有iOS也能适用。

解码核心代码
- (void)decodeVideo {

    //av_register_all(); FFmpeg 4.0废弃

    NSString * videoPath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp4"];
    pFormatCtx = avformat_alloc_context();

    if ((avformat_open_input(&pFormatCtx, videoPath.UTF8String, NULL, NULL)) != 0) {
        NSLog(@"Could not open input stream");
        return;
    }

    if ((avformat_find_stream_info(pFormatCtx, NULL)) < 0) {
        NSLog(@"Could not find stream information");
        return;
    }

    for (NSInteger i = 0; i < pFormatCtx->nb_streams; i++) {
        if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoIndex = i;  //视频流的索引
            break;
        }
    }

    if (videoIndex == NSNotFound) {
        NSLog(@"Did not find a video stream");
        return;
    }

    // FFmpeg 3.1 以上AVStream::codec被替换为AVStream::codecpar
    pCodec = avcodec_find_decoder(pFormatCtx->streams[videoIndex]->codecpar->codec_id);
    pCodecCtx = avcodec_alloc_context3(pCodec);
    avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoIndex]->codecpar);

    if (pCodec == NULL) {
        NSLog(@"Could not open codec");
        return;
    }

    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
        NSLog(@"Could not open codec");
        return;
    }

    av_dump_format(pFormatCtx, 0, videoPath.UTF8String, 0);

    AVPacket * packet = av_packet_alloc();

    while (av_read_frame(pFormatCtx, packet) >= 0) {
        if (packet->stream_index == videoIndex) {  //解码视频流
            //FFmpeg 3.0之后avcodec_send_packet和avcodec_receive_frame成对出现用于解码,包括音频和视频的解码,avcodec_decode_video2和avcodec_decode_audio4被废弃
            NSInteger ret = avcodec_send_packet(pCodecCtx, packet);
            if (ret < 0) {
                NSLog(@"send packet error");
                continue;
            }
            AVFrame * frame = av_frame_alloc();
            ret = avcodec_receive_frame(pCodecCtx, frame);
            if (ret < 0) {
                NSLog(@"receive frame error");
                continue;
            }
            av_frame_free(&frame); //释放frame,会先调用av_frame_unref()清除frame中数据然后释放frame
            av_packet_unref(packet); //清除packet中数据设为默认值,以便下次使用
            /*
             frame中data存放解码出的yuv数据,data[0]中是y数据,data[1]中是u数据,data[2]中是v数据,linesize对应的数据长度
             */
        }
    }
    av_packet_free(&packet); //释放packet
    avcodec_free_context(&pCodecCtx);
    avformat_close_input(&pFormatCtx);
    avformat_free_context(pFormatCtx);
}

你可能感兴趣的:(FFmpeg学习之开发Mac播放器(一):解码视频)