当前位置: 首页 > news >正文

哪些网站用python做服务框架奥林匹克做校服的网站

哪些网站用python做服务框架,奥林匹克做校服的网站,国外html 网站,软件外包公司如何找客源文章目录 概要整体架构流程技术细节 概要 本文介绍使用 FFmpeg,将MP3或WAV文件解码成PCM文件的方法。 整体架构流程 首先,使用的 FFmpeg 库要支持 MP3/WAV 解码功能,即编译的时候要加上(编译 FFmpeg 库可以参考:Win…

文章目录

    • 概要
    • 整体架构流程
    • 技术细节

概要

本文介绍使用 FFmpeg,将MP3或WAV文件解码成PCM文件的方法。

整体架构流程

首先,使用的 FFmpeg 库要支持 MP3/WAV 解码功能,即编译的时候要加上(编译 FFmpeg 库可以参考:Windows编译和使用ffmpeg):

--enable-decoder=mp3float --enable-decoder=pcm_s16le --enable-demuxer=mp3 --enable-demuxer=wav

下面的函数就是利用 FFmpeg 接口,实现将 MP3 或 WAV 文件解码成PCM文件:

#ifdef __cplusplus
extern "C" {
#endif
#include "libavutil/imgutils.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#ifndef __linux__
#include "libswscale/swscale.h"
#endif
#include "libavutil/opt.h"
#ifdef __cplusplus
}
#endif#pragma comment(lib, "libavcodec.a")
#pragma comment(lib, "libavformat.a")
#pragma comment(lib, "libavutil.a")
#pragma comment(lib, "libswresample.a")int DecodedAudioFile(const char *pFileName) {FILE *pFile = fopen("tmp.pcm", "wb");  // 输出文件AVFormatContext *pFormatCtx;AVCodecContext *pCodecCtx;AVCodec *pCodec;AVPacket *packet;AVFrame *pFrame;struct SwrContext *au_convert_ctx = NULL;av_register_all();avformat_network_init();pFormatCtx = avformat_alloc_context();if (avformat_open_input(&pFormatCtx, pFileName, NULL, NULL) != 0) {OutputDebugStringA("Couldn't open input stream.\n");return -1;}if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {OutputDebugStringA("Couldn't find stream information.\n");return -1;}av_dump_format(pFormatCtx, 0, pFileName, false);// Find the first audio streamint audioStream = -1;for (int i = 0; i < pFormatCtx->nb_streams; i++) {if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {audioStream = i;break;}}if (audioStream == -1) {OutputDebugStringA("Didn't find a audio stream.\n");return -1;}// Get a pointer to the codec context for the audio streampCodecCtx = pFormatCtx->streams[audioStream]->codec;// Find the decoder for the audio streampCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (pCodec == NULL) {OutputDebugStringA("Codec not found.\n");return -1;}// Open codecif (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {OutputDebugStringA("Could not open codec.\n");return -1;}int64_t in_channel_layout = av_get_default_channel_layout(pCodecCtx->channels);packet = (AVPacket*)av_malloc(sizeof(AVPacket));av_init_packet(packet);// Out Audio Paramuint64_t out_channel_layout = in_channel_layout;int out_nb_samples = pCodecCtx->frame_size;AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;int out_sample_rate = pCodecCtx->sample_rate;int out_channels = pCodecCtx->channels;// Out Buffer Sizeint out_buffer_size = av_samples_get_buffer_size(NULL, out_channels, out_nb_samples, out_sample_fmt, 1);uint8_t *out_buffer = (uint8_t*)av_malloc(MAX_AUDIO_FRAME_SIZE * 2);pFrame = av_frame_alloc();// 如果输入文件格式不是AV_SAMPLE_FMT_S16才需要if (pCodecCtx->sample_fmt != AV_SAMPLE_FMT_S16) {au_convert_ctx = swr_alloc();au_convert_ctx = swr_alloc_set_opts(au_convert_ctx, 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);}while (av_read_frame(pFormatCtx, packet) >= 0) {if (packet->stream_index == audioStream) {int got_picture;int ret = avcodec_decode_audio4(pCodecCtx, pFrame, &got_picture, packet);if (ret < 0) {OutputDebugStringA("Error in decoding audio frame.\n");return -1;}if (got_picture > 0) {if (au_convert_ctx) {  // MP3文件格式通常是AV_SAMPLE_FMT_FLTP,要重采样、格式转换等swr_convert(au_convert_ctx, &out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t**)pFrame->data, pFrame->nb_samples);// Write PCMfwrite(out_buffer, 1, out_buffer_size, pFile);} else {  // WAV文件格式通常是AV_SAMPLE_FMT_S16,与输出文件一致,直接保存fwrite(pFrame->data[0], 1, pFrame->nb_samples * pCodecCtx->channels * 2, pFile);}}}av_free_packet(packet);}swr_free(&au_convert_ctx);fclose(pFile);av_free(out_buffer);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);return 0;
}int main() {DecodedAudioFile("test.mp3");DecodedAudioFile("test.wav");return 0;
}

