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

百度竞价网站企业软文营销发布平台

百度竞价网站,企业软文营销发布平台,网站后台登入不了出现验证码错误,石家庄网站建设seoFFmpeg入门:最简单的音频播放器 欢迎大家来到FFmpeg入门的第二章,今天只做一个最简单的FFmpeg音频播放器;同样,话不多说,先上流程图 流程图 以上流程和视频播放器的解码过程基本上是一致的; 不同点在于 S…

FFmpeg入门:最简单的音频播放器

欢迎大家来到FFmpeg入门的第二章,今天只做一个最简单的FFmpeg音频播放器;同样,话不多说,先上流程图

流程图

在这里插入图片描述
以上流程和视频播放器的解码过程基本上是一致的;
不同点在于 SDL的渲染方式。下面我会重点说一下这个部分

SDL音频渲染

音频渲染的方式和视频不太一样的,我们对于音频的播放速度其实是根据采样率定义的(音频的采样率==视频的帧率),在初始化的时候SDL播放器就指定了这个参数,因此不需要向视频播放器那样手动去延迟来保持帧率。

如下是SDL音频播放器的初始化。

SDL_AudioSpec wanted_spec;
wanted_spec.freq = out_sample_rate;					// 采样率
wanted_spec.format = AUDIO_S16SYS;					// 采样格式 16bit
wanted_spec.channels = out_channels;				// 通道数
wanted_spec.silence = 0;
wanted_spec.samples = out_nb_samples;				// 单帧处理的采样点
wanted_spec.callback = fill_audio;					// 回调函数
wanted_spec.userdata = pCodecCtx;					// 回调函数的参数

其原理就是SDL音频播放器会不断从其缓冲区取出数据读取播放,因此我们只需要不断向其缓冲区中写入数据即可。(详见代码)

// 设置读取的音频数据
audio_info.audio_len = out_buffer_size;
audio_info.audio_pos = (Uint8 *) out_buffer;

但是有个点注意一下,就是在写入SDL播放器的缓冲区之前,我们要确保之前的数据已经被SDL播放器消化完了,不然会导致音频数据被覆盖,而没有读出来;(详见代码)

// 等待SDL播放完成
while(audio_info.audio_len > 0)SDL_Delay(0.5);

源代码

接下来看看源代码吧
tutorial03.h

//
//  tutorial03.h
//  learning
//
//  Created by chenhuaiyi on 2025/2/16.
//#ifndef tutorial03_h
#define tutorial03_h/**头文件*/
#include <stdio.h>
// ffmpeg
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/time.h>
// SDL
#include <SDL.h>
#include <SDL_thread.h>/**宏定义*/
#define USE_SDL 1/**数据类型定义*/
typedef struct {Uint32  audio_len;Uint8  *audio_pos;
} AudioInfo;/**全局变量*/
extern AudioInfo audio_info;#endif /* tutorial03_h */

tutorial03.c

