#include <stdio.h>
#define __STDC_CONSTANT_MACROS
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
};
//setup_array函数摘自ffmpeg例程
static void setup_array(uint8_t* out[SWR_CH_MAX], AVFrame* in_frame, int format, int samples)
{
if (av_sample_fmt_is_planar((AVSampleFormat)format))
{
int i;
int plane_size = av_get_bytes_per_sample((AVSampleFormat)(format & 0xFF)) * samples;
format &= 0xFF;
//从decoder出来的frame中的data数据不是连续分布的,所以不能这样写:in_frame->data[0]+i*plane_size;
for (i = 0; i < in_frame->channels; i++)
{
out[i] = in_frame->data[i];
}
}
else
{
out[0] = in_frame->data[0];
}
}
int main()
{
char filepath[] = "bird.mp3";
AVFormatContext *pFormatCtx;
int i,videoindex;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameYUV;
AVPacket *packet;
struct SwsContext *img_convert_ctx;
int ret, got_audio;
FILE *fp_pcm;
fp_pcm = fopen("output.pcm", "wb+");
av_register_all();
avformat_network_init();
pFormatCtx = avformat_alloc_context();
if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0){
printf("Couldn't open input stream.\n");
return -1;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0){
printf("Couldn't find stream information.\n");
return -1;
}
videoindex = -1;
for (i = 0; i < pFormatCtx->nb_streams; i++)
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO){
videoindex = i;
break;
}
if (videoindex == -1){
printf("Didn't find a video stream.\n");
return -1;
}
pCodecCtx = pFormatCtx->streams[videoindex]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec == NULL){
printf("Codec not found.\n");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0){
printf("Could not open codec.\n");
return -1;
}
pFrame = av_frame_alloc();
//pFrameYUV = av_frame_alloc();
//uint8_t *out_buffer = (uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));
//avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
packet = (AVPacket *)av_malloc(sizeof(AVPacket));
//Output Information-----------------------------
printf("------------- File Information ------------------\n");
av_dump_format(pFormatCtx, 0, filepath, 0);
printf("-------------------------------------------------\n");
while (av_read_frame(pFormatCtx, packet) >= 0){
if (packet->stream_index == videoindex){
//Decode
ret = avcodec_decode_audio4(pCodecCtx, pFrame, &got_audio, packet);
if (got_audio){
//输入也可能是分平面的,所以要做如下处理
uint8_t* m_ain[SWR_CH_MAX];
setup_array(m_ain, pFrame, pCodecCtx->sample_fmt, pFrame->nb_samples);
fwrite(pFrame->data[0], 1, pFrame->linesize[0], fp_pcm);
}
}
}
av_free(pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
ffmpeg MP3 转换 pcm
共123个文件
h:87个
dll:8个
lib:8个
4星 · 超过85%的资源 需积分: 34 115 下载量 69 浏览量
2017-02-10
09:38:26
上传
评论 4
收藏 13.95MB ZIP 举报
温馨提示
学习使用ffmpeg将MP3文件格式转换至PCM文件格式; vs2013编译测试通过; 主要参考雷神的博客和源码;
资源推荐
资源详情
资源评论
收起资源包目录
ffmpeg MP3 转换 pcm (123个子文件)
ffmpeg_pcm.cpp 3KB
avcodec-55.dll 18.06MB
avformat-55.dll 5.1MB
avfilter-4.dll 1.94MB
avdevice-55.dll 1.28MB
swscale-2.dll 424KB
avutil-52.dll 409KB
swresample-0.dll 270KB
postproc-52.dll 119KB
ffmpeg_pcm.vcxproj.filters 962B
avcodec.h 171KB
avformat.h 97KB
avfilter.h 56KB
opt.h 34KB
pixfmt.h 28KB
frame.h 22KB
avio.h 18KB
intreadwrite.h 18KB
avdevice.h 16KB
old_pix_fmts.h 14KB
mem.h 14KB
common.h 14KB
pixdesc.h 13KB
avstring.h 12KB
swscale.h 12KB
swresample.h 12KB
old_codec_ids.h 10KB
samplefmt.h 10KB
log.h 10KB
buffer.h 10KB
channel_layout.h 9KB
avutil.h 8KB
_mingw.h 8KB
imgutils.h 8KB
bprint.h 8KB
buffersink.h 7KB
parseutils.h 7KB
dict.h 6KB
vdpau.h 6KB
xvmc.h 6KB
stdint.h 6KB
inttypes.h 6KB
version.h 5KB
timecode.h 5KB
eval.h 5KB
mathematics.h 5KB
buffersrc.h 5KB
error.h 5KB
cpu.h 5KB
fifo.h 5KB
version.h 4KB
audio_fifo.h 4KB
attributes.h 4KB
rational.h 4KB
vda.h 4KB
vaapi.h 4KB
avcodec.h 4KB
stereo3d.h 4KB
version.h 3KB
asrc_abuffer.h 3KB
downmix_info.h 3KB
avfft.h 3KB
bswap.h 3KB
postprocess.h 3KB
hmac.h 3KB
version.h 3KB
crc.h 3KB
timestamp.h 3KB
file.h 2KB
dxva2.h 2KB
blowfish.h 2KB
version.h 2KB
avassert.h 2KB
base64.h 2KB
lzo.h 2KB
sha512.h 2KB
ripemd.h 2KB
lfg.h 2KB
md5.h 2KB
sha.h 2KB
xtea.h 2KB
version.h 2KB
aes.h 2KB
intfloat.h 2KB
version.h 2KB
version.h 2KB
replaygain.h 2KB
adler32.h 2KB
intfloat_readwrite.h 2KB
random_seed.h 1KB
time.h 1KB
murmur3.h 1KB
macros.h 1KB
avfiltergraph.h 975B
avconfig.h 211B
ffversion.h 128B
audioconvert.h 82B
vc120.idb 147KB
ffmpeg_pcm.lastbuildstate 152B
avutil.lib 321KB
共 123 条
- 1
- 2
资源评论
- 飞行小熊猫2018-07-31谢谢,可以用,转换出来的pcm文件可以用Audacity导入原始数据,设置 16位/44100HZ/单声道 播放
- 夕阳雨2017-12-11是个C的项目,不是需要的。
- 殊慧2017-10-12写的不错,就是看不懂
- ls19898222019-06-05够用,仅供参考
- IAmLinGe2020-04-11有些接口ffmpeg已经弃用了
知不知知不知
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功