windows 使用ffmpeg .a静态库读取Wav音频并保存PCM的方法

 更新时间:2024年02月28日 09:12:48   作者:qiufeng_xinqing  
这篇文章主要介绍了windows 使用ffmpeg .a静态库读取Wav音频并保存PCM,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

ffmpeg读取Wav音频并保存PCM(源代码保存成 c/c++ 文件需要与编译命令一致):

// test_ffmpeg.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//#include <iostream>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
//#pragma comment(lib, "libavdevice.a")
//#pragma comment(lib, "libavformat.a")
//#pragma comment(lib, "libavutil.a")
//#pragma comment(lib, "libavcodec.a")
//#pragma comment(lib, "libavfilter.a")
//#pragma comment(lib, "libswresample.a")
//#pragma comment(lib, "libswscale.a")
int main(int arvc, char*argv[]) {
    //av_register_all();
    printf("start...\n");
    AVFormatContext* formatCtx = NULL;
    const AVInputFormat* inputFmt = av_find_input_format(arvc > 1 ? argv[1] : "FLAC");
	int ret = 0;
	printf("format name: %s, long name; %s\n", inputFmt->name, inputFmt->long_name);
    // 打开输入流
    if ((ret = avformat_open_input(&formatCtx, "D:\\project\\ffmpeg-6.1.1\\test_in.wav", inputFmt, NULL)) != 0) {
        printf("Unable to open input stream: %s\n", av_err2str(ret));
        return -1;
    }
    // 查找音频流信息
    if (avformat_find_stream_info(formatCtx, NULL) < 0) {
        printf("Unable to find audio stream information: %s\n", av_err2str(ret));
        return -1;
    }
    // 寻找第一个音频流索引
    int audioStreamIndex = av_find_best_stream(formatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
    if (audioStreamIndex < 0) {
        printf("Unable to find audio stream: %s\n", av_err2str(ret));
        return -1;
    }
    //const AVCodecContext* codecCtx = formatCtx->streams[audioStreamIndex]->codec;
    AVCodecContext* pCodecCtx = avcodec_alloc_context3(NULL);
    if (pCodecCtx == NULL)
    {
        printf("Could not allocate AVCodecContext: %s\n", av_err2str(ret));
        return -1;
    }
    avcodec_parameters_to_context(pCodecCtx, formatCtx->streams[audioStreamIndex]->codecpar);
    // 打开解码器
    const AVCodec* codec = avcodec_find_decoder(pCodecCtx->codec_id);
    if (avcodec_open2(pCodecCtx, codec, NULL) < 0) {
        printf("Unable to open decoder: %s\n", av_err2str(ret));
        return -1;
    }
    // 创建输出文件
    FILE* outputFile = fopen("output.pcm", "wb");
    AVPacket packet;
	printf("start read frame...\n");
    while ((ret = av_read_frame(formatCtx, &packet)) == 0) {
        // 如果数据包是音频数据
        if (packet.stream_index == audioStreamIndex) {
            AVFrame* frame = av_frame_alloc();
            int gotFrame = 0;
			printf("read frame...index %d\n", audioStreamIndex);
            // 解码音频帧
            //avcodec_decode_audio4(pCodecCtx, frame, &gotFrame, &packet);
            if (avcodec_send_packet(pCodecCtx, &packet) < 0
                || (gotFrame = avcodec_receive_frame(pCodecCtx, frame)) < 0) {
                return -1;
            }
			printf("read frame...gotFrame = %d, frame size %d, frame pointer 0x%x\n", (ret), frame->linesize[0], frame->data[0]);
            if (gotFrame == 0) {
                // 处理解码后的音频数据,例如写入文件等
                fwrite(frame->data[0], 1, frame->linesize[0], outputFile);
            }
            av_frame_free(&frame);
        }
        //av_free_packet(&packet);
        av_packet_unref(&packet);
    }
	printf("read frame end. ret = %s\n", av_err2str(ret));
    fclose(outputFile);
    avformat_close_input(&formatCtx);
    return 0;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

编译命令:

gcc -c test.c -o test.o -I./ffmpeg-bin/include 
gcc -Llibavcodec -Llibavdevice -Llibavfilter -Llibavformat -Llibavutil -Llibpostproc -Llibswscale -Llibswresample -Wl,--nxcompat,--dynamicbase -Wl,--high-entropy-va -Wl,--as-needed -Wl,--warn-common -Wl,-rpath-link=:libpostproc:libswresample:libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil  -Wl,--image-base,0x140000000 -o test_g test.o -lavdevice -lavfilter -lavformat -lavcodec -lswresample -lswscale -lavutil  -lpsapi -lole32 -lstrmiids -luuid -loleaut32 -lshlwapi -lgdi32 -lm -latomic -lvfw32 -lm -latomic -lm -latomic -lz -lsecur32 -lws2_32 -liconv -lm -latomic -lmfuuid -lole32 -lstrmiids -lole32 -luser32 -lz -lm -latomic -lm -latomic -lm -luser32 -lbcrypt -latomic  -lole32 -lpsapi -lshell32
strip -o test_1.exe test_g.exe
# 使用upx压缩应用(需要windows终端执行)
upx.exe -9 -o test.exe test_1.exe
rm -rf test_g.exe test_1.exe
./test.exe

到此这篇关于windows 使用ffmpeg .a静态库读取Wav音频并保存PCM的方法的文章就介绍到这了,更多相关windows 使用ffmpeg .a静态库内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • QTimer与QTime实现电子时钟

    QTimer与QTime实现电子时钟

    这篇文章主要为大家详细介绍了QTimer与QTime实现电子时钟,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • C++实现WebSocket服务器的案例分享

    C++实现WebSocket服务器的案例分享

    WebSocket是一种在单个TCP连接上进行全双工通信的通信协议,与HTTP协议不同,它允许服务器主动向客户端发送数据,而不需要客户端明确地请求,本文主要给大家介绍了C++实现WebSocket服务器的案例,需要的朋友可以参考下
    2024-05-05
  • C++ const关键字的实例用法

    C++ const关键字的实例用法

    在本篇文章里小编给大家整理的是一篇关于C++ const关键字的实例用法,需要的朋友们可以学习下。
    2020-02-02
  • C++之关于string对象的大小比较

    C++之关于string对象的大小比较

    这篇文章主要介绍了C++之关于string对象的大小比较方式,具有很好的 参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • C++11中longlong超长整型和nullptr初始化空指针

    C++11中longlong超长整型和nullptr初始化空指针

    本文介绍 C++11 标准中新添加的 long long 超长整型和 nullptr 初始化空指针,在 C++11 标准下,相比 NULL 和 0,使用 nullptr 初始化空指针可以令我们编写的程序更加健壮,本文结合示例代码给大家详细讲解,需要的朋友跟随小编一起看看吧
    2022-12-12
  • C语言摄氏度互相转换华氏

    C语言摄氏度互相转换华氏

    这篇文章主要介绍了C语言摄氏度互相转换华氏,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Linux中rm命令使用以及C/C++代码实现

    Linux中rm命令使用以及C/C++代码实现

    m 是remove 的缩写,Linux中 rm 命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除,这篇文章主要给大家介绍了关于Linux中rm命令使用以及C/C++代码实现的相关资料,需要的朋友可以参考下
    2022-04-04
  • C语言 sprintf 函数详情

    C语言 sprintf 函数详情

    这篇文章主要介绍了C语言 sprintf 函数,文章主要包括sprintf 函数简介、sprintf 函数使用和简单说明了一下sprintf、fprintf、printf 函数区别,需要的朋友可以参考一下文章的具体内容
    2021-10-10
  • C字符串与C++字符串的深入理解

    C字符串与C++字符串的深入理解

    本篇文章是对C字符串与C++字符串进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C语言实现手写Map(数组+链表+红黑树)的示例代码

    C语言实现手写Map(数组+链表+红黑树)的示例代码

    这篇文章主要为大家详细介绍了如何利用C语言实现手写Map(数组+链表+红黑树),文中的示例代码讲解详细,对我们学习有一定借鉴价值,需要的可以参考一下
    2022-09-09

最新评论