首页 > 其他分享 >ffmpeg + SDL2播放音频示例

ffmpeg + SDL2播放音频示例

时间:2023-07-29 15:22:29浏览次数:33  
标签:return ffmpeg SDL2 示例 ctx version SDL NULL out

在网上搜罗了各种各样的样例代码,都存在各种各样的问题,调了好长时间终于能无杂音播放了
由于个人场景需要本样例加了选择扬声器的功能
不过有的可能还会有问题,目前ogg的文件都能播,mp3有的不行
写一下网上的其他代码可能存在的问题和我的修改
注:代码是C++17,如果编不过需要小改一下

测试平台

  • Ubuntu 16.04, ffmpeg version: 4.3.2, SDL version: 2.0.4
  • Windows 10, ffmpeg version: 5.1.2, SDL version: 2.28.1

注意事项

  • include<SDL.h>要注意加宏SDL_MAIN_HANDLED,因为里面有个#define main,比较坑
  • 对部分格式(比如mp3)需要调用 avformat_find_stream_info,不然stream_id、采样率这些东西都会获取不到
  • 由于音频的采样率和输出设备的采样率之类的参数可能不一致,所以需要SwrContext(有些音频采样率是48100),否则会导致播放有杂音
  • 要关注swr_convert的返回值,对于部分音频,单次解码出来的buffer可能没填满,需要像代码中那样计算一下,否则会导致音频播放卡顿
  • 网上部分使用回调的代码,由于上一条,不能每次swr_convert结束后都调一下SDL_Delay,否则可能会出现解码跟不上播放速度导致音频播放卡顿

关于多个扬声器

  • 不需要的话,SDL_OpenAudioDevice第一个参数传空指针就行,像SDL_PauseAudioDevice这种函数可以改成不带Device的
  • 可以通过device id同时控制多个扬声器(不过代码里没有多个,有需要加一下即可)
  • 要停止播放可以调用SDL_ClearQueuedAudio清除播放的缓冲区。暂停的话可以调用SDL_PauseAudioDevice,第二个参数填1。

其他

  • 部分mp3无法播放,部分mp3从memory中打开无法播放,原因不明
  • 似乎无法控制声道,改AV_CH_LAYOUT_STEREO没用

代码

#include <cstdio>
#include <string>
#include <fstream>

#define SDL_MAIN_HANDLED
#include "SDL.h"

extern "C" {
#include "libswresample/swresample.h"
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}

AVFormatContext *OpenAudioFromFile(const std::string &file_path) {
    AVFormatContext *format_ctx = avformat_alloc_context();
    if (int ret = avformat_open_input(&format_ctx, file_path.c_str(), NULL, NULL);
            ret < 0) {
        printf("avformat_open_input failed, ret: %d, path: %s\n", ret, file_path.c_str());
    }
    return format_ctx;
}

AVFormatContext *OpenAudioFromData(const std::string &data) {
    AVIOContext *avio_ctx = avio_alloc_context(
                (uint8_t *)data.data(), data.size(), 0, NULL, NULL, NULL, NULL);
    if (!avio_ctx) {
        printf("avio_alloc_context failed\n");
        return nullptr;
    }

    AVFormatContext *format_ctx = avformat_alloc_context();
    if (!format_ctx) {
        printf("avformat_alloc_context faield\n");
        return nullptr;
    }
    format_ctx->pb = avio_ctx;

    if (int ret = avformat_open_input(&format_ctx, NULL, NULL, NULL); ret < 0) {
        printf("avformat_open_input failed, ret: %d\n", ret);
        return nullptr;
    }
    return format_ctx;
}

std::string readFile(const std::string &file_path) {
    std::ifstream file(file_path); // 打开文件
    return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); // 读取文件内容到string
}

