ffmpeg新版本很多接口发生了变化,因此在使用时需要确定自己的版本,在这里使用的版本为4.1.4。
在此特别感谢雷博提供的相关文章:FFmpeg_雷霄骅的博客-优快云博客
-
ffmpeg视频解码
#include <iostream>
#include <stdio.h>
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include <libavutil/log.h>
#include <libavutil/timestamp.h>
}
#define ERROR_STR_SIZE 1024
int main() {
int ret = -1;
int err_code;
char errors[ERROR_STR_SIZE];
AVFormatContext *ifmt_ctx = NULL;
AVCodecContext *pCodecCtx;
const AVOutputFormat *ofmt = NULL;
AVStream *in_stream1 = NULL;
int vedio_stream_indes = 0;
// 文件最大时长,保证音频和视频数据长度一致
double max_duration = 0;
AVPacket *pkt;
int stream1 = 0, stream2 = 0;
const char* video = "E:\\jiao\\project\\162300.mp4";
/*//初始化网络,如果要解封装网络数据格式,则可调用该函数。
avformat_network_init();*/
//av_register_all();//高版本已弃用
//打开输入文件(可解析视频参数)
ifmt_ctx = avformat_alloc_context();
if ((err_code = avformat_open_input(&ifmt_ctx, video, 0, 0)) != 0) {
av_strerror(err_code, errors, ERROR_STR_SIZE);
return -1;
}
if (avformat_find_stream_info(ifmt_ctx, NULL) < 0) {
printf("Couldn't find stream information.\n");
return -1;
}
// 找到视频流下标
vedio_stream_indes = -1;
for (int i = 0; i < ifmt_ctx->nb_streams; i++) {
if (ifmt_ctx->streams[i]->codecpar->codec_type =&