Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Open file function (minus the error checking)
- avformat_open_input(&pFormatCtx, filepath.c_str(), NULL, NULL);
- avformat_find_stream_info(pFormatCtx, NULL);
- stream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
- context = pFormatCtx->streams[stream]->codec;
- avcodec_open2(context, codec, NULL);
- // Resampler
- resContext = swr_alloc();
- av_opt_set_int(resContext, "in_channel_layout", context->channel_layout, 0);
- av_opt_set_int(resContext, "out_channel_layout", context->channel_layout, 0);
- av_opt_set_int(resContext, "in_sample_rate", context->sample_rate, 0);
- av_opt_set_int(resContext, "out_sample_rate", 44100, 0);
- av_opt_set_sample_fmt(resContext, "in_sample_fmt", context->sample_fmt, 0);
- av_opt_set_sample_fmt(resContext, "out_sample_fmt", AV_SAMPLE_FMT_S16P, 0);
- swr_init(resContext);
- // and when it comes to reading frames
- av_read_frame(pFormatCtx, &pkt);
- avcodec_get_frame_defaults(&nFrame); // I'm fairly certain I'm doing something wrong here, but it doesn't work with out it. I don't think it's the issue I'm talking about though.
- avcodec_decode_audio4(context, &nFrame, &gotFrame, &pkt);
- data_size = av_samples_get_buffer_size(NULL, context->channels, frame->nb_samples, AV_SAMPLE_FMT_S16P, 1) / context->channels;
- swr_convert(resContext, barr, frame->nb_samples, (const uint8_t**)frame->extended_data, frame->nb_samples);
- av_free_packet(&pkt);
- // I realise there's a few things I should probably be doing better, but this is just kind of bare-bones until I can get it working the way I want it to. Nothing I am doing explains (at least, to me) why the audio data I am fetching ends 20 seconds short of the end of the file, when a frame lasts nowhere near that long.
- // I appreciate any help you can provide, I suspect it's just something silly I'm doing with the file set up.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement