您的位置:首页 > 其它

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

2016-11-23 18:24 162 查看
#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: