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;
}
#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;
}
相关文章推荐
- 如何用FFmpeg读取内存的PS/TS流,分离出视频和音频
- ffmpeg 视频抽取音频,视音频分离
- ffmpeg 视频抽取音频,视音频分离
- ffmpeg几个命令的使用:分离视频音频等
- 如何使用ffmpeg分离视频和音频t
- ffmpeg分离视频音频流
- ffmpeg+sdl教程----编写一个简单的播放器5(同步视频到音频)
- ffmpeg学习:ffmpeg下载,播放音频、视频,读取USB摄像头数据流并实时播放
- 使用FFMPEG类库分离出多媒体文件中的音频码流
- ffmpeg针对视频和音频裸码封装一点感想
- FFmpeg提取第一个视频合并第二个视频的音频同时加2个水印
- 音频视频解决方案:GStreamer/ffmpeg/ffdshow/directshow/vfw
- FFMPEG学习----分离视音频里的PCM数据
- 音频视频解决方案:GStreamer-ffmpeg-ffdshow-directshow-vfw
- ffmpeg音视频分离命令
- ffmpeg实现转码一个普通视频文件为视频mpeg4,音频mp3的功能的程序
- 音视频编解码问题:javaCV如何快速进行音频预处理和解复用编解码(基于javaCV-FFMPEG)
- 使用FFMPEG类库分离出多媒体文件中的音频码流
- lame MP3编码器 FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。
- Android本地视频播放器开发--ffmpeg解码视频文件中的音频(1)