int main(int argc, char *argv[]) {
    printf("ffmpeg version: %s\n", av_version_info());

    SDL_version version;
    SDL_GetVersion(&version);
    printf("SDL version: %d.%d.%d\n", version.major, version.minor, version.patch);

    char *file = "D:/qytx.mp3";
    AVFormatContext *pFormatCtx = NULL; //for opening multi-media file

    int audioStream = -1;

    AVCodecParameters *pCodecParameters = NULL; //codec context
    AVCodecContext *pCodecCtx = NULL;

    const AVCodec *pCodec = NULL; // the codecer
    AVFrame *pFrame = NULL;
    AVPacket *packet;
    uint8_t *out_buffer;

    int64_t in_channel_layout;
    struct SwrContext *au_convert_ctx;

    pFormatCtx = OpenAudioFromFile(file);
    //    std::string data = readFile(file);
    //    pFormatCtx = OpenAudioFromData(data);
    if (!pFormatCtx) {
        return -1;
    }

    if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) {
        // 处理获取流信息失败的情况
        return -1;
    }

    audioStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
    printf("av_find_best_stream %d\n", audioStream);

    if (audioStream == -1) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Din't find a video stream!");
        return -1;// Didn't find a video stream
    }

    // Get a pointer to the codec context for the video stream
    pCodecParameters = pFormatCtx->streams[audioStream]->codecpar;

    // Find the decoder for the video stream
    pCodec = avcodec_find_decoder(pCodecParameters->codec_id);
    if (pCodec == NULL) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unsupported codec!\n");
        return -1; // Codec not found
    }

    // Copy context
    pCodecCtx = avcodec_alloc_context3(pCodec);
    if (avcodec_parameters_to_context(pCodecCtx, pCodecParameters) != 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't copy codec context");
        return -1;// Error copying codec context
    }

    // Open codec
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to open decoder!\n");
        return -1; // Could not open codec
    }
    packet = (AVPacket *) av_malloc(sizeof(AVPacket));
    av_init_packet(packet);
    pFrame = av_frame_alloc();

    uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO;//输出声道
    int out_nb_samples = 1024;
    enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;//输出格式S16
    int out_sample_rate = pCodecCtx->sample_rate;
    int out_channels = av_get_channel_layout_nb_channels(out_channel_layout);

    int out_buffer_size = av_samples_get_buffer_size(NULL, out_channels, out_nb_samples, out_sample_fmt, 1);
    out_buffer = (uint8_t *) av_malloc(out_buffer_size * 2); // buffer不够大所以乘2,原因未知

    //Init
    if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
        printf("Could not initialize SDL - %s\n", SDL_GetError());
        return -1;
    }

    // 获取可用扬声器列表,不需要可以忽略
    int deviceCount = SDL_GetNumAudioDevices(0);
    printf("SDL_GetNumAudioDevices %d\n", deviceCount);
    for (int i = 0; i < deviceCount; i++) {
        printf("Audio Device %d: %s\n", i, SDL_GetAudioDeviceName(i, 0));
    }

    SDL_AudioSpec spec;
    spec.freq = out_sample_rate;
    spec.format = AUDIO_S16SYS;
    spec.channels = out_channels;
    spec.silence = 0;
    spec.samples = out_nb_samples;
    spec.callback = nullptr;

    // 指定扬声器,不需要第一个参数可以填nullptr
    SDL_AudioDeviceID device_id = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(1, 0), false, &spec, nullptr, false);
    printf("device_id %d\n", device_id);
    if (device_id == 0) {
        printf("can't open audio.\n");
        return -1;
    }

    in_channel_layout = av_get_default_channel_layout(pCodecCtx->channels);
    au_convert_ctx = swr_alloc_set_opts(nullptr, out_channel_layout, out_sample_fmt, out_sample_rate,
                                        in_channel_layout, pCodecCtx->sample_fmt, pCodecCtx->sample_rate, 0, NULL);
    swr_init(au_convert_ctx);

    SDL_PauseAudioDevice(device_id, 0);

    fflush(stdout);

    while (av_read_frame(pFormatCtx, packet) >= 0) {
        if (packet->stream_index == audioStream) {
            avcodec_send_packet(pCodecCtx, packet);
            while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) {
                int ret = swr_convert(au_convert_ctx, &out_buffer, out_buffer_size, (const uint8_t **) pFrame->data,
                                      pFrame->nb_samples); // 转换音频
                if (ret < 0) {
                    printf("swr_convert failed %d\n", ret);
                }
                int out_samples = ret;
                SDL_QueueAudio(device_id, out_buffer, out_samples * spec.channels * av_get_bytes_per_sample(out_sample_fmt));
            }
        }
        av_packet_unref(packet);
    }
    SDL_Delay(200000); //延迟播放
    swr_free(&au_convert_ctx);
    SDL_CloseAudioDevice(device_id);
    SDL_Quit();
}

