FFmpeg解封装通用代码

先定义好需要的类型

  AVFormatContext *ifmt_ctx = NULL;
    int video_index = -1;
    int audio_index = -1;
    AVPacket *packet = NULL;
    int ret = 0;
    char errors[ERROR_STRING_SIZE+1];

初始化 AVFormatContext


ifmt_ctx = avformat_alloc_context();
 ret = avformat_open_input(&ifmt_ctx,in_filename,NULL,NULL);

获取视频流和音频流

video_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
audio_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);

初始化packet

 packet = av_packet_alloc();
    av_init_packet(packet);//没有初始化buffer

循环读取buffer 里面的内容 分流操作

while (1){
        ret = av_read_frame(ifmt_ctx, packet);     // 不会去释放pkt的buf,如果我们外部不去释放,就会出现内存泄露
        if(ret < 0 ) {
            av_strerror(ret, errors, ERROR_STRING_SIZE);
            printf("av_read_frame failed:%s\n", errors);
            break;
        }
        if (packet->stream_index == video_index){

        }else if (packet->stream_index == audio_index){

        } else{
            av_packet_unref(packet);
        }

    }
在这里插入代码片//
// Created by zyc on 2022/3/4.
//

#include <stdio.h>

#include "libavutil/log.h"
#include "libavformat/avformat.h"

#define ERROR_STRING_SIZE 1024

#define ADTS_HEADER_LEN  7;

int main(int argc,char **argv){
    if(argc != 4) {
        printf("usage app input.mp4  out.h264 out.aac");
        return -1;
    }
    char *in_filename = argv[1];
    char *h264_filename = argv[1];
    char *aac_filename = argv[1];
    FILE *aac_fd = NULL;
    FILE *h264_fd =NULL;
    h264_fd = fopen(h264_filename, "wb");
    if(!h264_fd) {
        printf("fopen %s failed\n", h264_filename);
        return -1;
    }

    aac_fd = fopen(aac_filename, "wb");
    if(!aac_fd) {
        printf("fopen %s failed\n", aac_filename);
        return -1;
    }

    AVFormatContext *ifmt_ctx = NULL;
    int video_index = -1;
    int audio_index = -1;
    AVPacket *packet = NULL;
    int ret = 0;
    char errors[ERROR_STRING_SIZE+1];// 主要是用来缓存解析FFmpeg api返回值的错误string

    ifmt_ctx = avformat_alloc_context();
    if(!ifmt_ctx) {
        printf("avformat_alloc_context failed\n");

        return -1;
    }
    ret = avformat_open_input(&ifmt_ctx,in_filename,NULL,NULL);
    if(ret < 0) {
        av_strerror(ret, errors, ERROR_STRING_SIZE);
        printf("avformat_open_input failed:%d\n", ret);
        printf("avformat_open_input failed:%s\n", errors);
        avformat_close_input(&ifmt_ctx);
        //        go failed;
        return -1;
    }
    video_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
    if(video_index == -1) {
        printf("av_find_best_stream video_index failed\n");
        avformat_close_input(&ifmt_ctx);
        return -1;
    }

    audio_index = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
    if(audio_index == -1) {
        printf("av_find_best_stream audio_index failed\n");
        avformat_close_input(&ifmt_ctx);
        return -1;
    }

    packet = av_packet_alloc();
    av_init_packet(packet);
    while (1){
        ret = av_read_frame(ifmt_ctx, packet);     // 不会去释放pkt的buf,如果我们外部不去释放,就会出现内存泄露
        if(ret < 0 ) {
            av_strerror(ret, errors, ERROR_STRING_SIZE);
            printf("av_read_frame failed:%s\n", errors);
            break;
        }
        if (packet->stream_index == video_index){

        }else if (packet->stream_index == audio_index){

        } else{
            av_packet_unref(packet);
        }

    }
failed:
    if(h264_fd) {
        fclose(h264_fd);
    }
    if(aac_fd) {
        fclose(aac_fd);
    }
    if(packet)
        av_packet_free(&packet);
    if(ifmt_ctx)
        avformat_close_input(&ifmt_ctx);


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值