/**
//  tutorial03.c
//  learning
//
//  Created by chenhuaiyi on 2025/2/16.*/#include "tutorial03.h"AudioInfo audio_info;/* udata: 传入的参数* stream: SDL音频缓冲区* len: SDL音频缓冲区大小* 回调函数*/
void fill_audio(void *udata, Uint8 *stream, int len){SDL_memset(stream, 0, len);			// 必须重置,不然全是电音!!!if(audio_info.audio_len==0){					// 有音频数据时才调用return;}len = (len>audio_info.audio_len ? audio_info.audio_len : len);	// 最多填充缓冲区大小的数据SDL_MixAudio(stream, audio_info.audio_pos, len, SDL_MIX_MAXVOLUME);audio_info.audio_pos += len;audio_info.audio_len -= len;
}int main(int argc, char* argv[])
{AVFormatContext* pFormatCtx = avformat_alloc_context();int				 i, audioStream;AVCodecContext*  pCodecCtx = avcodec_alloc_context3(NULL);const AVCodec*	 pCodec;AVPacket		 packet;if(argc < 2) {fprintf(stderr, "Usage: test <file>\n");exit(1);}avformat_network_init();// 1. 打开视频文件,获取格式上下文if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0){printf("Couldn't open input stream.\n");return -1;}// 2. 对文件探测流信息if(avformat_find_stream_info(pFormatCtx, NULL) < 0){printf("Couldn't find stream information.\n");return -1;}// 打印信息av_dump_format(pFormatCtx, 0, argv[1], 0);// 3. 找到对应的音频流audioStream=-1;for(i=0; i < pFormatCtx->nb_streams; i++) {if(pFormatCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_AUDIO){audioStream=i;break;}}if(audioStream==-1){printf("Didn't find a audio stream.\n");return -1;}// 4. 将音频流编码参数写入上下文AVCodecParameters* pCodecParam = pFormatCtx->streams[audioStream]->codecpar;avcodec_parameters_to_context(pCodecCtx, pCodecParam);avcodec_parameters_free(&pCodecParam);// 5. 查找流的编码器pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if(pCodec==NULL){printf("Codec not found.\n");return -1;}// 6. 打开流的编解码器if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){printf("Could not open codec.\n");return -1;}// 输出用到的信息AVChannelLayout out_channel_layout = AV_CHANNEL_LAYOUT_STEREO;	// 通道 layoutint out_nb_samples = pCodecCtx->frame_size;	// 编解码器每个帧需要处理或者输出的采样点的大小 AAC:1024  MP3:1152enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;			// 采样格式int out_sample_rate = 44100;									// 采样率int out_channels = out_channel_layout.nb_channels;				// 通道数// 获取需要使用的缓冲区大小 -> 通道数,单通道样本数,位深 1024(单帧处理的采样点)*2(双通道)*2(16bit对应2字节)int out_buffer_size = av_samples_get_buffer_size(NULL, out_channels,out_nb_samples, out_sample_fmt, 1);// 分配缓冲区空间uint8_t* out_buffer = NULL;av_samples_alloc(&out_buffer, NULL, out_channels, out_nb_samples, out_sample_fmt, 1);AVFrame* pFrame = av_frame_alloc();//	SDL 初始化
#if USE_SDLif(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {printf( "Could not initialize SDL - %s\n", SDL_GetError());return -1;}SDL_AudioSpec wanted_spec;wanted_spec.freq = out_sample_rate;					// 采样率wanted_spec.format = AUDIO_S16SYS;					// 采样格式 16bitwanted_spec.channels = out_channels;				// 通道数wanted_spec.silence = 0;wanted_spec.samples = out_nb_samples;				// 单帧处理的采样点wanted_spec.callback = fill_audio;					// 回调函数wanted_spec.userdata = pCodecCtx;					// 回调函数的参数// 打开音频播放器if (SDL_OpenAudio(&wanted_spec, NULL)<0){printf("can't open audio.\n");return -1;}#endifint ret = 0;int index = 0;// 上下文格式转换SwrContext *swr_ctx = NULL;swr_alloc_set_opts2(&swr_ctx,&out_channel_layout,			// 输出layoutout_sample_fmt,					// 输出格式out_sample_rate,				// 输出采样率&pCodecCtx->ch_layout,			// 输入layoutpCodecCtx->sample_fmt,			// 输入格式pCodecCtx->sample_rate,			// 输入采样率0, NULL);swr_init(swr_ctx);// 开始播放SDL_PauseAudio(0);AVRational time_base = pFormatCtx->streams[audioStream]->time_base;int64_t av_start_time = av_gettime();								// 播放开始时间戳// 循环1: 从文件中读取packetwhile(av_read_frame(pFormatCtx, &packet)>=0){if(packet.stream_index==audioStream){// 将packet写入编解码器ret = avcodec_send_packet(pCodecCtx, &packet);if ( ret < 0 ) {printf("send packet error\n");return -1;}while (!avcodec_receive_frame(pCodecCtx, pFrame)) {// 格式转化swr_convert(swr_ctx, &out_buffer, out_buffer_size,(const uint8_t **)pFrame->data, pFrame->nb_samples);index++;printf("第%d帧 | pts:%lld | 帧大小(采样点):%d | 实际播放点%.2fs | 预期播放点%.2fs\n",index,packet.pts,packet.size,(double)(av_gettime() - av_start_time)/AV_TIME_BASE,pFrame->pts * av_q2d(time_base));#if USE_SDL// 设置读取的音频数据audio_info.audio_len = out_buffer_size;audio_info.audio_pos = (Uint8 *) out_buffer;// 等待SDL播放完成while(audio_info.audio_len > 0)SDL_Delay(0.5);
#endif}av_packet_unref(&packet);}}// 打印参数printf("格式: %s\n", pFormatCtx->iformat->name);printf("时长: %lld us\n", pFormatCtx->duration);printf("音频持续时长为 %.2f,音频帧总数为 %d\n", (double)(av_gettime()-av_start_time)/AV_TIME_BASE, index);printf("码率: %lld\n", pFormatCtx->bit_rate);printf("编码器: %s (%s)\n", pCodecCtx->codec->long_name, avcodec_get_name(pCodecCtx->codec_id));printf("通道数: %d\n", pCodecCtx->ch_layout.nb_channels);printf("采样率: %d \n", pCodecCtx->sample_rate);printf("单通道每帧的采样点数目: %d\n", pCodecCtx->frame_size);printf("pts单位(ms*1000): %.2f\n", av_q2d(pFormatCtx->streams[audioStream]->time_base) * AV_TIME_BASE);// 释放空间swr_free(&swr_ctx);
#if USE_SDLSDL_CloseAudio();SDL_Quit();
#endifav_free(out_buffer);av_free(pFrame);avcodec_free_context(&pCodecCtx);avformat_close_input(&pFormatCtx);return 0;
}
http://www.dtcms.com/wzjs/362157.html

