ffmpeg分离音频重采样

#include
#include
extern “C” // 因为FFmpeg是纯C程序
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
#include <libswresample/swresample.h>
};
using namespace std;
#define FRAME_SIZE 3200
#define SAMPLE_RATE 16000
int main()
{
AVCodecContext pCodecCtx;
AVCodec pCodec;
AVPacket packet;
char input = “/workspace/src/ffmpegdemo/data/3.mp4”;
char output = “/workspace/src/ffmpegdemo/data/1.pcm”;
enum AVSampleFormat out_sample_fmt = AV_SAMPLE_FMT_S16;
FILE * f1 = fopen(output,“wb”);
av_register_all();
avformat_network_init();
// get format from audio file
cout << "open: " << input <<endl;
AVFormatContext
format = avformat_alloc_context();
if (avformat_open_input(&format, input, NULL, NULL) != 0) {
return NULL;
}
if (avformat_find_stream_info(format, NULL) < 0) {
return NULL;
}
// Find the index of the first audio stream
int stream_index =- 1;
for (int i=0; inb_streams; i++) {
if (format->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
stream_index = i;
break;
}
}
if (stream_index == -1) {
return NULL;
}
AVStream
stream = format->streams[stream_index];
pCodecCtx = format->streams[stream_index]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec == NULL){
printf(“Codec not found.\n”);
}
if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){
printf(“Could not open codec.\n”);
}
// prepare resampler
struct SwrContext
swr = swr_alloc();
av_opt_set_int(swr, “in_channel_count”, pCodecCtx->channels, 0);
av_opt_set_int(swr, “out_channel_count”, 1, 0);
av_opt_set_int(swr, “in_channel_layout”, pCodecCtx->channel_layout, 0);
av_opt_set_int(swr, “out_channel_layout”, AV_CH_LAYOUT_MONO, 0);
av_opt_set_int(swr, “in_sample_rate”, pCodecCtx->sample_rate, 0);
av_opt_set_int(swr, “out_sample_rate”, SAMPLE_RATE, 0);
av_opt_set_sample_fmt(swr, “in_sample_fmt”, pCodecCtx->sample_fmt, 0);
av_opt_set_sample_fmt(swr, “out_sample_fmt”, out_sample_fmt, 0);
swr_init(swr);
if (!swr_is_initialized(swr)) {
fprintf(stderr, “Resampler has not been properly initialized\n”);
return NULL;
}
// prepare to read data
av_init_packet(&packet);
AVFrame
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, “Error allocating the frame\n”);
return NULL;
}
// iterate through frames
char data = NULL;
cout << “读取video frame” << endl;
while (av_read_frame(format, &packet) >= 0)
{
// decode one frame
if (packet.stream_index != stream_index)
{
continue;
}
int gotFrame;
if (avcodec_decode_audio4(pCodecCtx, frame, &gotFrame, &packet) < 0) {
break;
}
if (!gotFrame) {
continue;
}
uint8_t
buffer;
av_samples_alloc((uint8_t**) &buffer, NULL, 1, frame->nb_samples,
out_sample_fmt, 0);
cout << "pkt.size: " << frame->pkt_size << endl;
int frame_count = swr_convert(swr, (uint8_t**) &buffer, frame->nb_samples,
(const uint8_t**) frame->data, frame->nb_samples);
data = (char*) realloc(data, frame_count * sizeof(char));
memcpy(data, buffer, frame_count * sizeof(char));
fwrite(data,1,frame_count * sizeof(char),f1);
}
av_frame_free(&frame);
swr_free(&swr);
avcodec_close(pCodecCtx);
avformat_free_context(format);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值