[Libav-user] Efficient ignoring of video packets

Dave Craig davecraig at unbalancedaudio.com
Wed Jun 24 18:01:44 EEST 2020

FWIW: I found what I was looking for.

In order to not be returned packets for streams that you're not
interested in, the streams within a format context can have their
discard member set. In my case, to discard all but audio streams
something along the lines of:

AVFormatContext *format_context = NULL;
avformat_open_input(&format_context, filename, NULL, NULL);
avformat_find_stream_info(format_context, NULL));

for(unsigned i = 0; i < format_context->nb_streams; i++)
    AVStream *stream = format_context->streams[i];

    // During demux, discard all packets that aren't audio
    if(stream->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
        stream->discard = AVDISCARD_ALL;

The reason I didn't find this initially, was because the AVDiscard
enum appears at first glance to be decoder rather than demux related
(it's defined in avcodec.h), but in fact it is used in both.


On Tue, Jun 16, 2020 at 9:12 AM Dave Craig
<davecraig at unbalancedaudio.com> wrote:
> Hi,
> In my application, I want to be able to play just a single audio track
> from a file which also has loads of high bitrate video. Is there a way
> to configure an AVFormatContext such that av_read_frame only returns
> frames for the audio that I'm interested in? Apologies if I'm missing
> something obvious I'm fairly new to libav.
> For some container formats like transport stream the underlying code
> would still have to trawl through all of the packets inspecting them
> at some level, but with a MOV/MP4 container the code could just seek
> through to each audio block in turn ignoring the video track
> altogether. My current code skips past any packets with a stream_index
> that isn't the one I'm interested in which works fine, but there are
> 100 times more video than audio packets which makes this inefficient.
> Thanks for your help,
> Dave Craig

More information about the Libav-user mailing list