相关文章:

  • 怎么百度上搜到自己的网站济南优化网站关键词
  • 怎样用织梦做淘宝客网站免费建站免费网站
  • 综合性型门户网站有哪些百度电脑版下载官网
  • 淄博网站优化网络服务主要包括
  • 东莞齐诺做网站做网页设计的软件
  • 公司企业网站设计尺寸恶意点击推广神器
  • 温州网站建设设计公司百分百营销软件官网
  • 投资建设集团网站西安自动seo
  • 网站建设论文摘要青岛seo搜索优化
  • 湖南省人民政府官方网合肥seo推广培训班
  • 做一个网站西安网站seo推广
  • 新疆和田住房和城乡建设网站百度推广400电话
  • 安徽网页设计培训seog
  • 武汉建站模板汕头网站建设优化
  • 个人域名网站可以做企业站吗百度一下你就知道移动首页
  • 网站建设及优化重要性百度关键词搜索量统计
  • 蓝色色调网站企业网络规划与设计
  • 建站宝盒免费下载百度关键词多少钱一个月
  • 注册网络公司起什么名字深圳关键词优化报价
  • 网站公告左右滚动公众号开发网站公司
  • 加强门户网站建设的通知百度推广助手电脑版
  • 手机能做网站吗网站制作需要多少钱
  • 利用access做网站男生技能培训班有哪些
  • 百度做自己的网站汽车营销策划方案ppt
  • 一个公司网站的价格网站如何做优化排名
  • 做网站网页多少钱北京seo排名外包
  • 网站建设捌金手指下拉一太原seo哪家好
  • 最强的网站建设电话百度账号中心
  • 货物公司网站建设方案海南百度推广公司电话
  • app与网站数据交互百度置顶广告多少钱