Linux下使用ffmpeg播放mp3/aac/wav文件的音乐播放器应用

使用ffmpeg实现一个播放器的想法虽然不新颖,但在嵌入式linux上通过ffmpeg的代码接口实现,并使用alsa接口输出音频,还是有一定挑战和趣味性的。以下是对原文的伪原创处理:

实现一个嵌入式Linux上的音频播放器,支持mp3、aac和wav格式的文件,是一个有趣的项目。基于ffmpeg的强大功能,我们可以实现这个播放器,主要利用ffmpeg的协议处理和音频解码能力。尽管网上有很多相关代码,但由于版本差异,接口存在变化,实际实现时仍需花费不少时间调试。

总结几点关键经验:

多参考官方示例程序,路径为ffmpeg-4.1.9/tmp/share/ffmpeg/examples。avcodec_open2函数调用失败时,需使用avcodec_parameters_to_context函数将找到的音频解码器复制到解码器上下文中。av_read_frame函数可能会导致内存泄漏,应使用av_packet_unref(&input_packet)函数释放内存。要枚举ALSA播放设备,可使用snd_device_name_get_hint函数。avcodec_receive_frame函数接收解码后的帧,只需申请一次内存,AVFrame *pframeSRC = av_frame_alloc()。

使用的是ffmpeg-4.1.9版本,编译选项如下:

//fdk-aacroot@lyz-VirtualBox:/home/lyz/work/broadcast_app/app/thirds_libs_src/fdk-aac/build# vim arm-gcc-cxx11.cmakeroot@lyz-VirtualBox:/home/lyz/work/broadcast_app/app/thirds_libs_src/fdk-aac/build# cmake -DCMAKE_TOOLCHAIN_FILE=/home/lyz/work/broadcast_app/app/thirds_libs_src/fdk-aac/build/arm-gcc-cxx11.cmake ../-- The C compiler identification is GNU 6.4.1root@lyz-VirtualBox:/home/lyz/work/broadcast_app/app_linux# cat /home/lyz/work/broadcast_app/app/thirds_libs_src/fdk-aac/build/arm-gcc-cxx11.cmake# Sample toolchain file for building with gcc compiler# Typical usage:#    *) cmake -H. -B_build -DCMAKE_TOOLCHAIN_FILE="${PWD}/toolchains/gcc.cmake"SET(CMAKE_SYSTEM_NAME Linux)set(CMAKE_SYSTEM_PROCESSOR arm)# set compilerset(CMAKE_C_COMPILER arm-openwrt-linux-gnueabi-gcc)set(CMAKE_CXX_COMPILER arm-openwrt-linux-gnueabi-g++)set(CONFIGURE_OPTS --enable-static=yes --enable-shared=no --disable-shared)# set c++ standardset(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_STANDARD_REQUIRED ON)set(CMAKE_CXX_EXTENSIONS OFF)//mp3/home/lyz/work/broadcast_app/app/thirds_libs_src/lame-3.100./configure --host=arm-openwrt-linux-gnueabi --prefix=${PWD}/build/./configure --target-os=linux --prefix=/home/lyz/work/broadcast_app/app_linux/thirds_libs_src/ffmpeg-4.1.9/tmp --disable-shared --disable-muxers --enable-pic --enable-static --enable-gpl --enable-nonfree --enable-ffmpeg --disable-debug --disable-filters --disable-encoders --disable-hwaccels --enable-static --enable-libmp3lame --enable-demuxers --enable-parsers --enable-protocols --disable-x86asm --disable-stripping --extra-cflags='-I/home/lyz/work/broadcast_app/app_linux/libs/include/ -I/home/lyz/work/broadcast_app/app_linux/libs/include/lame -Os -fpic ' --extra-ldflags='-ldl -lm -L/home/lyz/work/broadcast_app/app_linux/libs/' --enable-decoder=aac --enable-swresample --enable-decoder=ac3

在cpp文件中引用ffmpeg库时,如果出现链接错误,需要在头文件包含处添加两个前缀:

//.cpp#include #ifdef __cplusplusextern "C" {#endif#include "libavutil/time.h"#include "libavformat/avformat.h"#include "libavcodec/avcodec.h"#include "libavdevice/avdevice.h"#include "libswresample/swresample.h"#include "libswscale/swscale.h"#ifdef __cplusplus}#endif

即使修改了头文件包含方式,仍然可能出现链接错误,这与链接库的顺序有关。错误的库链接顺序如下:

