ffmpeg分离音频重采样

#include
#include
extern “C” // 因为FFmpeg是纯C程序
{
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
};
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 < 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);
}

你可能感兴趣的:(ffmpeg)