Advertisement
Guest User

Untitled

a guest
May 10th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. // Open file function (minus the error checking)
  2. avformat_open_input(&pFormatCtx, filepath.c_str(), NULL, NULL);
  3. avformat_find_stream_info(pFormatCtx, NULL);
  4. stream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
  5. context = pFormatCtx->streams[stream]->codec;
  6. avcodec_open2(context, codec, NULL);
  7.  
  8. // Resampler
  9. resContext = swr_alloc();
  10. av_opt_set_int(resContext, "in_channel_layout", context->channel_layout, 0);
  11. av_opt_set_int(resContext, "out_channel_layout", context->channel_layout, 0);
  12. av_opt_set_int(resContext, "in_sample_rate", context->sample_rate, 0);
  13. av_opt_set_int(resContext, "out_sample_rate", 44100, 0);
  14. av_opt_set_sample_fmt(resContext, "in_sample_fmt", context->sample_fmt, 0);
  15. av_opt_set_sample_fmt(resContext, "out_sample_fmt", AV_SAMPLE_FMT_S16P, 0);
  16. swr_init(resContext);
  17.  
  18.  
  19. // and when it comes to reading frames
  20. av_read_frame(pFormatCtx, &pkt);
  21. 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.
  22. avcodec_decode_audio4(context, &nFrame, &gotFrame, &pkt);
  23. data_size = av_samples_get_buffer_size(NULL, context->channels, frame->nb_samples, AV_SAMPLE_FMT_S16P, 1) / context->channels;
  24. swr_convert(resContext, barr, frame->nb_samples, (const uint8_t**)frame->extended_data, frame->nb_samples);
  25. av_free_packet(&pkt);
  26.  
  27. // 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.
  28. // 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