ffmpeg单纯播放视频中的音频(2018)

本文介绍了如何利用ffmpeg工具从视频文件中提取并播放音频,适用于需要独立处理视频音频流的场景。
摘要由CSDN通过智能技术生成

ffmpeg3版本的解码接口做了不少调整,网上找了好多 代码学习,百分九十九都会有 声明为己否决的问题
以下代码是更新了新接口 vs2015 x64下运行。

#include <iostream>
#include <stdio.h>
#include <assert.h>
#include <SDL.h>
#include <SDL_thread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define __STDC_CONSTANT_MACROS

extern "C"{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#include "libavutil/audio_fifo.h"
};

#ifdef __MINGW32__
#undef main // Prevents SDL from overriding main().
#endif
Uint32  audio_len;//每音频帧的长度
Uint8  *audio_pos;//目前接收播放位置
AVAudioFifo* audiofifo = NULL;//
int ct = 0;
DWORD start_time, end_time;
//音频回调函数   用户数据   要填充的音频缓存区头指针  缓存区大小
void  fill_audio(void *udata, Uint8 *stream, int len) {
	
	SDL_memset(stream, 0, len);//初始化缓存区
	if (audio_len == 0)
		return;
	len = (len>audio_len ? audio_len : len);  
	SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
	audio_pos += len;
	audio_len -= len;
	//SDL_Delay(20);
}
int main(int argc, char* argv[])
{
	AVFormatContext *pFormatCtx;
	int             i, audioStream;
	AVCodecContext  *pCodecCtx;
	AVCodec         *pCodec;
	AVPacket        *packet;
	AVFrame         *pFrame;
	SDL_AudioSpec wanted_spec;
	int ret;
	uint32_t len = 0;
	int64_t in_channel_layout;
	struct SwrContext *au_convert_ctx;
	FILE *pFile = NULL;
	//char url[] = "yinyuepenquan.mp4";
	char url[] = "zzz.mp4";

	start_time = GetTickCount();
	//初始化
	av_register_all();
	avformat_network_init();
	pFormatCtx = avformat_alloc_context();
	if (avformat_open_input(&pFormatCtx, url, NULL, NULL) != 0) {
		printf("Couldn't ope
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值