13.FFmpeg学习笔记 - 解复用和解码(旧的解码API)

本篇文章,输入一个视频文件,分离出解码后的音视频数据,然后分别写入文件当中。

入口是:demux_decode函数。

下一篇文章会对以下代码中的一些关键函数做以说明。

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>

static const char *src_filename = "/Users/zhw/Desktop/resource/sintel_h264_aac.flv";
static const char *video_dst_filename = "/Users/zhw/Desktop/sintel.yuv";
static const char *audio_dst_filename = "/Users/zhw/Desktop/sintel.pcm";

static AVFormatContext *fmt_ctx = NULL;
static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx = NULL;
static AVStream *video_stream = NULL, *audio_stream = NULL;
static int video_stream_idx = -1, audio_stream_idx = -1;
static FILE *video_dst_file = NULL;
static FILE *audio_dst_file = NULL;
static int width, height;
static enum AVPixelFormat pix_fmt;
static AVFrame *frame = NULL;
static AVPacket pkt;
static int video_frame_count = 0;
static int audio_frame_count = 0;

static uint8_t *video_dst_data[4] = {NULL};
static int video_dst_linesize[4];
static int video_dst_bufsize;


static void demux_decode(void)
{
    int ret = 0, got_frame;

    /* 打开文件,创建AVFormatContext */
    if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
        printf("Could not open source file %s\n", src_filename);
        goto end;
    }
    
    /* 查找流信息 */
    if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
        printf("Could not find stream information\n");
        goto end;
    }
    
    //打开视频解码器
    if (open_codec_context(&video_stream_idx, &video_dec_ctx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) {
        //视频流
        video_stream = fmt_ctx->streams[video_stream_idx];
        
        //要写入的目标视频文件
        video_dst_file = fopen(video_dst_filename, "wb");
        if (!video_dst_file) {
            printf("could not open file %s\n", video_dst_filename);
            goto end;
        }
        
        //创建用于存放解码视频
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值