先定义好需要的类型
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);
}