audio
// media.cpp : 定义控制台应用程序的入口点。
// https://wenku.baidu.com/view/e910c474c5da50e2524d7fb4.html https://blog.csdn.net/leixiaohua1020/article/details/10528443 最主要这个\examples\demuxing_decoding.c #include "stdafx.h" /*
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
*/
#include "stdafx.h"
extern "C"
{
#include <stdio.h> #include "libavformat/avformat.h"
#include "libavutil/dict.h" #include <libavcodec/avcodec.h>
#include <libavformat/avformat.h> #include <libswscale/swscale.h>
}
#pragma comment(lib,"lib/avcodec.lib")
#pragma comment(lib,"lib/avformat.lib")
#pragma comment(lib,"lib/avutil")
//#pragma comment(lib,"lib/SDLmain.lib")
#pragma comment(lib,"lib/avdevice.lib")
#pragma comment(lib,"lib/avfilter.lib")
#pragma comment(lib,"lib/postproc.lib") #pragma comment(lib,"lib/swresample.lib")
#pragma comment(lib,"lib/swscale.lib") #pragma comment(lib, "sdl2.lib")
#include <iostream> #include <SDL2/SDL.h>
#include <SDL2/SDL_thread.h> #ifdef __MINGW32__
#undef main
#endif #include <stdio.h> int
randomInt(int min, int max)
{
return min + rand() % (max - min + );
}
#define MAX_AUDIO_FRAME_SIZE 192000
SDL_AudioSpec wanted_spec, spec; int audio_decode_frame(AVCodecContext *aCodecCtx, uint8_t *audio_buf, int buf_size) {
static AVPacket pkt;
static uint8_t *audio_pkt_data = NULL;
static int audio_pkt_size = ;
static AVFrame frame; int len1, data_size = ; for (;;) {
while(audio_pkt_size > ) {
int got_frame = ;
len1 = avcodec_decode_audio4(aCodecCtx, &frame, &got_frame, &pkt);
if (len1 < ) {
// if error, skip frame.
audio_pkt_size = ;
break;
}
audio_pkt_data += len1;
audio_pkt_size -= len1;
if (got_frame) {
data_size = av_samples_get_buffer_size(NULL, aCodecCtx->channels, frame.nb_samples, aCodecCtx->sample_fmt, );
memcpy(audio_buf, frame.data[], data_size);
}
if (data_size <= ) {
// No data yet, get more frames.
continue;
}
// We have data, return it and come back for more later.
return data_size;
}
if (pkt.data) {
av_packet_unref(&pkt);
} //if (packet_queue_get(&audioq, &pkt, 1) < 0)
{
return -;
}
audio_pkt_data = pkt.data;
audio_pkt_size = pkt.size;
}
} void audio_callback(void *userdata, Uint8 *stream, int len) {
AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;
int len1, audio_size; static uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * ) / ];
static unsigned int audio_buf_size = ;
static unsigned int audio_buf_index = ; while (len > ) {
if (audio_buf_index >= audio_buf_size) {
// We have already sent all our data; get more.
audio_size = audio_decode_frame(aCodecCtx, audio_buf, audio_buf_size);
if (audio_size < ) {
// If error, output silence.
audio_buf_size = ; // arbitrary?
memset(audio_buf, , audio_buf_size);
} else {
audio_buf_size = audio_size;
}
audio_buf_index = ;
}
len1 = audio_buf_size - audio_buf_index;
if (len1 > len) {
len1 = len;
}
memcpy(stream, (uint8_t *) audio_buf + audio_buf_index, len1);
len -= len1;
stream += len1;
audio_buf_index += len1;
}
} int _tmain(int argc, char *argv[]) {
AVFormatContext *pFormatCtx = NULL;
int i, videoStream,audioStream;
AVCodecContext *pCodecCtx = NULL,*pCodecAudioCtx = NULL;
AVCodec *pCodec = NULL,*pCodecAudio = NULL;
AVFrame *pFrame = NULL;
AVPacket packet;
int frameFinished;
//float aspect_ratio; AVDictionary *optionsDict = NULL ,*audioOptionsDict=NULL;
struct SwsContext *sws_ctx = NULL;
//SDL_CreateTexture();
SDL_Texture *bmp = NULL;
SDL_Window *screen = NULL;
SDL_Rect rect;
SDL_Event event;
/*
if(argc < 2) {
fprintf(stderr, "Usage: test <file>\n");
exit(1);
}*/
// Register all formats and codecs
av_register_all(); if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
exit();
} // Open video file
if(avformat_open_input(&pFormatCtx,"D://shitu.mkv"/* argv[1]"D://3s.mp4"*/, NULL, NULL)!=)
return -; // Couldn't open file if(avformat_find_stream_info(pFormatCtx, NULL)<)
return -; // Couldn't find stream information // Dump information about file onto standard error
av_dump_format(pFormatCtx, , argv[], ); // Find the first video stream
videoStream=-;
for(i=; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
videoStream=i;
break;
}
else if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
audioStream=i;
break;
}
if(videoStream==-||audioStream==-)
return -; // Didn't find a video stream // Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
pCodecAudioCtx=pFormatCtx->streams[audioStream]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
fprintf(stderr, "Unsupported codec!\n");
return -; // Codec not found
} pCodecAudio=avcodec_find_decoder(pCodecAudioCtx->codec_id);
if(pCodecAudio==NULL) {
fprintf(stderr, "Unsupported audio codec!\n");
return -; // Codec not found
} // Open codec
if(avcodec_open2(pCodecCtx, pCodec, &optionsDict)<)
return -; // Could not open codec //--
if (SDL_OpenAudio(&wanted_spec, &spec) < ) {
fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
return -; } avcodec_open2(pCodecAudioCtx, pCodecAudio, &audioOptionsDict);
//-- pFrame=av_frame_alloc();
AVFrame* pFrameYUV =av_frame_alloc(); if( pFrameYUV == NULL )
return -;
//--
AVFrame* pAudioFrame =av_frame_alloc();
//--
screen = SDL_CreateWindow("My Game Window",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
pCodecCtx->width, pCodecCtx->height,
SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL);
SDL_Renderer *renderer = SDL_CreateRenderer(screen, -, ); if(!screen) {
fprintf(stderr, "SDL: could not set video mode - exiting\n");
exit();
} bmp = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_YV12,SDL_TEXTUREACCESS_STREAMING,pCodecCtx->width,pCodecCtx->height); sws_ctx = sws_getContext(pCodecCtx->width,pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width,pCodecCtx->height,
AV_PIX_FMT_YUV420P,SWS_BILINEAR,NULL,NULL, NULL); int numBytes = avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width,
pCodecCtx->height);
uint8_t* buffer = (uint8_t *)av_malloc(numBytes*sizeof(uint8_t)); avpicture_fill((AVPicture *)pFrameYUV, buffer, AV_PIX_FMT_YUV420P,
pCodecCtx->width, pCodecCtx->height); i=; rect.x = ;
rect.y = ;
rect.w = pCodecCtx->width;
rect.h = pCodecCtx->height; while(av_read_frame(pFormatCtx, &packet)>=) {
// Is this a packet from the video stream?
if(packet.stream_index==videoStream) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished,
&packet); // Did we get a video frame?
if(frameFinished) {
sws_scale(sws_ctx,(uint8_t const * const *)pFrame->data, pFrame->linesize, ,
pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize);
////iPitch 计算yuv一行数据占的字节数
SDL_UpdateTexture( bmp, &rect, pFrameYUV->data[], pFrameYUV->linesize[] );
SDL_RenderClear( renderer );
SDL_RenderCopy( renderer, bmp, &rect, &rect );
SDL_RenderPresent( renderer );
}
SDL_Delay();
//Sleep(500);
}else if (packet.stream_index == audioStream) {
} // Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
SDL_PollEvent(&event);
switch(event.type) {
case SDL_QUIT:
SDL_Quit();
exit();
break;
default:
break;
} } SDL_DestroyTexture(bmp); // Free the YUV frame
av_free(pFrame);
av_free(pFrameYUV);
// Close the codec
avcodec_close(pCodecCtx); // Close the video file
avformat_close_input(&pFormatCtx); return ;
}
https://www.cnblogs.com/lgh1992314/p/5834639.html
这个_-to thi tha
回看tha视频,不要杂念,杂容易慢,一段只一件
audio的更多相关文章
- html5 audio总结
前言 html5中对音频,视频播放原生支持.最近做了一个音乐播放器,得益于快过年了,才能抽出一点时间来总结一下.总的来说,html5对audio的支持非常强大, 难怪flash要死.浏览器上装播放插件 ...
- 《HTML5》 Audio/Video全解
一.标签解读 <audio> 标签属性 <audio id="media" src="http://www.abc.com/test.mp3" ...
- video/audio在ios/android上播放兼容
1.audio自动播放 <audio src='xxx.mp3' autoplay></audio> 上面是audio标签autoplay属性是自动播放,但是在安卓部分浏览器和 ...
- audio 基本功能实现(audio停止播放,audio如何静音,audio音量控制等)
audio最简单原始的播放.暂停.停止.静音.音量大小控制的功能,注意某些浏览器会有权限无法自动播放噢(video也会如此) <!doctype html> <html> &l ...
- h5自定义audio(问题及解决)
h5活动需要插入音频,但又需要自定义样式,于是自己写咯 html <!-- cur表示当前时间 max表示总时长 input表示进度条 --> <span class='cur'&g ...
- Your app declares support for audio in the UIBackgroundModes key in your Info.plist 错误
提交AppStore时候被拒绝 拒绝原因:Your app declares support for audio in the UIBackgroundModes key in your Info.p ...
- HTML5的Audio标签打造WEB音频播放器
目前,WEB页面上没有标准的方式来播放音频文件,大多数的音频文件是使用插件来播放,而众多浏览器都使用了不同的插件.而HTML5的到来,给我们提供了一个标准的方式来播放WEB中的音频文件,用户不再为浏览 ...
- 微信的audio无法自动播放的问题
一.问题 最近做了一个html5的项目,里面涉及到音乐播放,项目要求音乐进入页面就自动播放,于是我就想到了html5的audio标签,将mp3引入进去. 1.在audio标签里引入了autoplay属 ...
- HTML5 audio与video标签实现视频播放,音频播放
随着互联网的飞速发展以及HTML5的应用,越来越多的项目中用到video,audio当常用标签. <audio> 标签属性 <audio src="song.mp3&quo ...
- android audio无法自动播放
audio无法在android4.4+和ios6以上的版本自动播放,因为他们出于安全考虑,做了限制.必须用户自己手工点击才能播放,程序是控制不了播放的. 整死我了,整整搞了2天,查不出所以然,原来就这 ...
随机推荐
- 就算会用python画颗心,可你依然还是只单身狗
:) 标题是开玩笑的,千万别认真. 随着AI的飞速发展,有志于此行的码农也是急剧的增加,带来的就是大家对算法.数学的兴趣也格外升高. 本文的来历是这样,今天某老同事在朋友圈发了一张屏拍,求公式. 看了 ...
- Django项目开发
1.django中Form验证.CSRF.Cookie.Session.Model操作数据库 2.django介绍&快速实现简单留言系统 3.django开发在线教育网站
- SpringBoot+gradle项目构建war
前言 一开始觉得这并不是什么很难的事情,但是踩坑了之后才发现... 步骤 1.在build.gradle配置文件中加入apply plugin: 'war' 很多人以为这样就完事了,我一开始也是这样认 ...
- log4j2.yml配置文件
# https://blog.csdn.net/u010598111/article/details/80556437 # 共有8个级别,按照从低到高为:ALL < TRACE < DEB ...
- SVN就是这么简单
什么是SVN SVN全称:Subversion,是一个开放源代码的版本控制系统 Svn是一种集中式文件版本管理系统.集中式代码管理的核心是服务器,所有开发者在开始新一天的工作之前必须从服务器获取代码, ...
- C语言实现二叉树的创建&遍历
算法思想(重点是递归的使用) 利用扩展先序遍历序列创建二叉链表 采用类似先序遍历的递归算法,首先读入当前根结点的数据,如果是'.'则将当前 树根置为空,否则申请一个新结点,存入当前根结点的数据,分别 ...
- 设计模式总结篇系列:组合模式(Composite)
在探讨Java组合模式之前,先要明白几个概念的区别:继承.组合和聚合. 继承是is-a的关系.组合和聚合有点像,有些书上没有作区分,都称之为has-a,有些书上对其进行了较为严格区分,组合是conta ...
- ES6躬行记(16)——Set
ES6引入了两种新的数据结构:Set和Map.Set是一组值的集合,其中值不能重复:Map(也叫字典)是一组键值对的集合,其中键不能重复.Set和Map都由哈希表(Hash Table)实现,并可按添 ...
- SpringCloud-Greenwich版本新特性探索(1)---SpringCloudGateway
一.前言 1.SpringCloudGateway是SpringCloud新推出的网关框架,比较于上一代Zuul,功能和性能有很大的提升.Zuul1.x采用的是阻塞多线程方式,也就是一个线程处理一个连 ...
- 理解Python闭包概念
闭包并不只是一个python中的概念,在函数式编程语言中应用较为广泛.理解python中的闭包一方面是能够正确的使用闭包,另一方面可以好好体会和思考闭包的设计思想. 1.概念介绍 首先看一下维基上对闭 ...