ffmpeg之将avi格式的视频和音频分离




#include "stdafx.h"
#include <stdio.h>
extern "C"{


#include <libavcodec\avcodec.h>
#include <libavformat\avformat.h>
#include <libavutil\avutil.h>
}
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
int _tmain(int argc, _TCHAR* argv[])
{
av_register_all();
AVCodec* pCodec = NULL;
AVCodecContext* pCodecContext = NULL;
AVFormatContext* pFormatContext = NULL;

if(avformat_open_input(&pFormatContext, "C:/3.avi",NULL,NULL)<0)
printf("打开文件失败!\n");
avformat_find_stream_info(pFormatContext, NULL);
int index = -1;
for (int i = 0; i < pFormatContext->nb_streams; i++)
{
if (pFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
index = i;
break;
}
}
int indexaudio = -1;
for (int i = 0; i < pFormatContext->nb_streams; i++)
{
if (pFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
indexaudio = i;
break;
}
}


// 音频部分




// 视频部分
pCodecContext = pFormatContext->streams[index]->codec;
pCodec = avcodec_find_decoder(pCodecContext->codec_id);
if (pCodec == NULL)
printf("**********************\n");


if (avcodec_open2(pCodecContext, pCodec, NULL) < 0)
{
printf("对不起了!\n");
}


AVFrame* h264 = av_frame_alloc();
int Get = 0;
AVPacket* packet = (AVPacket*)av_malloc(sizeof(AVPacket));
av_init_packet(packet);
FILE* fph264 = fopen("test.h264", "wb+");
FILE* fphMP3 = fopen("test1.MP3", "wb+");
while (av_read_frame(pFormatContext, packet) >= 0)
{
if (packet->stream_index == index)
{
fwrite(packet->data, 1, packet->size, fph264);
printf("*************\n");
}
if (packet->stream_index == indexaudio)
{
fwrite(packet->data, 1, packet->size, fphMP3);
printf("--------------\n");
}
//avcodec_decode_video2(pCodecContext, h264, &Get, &packet);
if (Get)
{
    
}

}
av_free_packet(packet);
fclose(fph264);
fclose(fphMP3);
//printf("%d\n", pFormatContext->duration);
//printf("%d\n", pFormatContext->streams[0]->codec->width);
//printf("%d\n", pFormatContext->streams[0]->codec->height);

system("pause");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值