下載地址:
https://ffmpeg.zeranoe.com/builds/
.h文件
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "libavutil/mem.h"
#include "libavutil/fifo.h"
#include "libswscale/swscale.h"
}
//dll導出lib
implib -a avcodec.lib avcodec-58.dll
....
.....
視頻解碼
- 打開輸入文件
avformat_open_input
- 找到視頻流
av_find_best_stream
- 找到對應的解碼器
avcodec_find_decoder
- 初始化一個編解碼上下文
avcodec_alloc_context3
- 拷貝流參數到編解碼上下文中
avcodec_parameters_to_context
- 打開解碼器
avcodec_open2
- 讀取視頻幀
av_read_frame
- 發送等待解碼幀
avcodec_send_packet
- 接收解碼后frame數據
avcodec_receive_frame
作者:MzDavid
鏈接:https://www.jianshu.com/p/3886bc5846c9
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
//打開視頻文件
int ret;
// const char *out_filename;
const char *in_filename = "g:\\su\\test.mp4";
AVFormatContext *fmt_ctx = NULL;
const AVCodec *codec;
AVCodecContext *codeCtx = NULL;
AVStream *stream = NULL;
int stream_index;
AVPacket avpkt;
int frame_count;
AVFrame *frame;
// 1
if (avformat_open_input(&fmt_ctx, in_filename, NULL, NULL) < 0) {
printf("Could not open source file %s\n", in_filename);
exit(1);
}
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
printf("Could not find stream information\n");
exit(1);
}
av_dump_format(fmt_ctx, 0, in_filename, 0);
av_init_packet(&avpkt);
avpkt.data = NULL;
avpkt.size = 0;
// 2
stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (ret < 0) {
fprintf(stderr, "Could not find %s stream in input file '%s'\n",
av_get_media_type_string(AVMEDIA_TYPE_VIDEO), in_filename);
return ;
}
stream = fmt_ctx->streams[stream_index];
// 3
codec = avcodec_find_decoder(stream->codecpar->codec_id);
if (codec == NULL) {
return ;
}
// 4
codeCtx = avcodec_alloc_context3(NULL);
if (!codeCtx) {
fprintf(stderr, "Could not allocate video codec context\n");
return;
}
// 5
if ((ret = avcodec_parameters_to_context(codeCtx, stream->codecpar)) < 0) {
fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
av_get_media_type_string(AVMEDIA_TYPE_VIDEO));
return;
}
// 6
avcodec_open2(codeCtx, codec, NULL);
//初始化frame,解碼后數據
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
frame_count = 0;
char buf[1024];
// 7
while (av_read_frame(fmt_ctx, &avpkt) >= 0) {
if (avpkt.stream_index == stream_index) {
// 8
int re = avcodec_send_packet(codeCtx, &avpkt);
if (re < 0) {
continue;
}
// 9 這里必須用while(),因為一次avcodec_receive_frame可能無法接收到所有數據
while (avcodec_receive_frame(codeCtx, frame) == 0) {
//
}
frame_count++;
}
av_packet_unref(&avpkt);
}