- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个程序来使用 FFmpeg 和 libao 读取和播放音频文件。我一直按照 FFmpeg documentation 中概述的程序进行操作。使用新的 avcodec_send_packet
解码音频和 avcodec_receive_frame
函数,但是我能找到的例子很少(FFmpeg 文档中的例子要么不使用 libavformat 要么使用已弃用的 avcodec_decode_audio4
)。我的很多程序都基于 transcode_aac.c FFmpeg 文档中的示例(最多 init_resampler
),但这也使用了已弃用的解码功能。
我相信我的程序的解码部分可以正常工作,但我需要重新采样音频,以便将其转换为交错格式以发送到 libao,为此我正在尝试使用 libswresample。每当程序以其当前状态运行时,它会输出(多次)“错误重采样:输出已更改”。我一直在使用的测试文件只是我手头上的一个 YouTube 翻录。 ffprobe 将唯一的流报告为:
Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
#include<stdio.h>
#include<libavcodec/avcodec.h>
#include<libavformat/avformat.h>
#include<libavutil/avutil.h>
#include<libswresample/swresample.h>
#include<ao/ao.h>
#define OUTPUT_CHANNELS 2
#define OUTPUT_RATE 44100
#define BUFFER_SIZE 192000
#define OUTPUT_BITS 16
#define OUTPUT_FMT AV_SAMPLE_FMT_S16
static char *errtext (int err) {
static char errbuff[256];
av_strerror(err,errbuff,sizeof(errbuff));
return errbuff;
}
static int open_audio_file (const char *filename, AVFormatContext **context, AVCodecContext **codec_context) {
AVCodecContext *avctx;
AVCodec *codec;
int ret;
int stream_id;
int i;
// Open input file
if ((ret = avformat_open_input(context,filename,NULL,NULL)) < 0) {
fprintf(stderr,"Error opening input file '%s': %s\n",filename,errtext(ret));
*context = NULL;
return ret;
}
// Get stream info
if ((ret = avformat_find_stream_info(*context,NULL)) < 0) {
fprintf(stderr,"Unable to find stream info: %s\n",errtext(ret));
avformat_close_input(context);
return ret;
}
// Find the best stream
if ((stream_id = av_find_best_stream(*context,AVMEDIA_TYPE_AUDIO,-1,-1,&codec,0)) < 0) {
fprintf(stderr,"Unable to find valid audio stream: %s\n",errtext(stream_id));
avformat_close_input(context);
return stream_id;
}
// Allocate a decoding context
if (!(avctx = avcodec_alloc_context3(codec))) {
fprintf(stderr,"Unable to allocate decoder context\n");
avformat_close_input(context);
return AVERROR(ENOMEM);
}
// Initialize stream parameters
if ((ret = avcodec_parameters_to_context(avctx,(*context)->streams[stream_id]->codecpar)) < 0) {
fprintf(stderr,"Unable to get stream parameters: %s\n",errtext(ret));
avformat_close_input(context);
avcodec_free_context(&avctx);
return ret;
}
// Open the decoder
if ((ret = avcodec_open2(avctx,codec,NULL)) < 0) {
fprintf(stderr,"Could not open codec: %s\n",errtext(ret));
avformat_close_input(context);
avcodec_free_context(&avctx);
return ret;
}
*codec_context = avctx;
return 0;
}
static void init_packet (AVPacket *packet) {
av_init_packet(packet);
packet->data = NULL;
packet->size = 0;
}
static int init_resampler (AVCodecContext *codec_context, SwrContext **resample_context) {
int ret;
// Set resampler options
*resample_context = swr_alloc_set_opts(NULL,
av_get_default_channel_layout(OUTPUT_CHANNELS),
OUTPUT_FMT,
codec_context->sample_rate,
av_get_default_channel_layout(codec_context->channels),
codec_context->sample_fmt,
codec_context->sample_rate,
0,NULL);
if (!(*resample_context)) {
fprintf(stderr,"Unable to allocate resampler context\n");
return AVERROR(ENOMEM);
}
// Open the resampler
if ((ret = swr_init(*resample_context)) < 0) {
fprintf(stderr,"Unable to open resampler context: %s\n",errtext(ret));
swr_free(resample_context);
return ret;
}
return 0;
}
static int init_frame (AVFrame **frame) {
if (!(*frame = av_frame_alloc())) {
fprintf(stderr,"Could not allocate frame\n");
return AVERROR(ENOMEM);
}
return 0;
}
int main (int argc, char *argv[]) {
AVFormatContext *context = 0;
AVCodecContext *codec_context;
SwrContext *resample_context = NULL;
AVPacket packet;
AVFrame *frame = 0;
AVFrame *resampled = 0;
int16_t *buffer;
int ret, packet_ret, finished;
ao_device *device;
ao_sample_format format;
int default_driver;
if (argc != 2) {
fprintf(stderr,"Usage: %s <filename>\n",argv[0]);
return 1;
}
av_register_all();
printf("Opening file...\n");
if (open_audio_file(argv[1],&context,&codec_context) < 0)
return 1;
printf("Initializing resampler...\n");
if (init_resampler(codec_context,&resample_context) < 0) {
avformat_close_input(&context);
avcodec_free_context(&codec_context);
return 1;
}
// Setup libao
printf("Starting audio device...\n");
ao_initialize();
default_driver = ao_default_driver_id();
format.bits = OUTPUT_BITS;
format.channels = OUTPUT_CHANNELS;
format.rate = codec_context->sample_rate;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
if ((device = ao_open_live(default_driver,&format,NULL)) == NULL) {
fprintf(stderr,"Error opening audio device\n");
avformat_close_input(&context);
avcodec_free_context(&codec_context);
swr_free(&resample_context);
return 1;
}
// Mainloop
printf("Beginning mainloop...\n");
init_packet(&packet);
// Read packets until done
while (1) {
packet_ret = av_read_frame(context,&packet);
// Send a packet
if ((ret = avcodec_send_packet(codec_context,&packet)) < 0)
fprintf(stderr,"Error sending packet to decoder: %s\n",errtext(ret));
av_packet_unref(&packet);
while (1) {
if (!frame)
frame = av_frame_alloc();
ret = avcodec_receive_frame(codec_context,frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) // Need more input
break;
else if (ret < 0) {
fprintf(stderr,"Error receiving frame: %s\n",errtext(ret));
break;
}
// We have a valid frame, need to resample it
if (!resampled)
resampled = av_frame_alloc();
resampled->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
resampled->sample_rate = codec_context->sample_rate;
resampled->format = OUTPUT_FMT;
if ((ret = swr_convert_frame(resample_context,resampled,frame)) < 0) {
fprintf(stderr,"Error resampling: %s\n",errtext(ret));
} else {
ao_play(device,(char*)resampled->extended_data[0],resampled->linesize[0]);
}
av_frame_unref(resampled);
av_frame_unref(frame);
}
if (packet_ret == AVERROR_EOF)
break;
}
printf("Closing file and freeing contexts...\n");
avformat_close_input(&context);
avcodec_free_context(&codec_context);
swr_free(&resample_context);
printf("Closing audio device...\n");
ao_close(device);
ao_shutdown();
return 0;
}
resampled
框架在传递给
swr_convert_frame
之前需要设置某些属性.我还添加了
av_packet_unref
和
av_frame_unref
,但我仍然不确定在哪里找到它们的最佳位置。
最佳答案
ao_play(device,(char*)resampled->extended_data[0],resampled->linesize[0]);
ao_play(device, (char*)resampled->extended_data[0], av_sample_get_buffer_size(resampled->linesize, resampled->channels, resampled->nb_samples, resampled->format, 0));
关于c - 使用 FFmpeg 解码和重采样音频以使用 libao 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44191819/
我从 aplay -l, lsusb 得到以下内容: pi@raspberrypi:~ $ aplay -l **** List of PLAYBACK Hardware Devices **** c
我正在尝试让一个简单的 C 程序播放 AIFF 或 WAV 文件。根据我在 http://www.xiph.org/ao/doc/ 看到的内容,这应该可以工作,但是无论我输入什么文件,它都会发出嗡嗡声
我试图让 PCM 在 Python 中听起来有点像,但我尝试过的所有包都太多了,或者没有记录或死了,所以我决定用 libao 做一个简单的包。 我用作起点 this source code xiph.
我正在尝试让 libao 库在 Qt 中运行。这是我目前所拥有的。 #include ... static int audio_driver; static ao_device *audio_dev
我编写了一个小程序来播放 PCM 音频文件,使用来自 github 的引用,并对我的应用程序稍作修改。该程序编译良好,但我在依赖项和 ALSA 设备方面遇到了一些运行时问题。 我收到的错误是: ERR
我正在尝试编写一个程序来使用 FFmpeg 和 libao 读取和播放音频文件。我一直按照 FFmpeg documentation 中概述的程序进行操作。使用新的 avcodec_send_pack
我正在使用 libao 编写应用程序用于音频输出。该部分我的程序中调用 libao 的部分位于共享对象中: // playao.c // compile with: gcc -shared -o li
我使用以下代码片段来解码音频文件(使用 MP3、WAV、WMV 进行测试)。 但是当它播放音频时,它只是发出静态声音并且不时崩溃。关于我在这里做错了什么的任何提示? #include #includ
嘿嘿, 所以我正在尝试用 libao 播放 pcm 音频。我的目标是让播放可以暂停和取消。 出于某种原因,alsa 驱动程序的输出断断续续,播放速度非常非常快。就像您正在擦洗输出一样。如果我只是播放整
Reading package lists... Done Building dependency tree Reading state information... Done The f
我是一名优秀的程序员,十分优秀!