LDFLAGS +=  -L ./libs/  -lavcodec -lavfilter -lavformat -lavutil -lpostproc -lswscale -lswresample  -lfdk-aac -lmp3lame

正确的库链接顺序应为:

LDFLAGS += -Wl,-Bstatic -L./libs -lavformat -lavcodec -lswscale -lswresample -lavutil -lavfilter -lavdevice -lpostproc -lfdk-aac -lmp3lame

注意动态链接和静态链接的区别

播记 播记

播客shownotes生成器 | 为播客创作者而生

播记 43 查看详情 播记

LDFLAGS += -Wl,-Bstatic -L./libs -lavformat -lavcodec -lswscale -lswresample -lavutil -lavfilter -lavdevice -lpostproc -lfdk-aac -lmp3lameLDFLAGS += -Wl,-Bdynamic -ldl -lm -lasound -lpthread

在处理内存泄漏问题时,可以使用valgrind工具进行检测:

root@lyz-VirtualBox:/home/lyz/work/broadcast_app/app_linux# valgrind ./bas ./Test1.wav 0

最后,使用ALSA接口完整播放mp3文件声音的代码如下:

//static const char *device = "hw:1,0"; /* playback device "hw:0,0" */static snd_pcm_format_t format = SND_PCM_FORMAT_S16; /* sample format */static unsigned int rate = 44100; /* stream rate */static unsigned int channels = 2; /* count of channels */static unsigned int buffer_time = 500000; /* ring buffer length in us */static unsigned int period_time = 100000; /* period time in us */static int resample = 1; /* enable alsa-lib resampling */static snd_pcm_sframes_t buffer_size;static snd_pcm_sframes_t period_size;snd_pcm_access_t mode = SND_PCM_ACCESS_RW_INTERLEAVED;static snd_output_t *output = NULL;/*配置参数*/static int set_hwparams(snd_pcm_t *handle,snd_pcm_hw_params_t *params,snd_pcm_access_t access){unsigned int rrate;snd_pcm_uframes_t size;int err, dir = 0;/* choose all parameters */err = snd_pcm_hw_params_any(handle, params);if (err < 0) {    printf("Broken configuration for playback: no configurations available: %sn", snd_strerror(err));    return err;}/* set hardware resampling */err = snd_pcm_hw_params_set_rate_resample(handle, params, resample);if (err < 0) {    printf("Resampling setup failed for playback: %sn", snd_strerror(err));    return err;}/* set the interleaved read/write format */err = snd_pcm_hw_params_set_access(handle, params, access);if (err < 0) {    printf("Access type not available for playback: %sn", snd_strerror(err));    return err;}/* set the sample format */err = snd_pcm_hw_params_set_format(handle, params, format);if (err < 0) {    printf("Sample format not available for playback: %sn", snd_strerror(err));    return err;}/* set the count of channels */err = snd_pcm_hw_params_set_channels(handle, params, channels);if (err < 0) {    printf("Channels count (%i) not available for playbacks: %sn", channels, snd_strerror(err));    return err;}/* set the stream rate */rrate = rate;err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);if (err < 0) {    printf("Rate %iHz not available for playback: %sn", rate, snd_strerror(err));    return err;}if (rrate != rate) {    printf("Rate doesn't match (requested %iHz, get %iHz)n", rate, err);    return -EINVAL;}/* set the buffer time */err = snd_pcm_hw_params_set_buffer_time_near(handle, params, &buffer_time, &dir);if (err < 0) {    printf("Unable to set buffer time %i for playback: %sn", buffer_time, snd_strerror(err));    return err;}err = snd_pcm_hw_params_get_buffer_size(params, &buffer_size);if (err < 0) {    printf("Unable to get buffer size for playback: %sn", snd_strerror(err));    return err;}printf("buffer_size = %ldn", buffer_size);/* set the period time */err = snd_pcm_hw_params_set_period_time_near(handle, params, &period_time, &dir);if (err < 0) {    printf("Unable to set period time %i for playback: %sn", period_time, snd_strerror(err));    return err;}err = snd_pcm_hw_params_get_period_size(params, &period_size, &dir);if (err < 0) {    printf("Unable to get period size for playback: %sn", snd_strerror(err));    return err;}printf("period_size = %ldn", period_size);/* write the parameters to the driver */err = snd_pcm_hw_params(handle, params);if (err < 0) {    printf("Unable to set hw params for playback: %sn", snd_strerror(err));    return err;}return 0;}int test_play_mp3(int argc, char *argv[]){int rc;int size;int got_picture;int nb_data;bool pkt_pending = false;int audio_stream_idx;char **hints, **n;char *alsa_device_name;if (argc < 2) {    printf("Usage: %s n", argv[0]);    return 1;}AVFormatContext *infmt_ctx = NULL;AVPacket *input_packet = av_packet_alloc();AVFrame *pframeSRC = av_frame_alloc();AVFrame *pframePCM = av_frame_alloc();int ret;if ((ret = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {    printf("Playback open error: %sn", snd_strerror(ret));    return ret;}if ((err = snd_pcm_hw_params_malloc(&params)) < 0) {    printf("Cannot allocate hardware parameter structure (%s)n", snd_strerror(err));    return err;}if ((err = set_hwparams(handle, params, mode)) < 0) {    printf("Setting of hwparams failed: %sn", snd_strerror(err));    snd_pcm_hw_params_free(params);    snd_pcm_close(handle);    return err;}if ((err = snd_pcm_sw_params_malloc(&swparams)) < 0) {    printf("Cannot allocate software parameters structure (%s)n", snd_strerror(err));    return err;}if ((err = set_swparams(handle, swparams)) < 0) {    printf("Setting of swparams failed: %sn", snd_strerror(err));    snd_pcm_sw_params_free(swparams);    snd_pcm_hw_params_free(params);    snd_pcm_close(handle);    return err;}if ((err = snd_pcm_prepare(handle)) < 0) {    printf("Cannot prepare audio interface for use (%s)n", snd_strerror(err));    return err;}printf("Try to open the device for playback - successrn");snd_pcm_close (pcm);pcm = NULL;alsa_device_name = name;break;}printf("found device:%srn", alsa_device_name);//break;}}n++;}printf("Playback device is %sn", alsa_device_name);/* Install error handler after enumeration, otherwise we'll get many* error messages about invalid card/device ID.*/snd_lib_error_set_handler(alsa_error_handler);err = snd_device_name_free_hint((void**)hints);err = snd_output_stdio_attach(&output, stdout, 0);if (err < 0) {    printf("Attach output failed: %sn", snd_strerror(err));    return err;}if ((ret = avformat_open_input(&infmt_ctx, argv[1], NULL, NULL)) max_analyze_duration = 5*AV_TIME_BASE;//读取一部分视音频流并且获得一些相关的信息ret=avformat_find_stream_info(infmt_ctx, NULL);if (ret < 0) {    fprintf(stderr, "Could not find stream informationn");    avformat_close_input(&infmt_ctx);    return ret;}audio_stream_idx = av_find_best_stream(infmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);if (audio_stream_idx streams[audio_stream_idx]->codecpar;if (pCodecParameters == NULL){    printf("pCodecParameters is NULLn");    avformat_close_input(&infmt_ctx);    return -1;}//找到解码器const AVCodec* decodec = avcodec_find_decoder(pCodecParameters->codec_id);if (!decodec) {    printf("not find decoder codec audio_stream_idx:%d codec_id:%dn", audio_stream_idx, pCodecParameters->codec_id);    avformat_close_input(&infmt_ctx);    return -1;}AVCodecContext *decodec_ctx = avcodec_alloc_context3(decodec);if (!decodec_ctx) {    printf("Can't allocate decoder contextn");    avformat_close_input(&infmt_ctx);    return AVERROR(ENOMEM);}if(avcodec_parameters_to_context(decodec_ctx, pCodecParameters) streams[audio_stream_idx]->time_base;#if 0decodec_ctx->sample_rate = pCodecParameters->sample_rate;decodec_ctx->sample_fmt = (AVSampleFormat)pCodecParameters->format   ;decodec_ctx->channels = pCodecParameters->channels;decodec_ctx->channel_layout = pCodecParameters->channel_layout;#endif//打开解码器ret = avcodec_open2(decodec_ctx, decodec, NULL);if (ret format = AV_SAMPLE_FMT_S16;pframePCM->channel_layout = AV_CH_LAYOUT_STEREO;pframePCM->sample_rate = rate;pframePCM->nb_samples = period_size;pframePCM->channels = channels;av_frame_get_buffer(pframePCM, 0);#elseuint8_t *converted_input_samples = NULL;int converted_input_samples_size = av_samples_alloc(&converted_input_samples, NULL,  channels ,  period_size,  AV_SAMPLE_FMT_S16, 0);#endifstruct SwrContext *pcm_convert_ctx  = swr_alloc();if (!pcm_convert_ctx) {    printf("Could not allocate resampler contextn");    free(buffer);    return -1;}swr_alloc_set_opts(pcm_convert_ctx,                   AV_CH_LAYOUT_STEREO,                   AV_SAMPLE_FMT_S16,                   pframePCM->sample_rate,    av_get_default_channel_layout(decodec_ctx->channels),                   decodec_ctx->sample_fmt,                  decodec_ctx->sample_rate,                   0,                   NULL);ret = swr_init(pcm_convert_ctx);if (ret format = (AVSampleFormat)pCodecParameters->format   ;pframeSRC->channel_layout = decodec_ctx->channel_layout;pframeSRC->sample_rate = decodec_ctx->sample_rate;pframeSRC->nb_samples =  (20*decodec_ctx->sample_rate * channels * 2) / 8000;;pframeSRC->channels = channels;av_frame_get_buffer(pframeSRC, 0);#endifint finished = 0;int decode_ret = 0;int data_size = av_get_bytes_per_sample(decodec_ctx->sample_fmt);printf("data_size:%d,  frame_size:%d, dst_samples:%dn", data_size,  pCodecParameters->frame_size, pframePCM->nb_samples);while (!finished) {ret=av_read_frame(infmt_ctx, input_packet);if (ret != 0) {    if (ret == AVERROR_EOF){       finished = 1;       break;    }            printf("fail to read_framen");            break;        }        //avcodec_send_packet/avcodec_receive_frame        //解码获取初始音频        ret = avcodec_send_packet(decodec_ctx, input_packet);        if (ret == AVERROR(EAGAIN)) {            pkt_pending = true;            continue;        }        if (ret = 0) {            ret = avcodec_receive_frame(decodec_ctx, pframeSRC);            if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {                break;            } else if (ret nb_samples;            int out_samples = source_samples;            // uint8_t *write_2_pcm = NULL;            if (out_samples != pframePCM->nb_samples){                no_resample = 1;                //读取到一帧音频或者视频                //MP3->PCM,ret=swr_convert(pcm_convert_ctx, pframePCM->data, pframePCM->nb_samples,(const uint8_t **)pframeSRC->extended_data, pframeSRC->nb_samples);                if (ret nb_samples:%d,ret:%dn", source_samples, pframeSRC->nb_samples, ret);                    continue;                } else {                    //printf("[2]out_samples:%d, pframeSRC->nb_samples:%d,ret:%dn", source_samples, pframeSRC->nb_samples, ret);                }                write_2_pcm = pframePCM->data[0];                nb_data = ret;            } else {                printf("out_samples:%d, pframeSRC->nb_samples:%d  n", out_samples, pframeSRC->nb_samples );                nb_data = out_samples;                write_2_pcm = pframeSRC->data[0];            }            //向硬件写入音频数据            rc = snd_pcm_writei(handle, write_2_pcm, out_samples);            if (rc == -EPIPE) {                printf("underrun occurredn");                err=snd_pcm_prepare(handle);                if(err < 0) {                    printf("Can't recovery from underrun, prepare failed: %sn", snd_strerror(err));                    break;                }            }            if (rc < 0) {                printf("write to audio interface failed (%s)n", snd_strerror(rc));                break;            }            if (rc != out_samples) {                printf("short write, write %d framesn", rc);            }            av_packet_unref(input_packet);        }    }    if (pcm_convert_ctx) {        swr_free(&pcm_convert_ctx);    }    av_packet_free(&input_packet);    if (pframeSRC) {       av_frame_free(&pframeSRC);    }#if 1    if (pframePCM) {       av_frame_free(&pframePCM);    }#endif    if(decodec_ctx != NULL){        avcodec_close(decodec_ctx);        avcodec_free_context(&decodec_ctx);    }    if (infmt_ctx != NULL) {        avformat_close_input(&infmt_ctx);        avformat_free_context(infmt_ctx);    }    snd_pcm_drain(handle);    snd_pcm_close(handle);    //free(converted_input_samples);    free(buffer);    free(alsa_device_name);    return 0;}

参考:https://www.php.cn/link/94a5313663ab243911f0da89ed1096db

下一步计划是实现对rtsp流的请求。

2022/11/28更新:实现rtsp播放器,只需将播放路径直接设置为rtsp地址,操作非常简单!

Linux下使用ffmpeg播放mp3/aac/wav文件的音乐播放器应用

以上就是Linux下使用ffmpeg播放mp3/aac/wav文件的音乐播放器应用的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/510649.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月9日 00:27:26
下一篇 2025年11月9日 00:37:09

相关推荐

发表回复

登录后才能评论
关注微信