技术细节

WAV 文件通常是未压缩的 PCM 音频,解码的步骤与压缩格式(如 MP3)有所不同。在解码 WAV 文件时,解码器可能会直接输出 PCM 数据,而不是像 MP3 那样的压缩数据。对于 WAV 文件,如果格式是 AV_SAMPLE_FMT_S16,则不需要使用 swr_convert,因为音频数据已经是 PCM 格式,可以直接写入文件。例如上述代码中,对文件格式的检查:

// ...
// 如果输入文件格式不是AV_SAMPLE_FMT_S16才需要
if (pCodecCtx->sample_fmt != AV_SAMPLE_FMT_S16) {au_convert_ctx = swr_alloc();au_convert_ctx = swr_alloc_set_opts(au_convert_ctx, 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);
}
// ...
if (au_convert_ctx) {  // MP3文件格式通常是AV_SAMPLE_FMT_FLTP,要重采样、格式转换等swr_convert(au_convert_ctx, &out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t**)pFrame->data, pFrame->nb_samples);// Write PCMfwrite(out_buffer, 1, out_buffer_size, pFile);
} else {  // WAV文件格式通常是AV_SAMPLE_FMT_S16,与输出文件一致,直接保存fwrite(pFrame->data[0], 1, pFrame->nb_samples * pCodecCtx->channels * 2, pFile);
}
// ...
http://www.15wanjia.com/news/163729.html

相关文章:

  • ui设计的网站有哪些WordPress国外打赏
  • 二环建设部网站手机端关键词排名优化
  • 网站建设团队山西省经济建设投资公司网站
  • 关于旅行的网站怎样做网站建设课程的建议
  • 万网怎么建立网站wordpress文章导出
  • 架设个人网站二级域名免费发放
  • 上海网站建设021360甘肃做网站价格
  • 家居公司网站建设方案ppt专做美食的网站
  • 怎么给自己做个网站吗外贸推广服务公司
  • 长春网站制作机构wordpress 大图 主题
  • 中国工程建设招标网官方网站做产品类网站
  • 小公司做网站需要注意什么广州网站建设哪个平台好
  • 官网建站模板库上海网站建设熊掌号
  • 吉林省科瑞建设项目管理有限公司网站网站网址有哪些
  • 专做外贸的网站如何创建网站
  • 大学生商品网站建设移动互联网开发方向包含哪些课程
  • 营销型网站建设目的石家庄做外贸网站
  • 黑色个人网站欣赏wordpress二维码动态图片大小
  • 餐饮品牌设计项目广州网站运营专注乐云seo
  • 宁波网站推广厂家排名asp.net mvc 5网站开发之美 pdf
  • 怎么用织梦做网站后台上海网站高端
  • 中学网站源码ui中国设计官网
  • 新手做网站买服务器免费建网站平台教
  • 商城网站建设价位电商静态网页模板
  • 网站电话素材衡水企业做网站多少钱
  • 网站做迅雷下载链接it程序员需要什么学历
  • 公司办网站大概多少钱优秀专题网站
  • 百度联盟网站备案信息wordpress没有搜索小工具
  • 郑州网站建设专业公司做网站 江门
  • 平面排版网站php cms网站建设