标签:return,ffmpeg,SDL2,示例,ctx,version,SDL,NULL,out
From: https://www.cnblogs.com/oyking/p/17589871.html

相关文章

  • @Around简单使用示例——SpringAOP增强处理
    @Around简单使用示例——SpringAOP增强处理@Around的作用既可以在目标方法之前织入增强动作,也可以在执行目标方法之后织入增强动作;可以决定目标方法在什么时候执行,如何执行,甚至可以完全阻止目标目标方法的执行;可以改变执行目标方法的参数值,也可以改变执行目标方法之后的返回......
  • ffmpeg合并音频和视频
    ffmpeg合并音频和视频命令行ffmpeg-ivideo.m4s-iaudio.m4s-acodeccopy-vcodeccopyout.mp4使用ffmpeg的apiextern"C"{#include"libavformat/avformat.h"#include"libavutil/dict.h"#include"libavutil/opt.h"#include&quo......
  • 设计模式-备忘录模式在Java中使用示例-象棋悔棋
    场景备忘录模式备忘录模式提供了一种状态恢复的实现机制,使得用户可以方便地回到一个特定的历史步骤,当新的状态无效或者存在问题时,可以使用暂时存储起来的备忘录将状态复原,当前很多软件都提供了撤销(Undo)操作,其中就使用了备忘录模式。备忘录模式结构图 在备忘录模式结构......
  • ffmpeg 编译安装android和linux
    ffmpeg编译安装android和linux下载:https://github.com/FFmpeg/FFmpeghttps://www.ffmpeg.org/download.htmlenvirenmentndk:https://github.com/android/ndk/wiki/Unsupported-Downloadssudoapt-getinstallbuild-essentialpkg-configsudoapt-getintalllibx264-dev......
  • 设计模式-中介者模式在Java中使用示例-客户信息管理
    场景欲开发客户信息管理窗口界面,界面组件之间存在较为复杂的交互关系:如果删除一个客户,要在客户列表(List)中删掉对应的项,客户选择组合框(ComboBox)中客户名称也将减少一个;如果增加一个客户信息,客户列表中需增加一个客户,且组合框中也将增加一项。中介者模式概述如果在一个系统......
  • Web Component 简单示例
    前言学习内容来源:https://www.youtube.com/watch?v=2I7uX8m0Ta0https://developer.mozilla.org/zh-CN/docs/Web/API/Web_components基本概念Customelement(自定义元素):class或者function,定义组件apiShadowDOM(影子DOM):用于将封装的“影子”DOM树附加到元素(与主文档DOM......
  • java接口文档示例
    Java接口文档示例及其用途引言在Java开发中,接口文档是非常重要的一部分。它提供了对代码库的详细描述,包括类、方法、参数和返回值等信息。接口文档不仅可以帮助开发人员了解代码库的功能和用途,还可以作为代码库的使用指南,方便其他开发人员快速上手。本文将介绍Java接口文档的示例......
  • 自定义过滤器写法示例
    点击查看代码@Component@Slf4j@RequiredArgsConstructorpublicclassCustomFilterextendsOncePerRequestFilter{privatefinalObjectMapperobjectMapper;/***指定要放行的接口路径*/privatestaticfinalString[]ALLOWED_PATHS={......
  • Go语言网络编程示例
    1.简单示例以下是一个使用Go语言标准库net实现的简单的客户端和服务器端示例。服务器端监听本地的8080端口,并在接收到客户端连接后,向客户端发送一条欢迎消息。客户端通过Dial方法连接服务器,并接收服务器发送的欢迎消息。服务器端代码:packagemainimport("......
  • 关于context的用法示例
    1.示例代码ser=self.get_serializer(context={'request':request},data=request.data)以上代码使用了context的方法将request传入到序列化类中 2.另一种写法ser=self.get_serializer(data=request.data)ser.aaa=request 这样也可以向序列化类传入request,如果序列化类......