#include <QtCore/QCoreApplication>
#include <stdio.h>
#include <QString>
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavdevice/avdevice.h"
}
#include <qDebug>
#include <QTextCodec>
//Show Dshow Device
void show_dshow_device(){
AVFormatContext *pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options,"list_devices","true",0);
AVInputFormat *iformat = av_find_input_format("dshow");
printf("========Device Info=============\n");
avformat_open_input(&pFormatCtx,"video=dummy",iformat,&options);
printf("================================\n");
}
//Show Dshow Device Option
void show_dshow_device_option(){
AVFormatContext *pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options,"list_options","true",0);
AVInputFormat *iformat = av_find_input_format("dshow");
printf("========Device Option Info======\n");
avformat_open_input(&pFormatCtx,"video=Integrated Camera",iformat,&options);
printf("================================\n");
}
//Show AVFoundation Device
void show_avfoundation_device(){
AVFormatContext *pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options,"list_devices","true",0);
AVInputFormat *iformat = av_find_input_format("avfoundation");
printf("==AVFoundation Device Info===\n");
avformat_open_input(&pFormatCtx,"",iformat,&options);
printf("=============================\n");
}
#define QStringLiteral(str) QString::fromUtf8(str, sizeof(str) - 1)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));//支持中文
AVFormatContext *pFormatCtx;
int i, audioindex;
AVCodecContext *aCodecCtx;
AVCodec *aCodec = NULL;
AVFrame *aFrame;
av_register_all();
// avformat_network_init(); //初始化网络
avdevice_register_all();//Register Device
//Show Dshow Device
show_dshow_device();
//Show Device Options
// show_dshow_device_option();
//Show VFW Options
// show_vfw_device();
pFormatCtx = avformat_alloc_context();
QString audioDeviceName = QStringLiteral("audio=麦克风 (Realtek High Definition Audio)");
// QString audioDeviceName = QString("audio=麦克风 (Realtek High Definition Audio)");
AVInputFormat *ifmt = av_find_input_format("dshow");
QByteArray qbArray = audioDeviceName.toUtf8();
char* audioName = qbArray.data();
if(avformat_open_input(&pFormatCtx,audioName,ifmt,NULL)!=0){
fprintf(stderr,"Couldn't open input stream.(无法打开输入流)\n");
return -1;
}
audioindex = -1;
aCodecCtx = NULL;
for(i=0; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
{
audioindex=i;
break;
}
if(audioindex==-1)
{
printf("Didn't find a video stream.(没有找到视频流)\n");
return -1;
}
aCodecCtx = pFormatCtx->streams[audioindex]->codec;
aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
if(aCodec == NULL)
{
printf("audio Codec not found.\n");
return -1;
}
if(avcodec_open2(aCodecCtx, aCodec,NULL)<0)
{
printf("Could not open video codec.\n");
return -1;
}
aFrame=avcodec_alloc_frame();
int ret, got_frame;
AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket));
FILE *fp_pcm=fopen("output.pcm","wb");
///这里打印出音频的信息
qDebug()<<"audio info:";
qDebug()<<"audio info:"<<aCodecCtx->sample_fmt<<aCodecCtx->bit_rate<<aCodecCtx->sample_rate<<aCodecCtx->channels;
float Time = 0;
for(int i=0;;i++)
{
if (Time > 30) break; //就采集30秒
if(av_read_frame(pFormatCtx, packet) < 0)
{
break;
}
if(packet->stream_index==audioindex)
{
ret = avcodec_decode_audio4(aCodecCtx, aFrame, &got_frame, packet);
// qDebug()<<i<<ret<<audioindex;
if(ret < 0)
{
fprintf(stderr,"Audio Error.\n");
return -1;
}
if (got_frame)
{
int pcmSize = av_samples_get_buffer_size(NULL,aCodecCtx->channels, aFrame->nb_samples,aCodecCtx->sample_fmt, 1);
uint8_t * pcmBuffer = aFrame->data[0];
float useTime = aFrame->nb_samples * 1.0 / aCodecCtx->sample_rate;
Time += useTime;
qDebug()<<i<<Time<<useTime;
fwrite(pcmBuffer,1,pcmSize,fp_pcm); //写入文件
}
}
av_free_packet(packet);
}
fclose(fp_pcm);
if (aFrame != NULL)
{
av_free(aFrame);
aFrame = NULL;
}
if (aCodecCtx != NULL)
avcodec_close(aCodecCtx);
avformat_close_input(&pFormatCtx);
avformat_free_context(pFormatCtx);
return a.exec();
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ffmpeg 音频数据采集 (143个子文件)
libavutil.dll.a 275KB
libavcodec.dll.a 142KB
libavformat.dll.a 103KB
libavfilter.dll.a 54KB
libswscale.dll.a 23KB
libswresample.dll.a 14KB
libavdevice.dll.a 11KB
libpostproc.dll.a 7KB
main.cpp 5KB
avutil-54.def 10KB
avcodec-56.def 6KB
avformat-56.def 4KB
avfilter-5.def 2KB
swscale-3.def 781B
avdevice-56.def 491B
swresample-1.def 433B
postproc-53.def 233B
avcodec-56.dll 18.74MB
avcodec-56.dll 18.74MB
avformat-56.dll 5.57MB
avformat-56.dll 5.57MB
avfilter-5.dll 2.21MB
avfilter-5.dll 2.21MB
avdevice-56.dll 1.3MB
avdevice-56.dll 1.3MB
avutil-54.dll 442KB
avutil-54.dll 442KB
swscale-3.dll 437KB
swscale-3.dll 437KB
swresample-1.dll 264KB
swresample-1.dll 264KB
postproc-53.dll 119KB
postproc-53.dll 119KB
ffplay.exe 464KB
ffmpeg.exe 317KB
ffprobe.exe 149KB
VideoRecorder5.exe 26KB
VideoRecorder5.vcxproj.filters 1KB
avcodec.h 176KB
avformat.h 100KB
avfilter.h 56KB
opt.h 37KB
pixfmt.h 32KB
frame.h 23KB
swresample.h 19KB
avio.h 18KB
intreadwrite.h 18KB
avdevice.h 16KB
mem.h 14KB
old_pix_fmts.h 14KB
pixdesc.h 14KB
common.h 14KB
avstring.h 13KB
swscale.h 12KB
old_codec_ids.h 10KB
log.h 10KB
samplefmt.h 10KB
buffer.h 10KB
channel_layout.h 9KB
avutil.h 8KB
imgutils.h 8KB
dict.h 8KB
bprint.h 8KB
buffersink.h 7KB
parseutils.h 7KB
vdpau.h 7KB
version.h 6KB
xvmc.h 6KB
error.h 5KB
vda.h 5KB
timecode.h 5KB
eval.h 5KB
mathematics.h 5KB
fifo.h 5KB
buffersrc.h 5KB
cpu.h 5KB
audio_fifo.h 4KB
stdint.h 4KB
attributes.h 4KB
rational.h 4KB
vaapi.h 4KB
version.h 4KB
dv_profile.h 4KB
stereo3d.h 4KB
hash.h 4KB
asrc_abuffer.h 3KB
downmix_info.h 3KB
display.h 3KB
threadmessage.h 3KB
avfft.h 3KB
postprocess.h 3KB
version.h 3KB
hmac.h 3KB
bswap.h 3KB
crc.h 3KB
timestamp.h 3KB
file.h 2KB
version.h 2KB
avcodec.h 2KB
dxva2.h 2KB
共 143 条
- 1
- 2
资源评论
- zhangnianqian2017-07-11编译不了,太山寨
- 归途醉染2019-03-04这个渣渣,完全来骗积分的,直接复制别人qt的工程放到vs2010里来,里面还有QT里面特有的函数,已举报!坚决打击这种骗积分的!
小学生009
- 粉丝: 55
- 资源: 61
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功