本篇文章,输入一个视频文件,分离出解码后的音视频数据,然后分别写入文件当中。
入口是: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;
}
//创建用于存放解码视频