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

网站建设开发中br标签的作用品牌营销策略分析

网站建设开发中br标签的作用,品牌营销策略分析,公司网站设计网络公司,wordpress主题仿北京时间设置FFmpeg入门:最简单的音频播放器 欢迎大家来到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/99619.html

相关文章:

  • 济南网站开发设计东莞网站快速排名提升
  • 公司网站备案具体什么情况百度竞价怎么收费
  • 哪做网站比较便宜识图搜索在线 照片识别
  • 盘锦做网站企业什么是seo优化?
  • 网站建设祥云平台宁波网络推广优化方案
  • 会员管理系统怎么用优化关键词技巧
  • 舟山网站建设开发百度搜索入口网址
  • wordpress无法重定向网站网页的优化方法
  • 腾讯云点播做视频网站厦门网站搜索引擎优化
  • 信誉好的免费网站建设百度seo搜索排名
  • 域名备案时网站名称今日短新闻20条
  • 中山蚂蚁网站开发百度收录接口
  • 美术馆网站建设方案书谷歌广告推广怎么做
  • 东昌府聊城网站建设公司网站建设代理
  • 静态网站没有数据库吗厦门网
  • 免费做电子书的网站流量精灵官网
  • 中资源的 域名管理网站中国最权威的网站排名
  • 建网站如何赚钱通州优化公司
  • 网站上传文件大小限制网站seo设计方案案例
  • 什么网站建站公司可靠seo课程心得体会
  • 百度做商务网站多少钱个人网站模板
  • 怎样做网站公司的销售中山谷歌推广
  • 上海市杨浦区建设小学网站引擎搜索网站
  • 内蒙古建设网站夸克搜索
  • 网站制作报价是否合法培训机构还能开吗
  • 用axure做的网站成品产品软文范例100字
  • 简约网站内容布局免费网页设计制作网站
  • 中国建筑网官网一级建造师管理天津seo博客
  • 网站建设 深圳百度搜索推广创意方案
  • 张店网站建设方案seo 是什么