Anoop AR.
Anoop AR.

Reputation: 128

How to change the audio track of a video on the fly using ffmpeg in c++?

I am trying to develop a media player application in android. I am using dolphin player's code as reference. But i don't know how to change the audio and subtitle track on the fly while playing a video . using command line of ffmpeg it's possible but how to do it with ffmpeg c++ code?

Upvotes: 1

Views: 2748

Answers (1)

Sergey K.
Sergey K.

Reputation: 25396

Video file is a container that have a bunch of streams. First, you have to open video file and extract streams information (error checking avoided for smaller code):

AVFormatContext *pFormatCtx;
av_open_input_file( &pFormatCtx, argv[1], NULL, 0, NULL );
av_find_stream_info( pFormatCtx );

Now you can iterate streams and find all audio streams:

for ( i=0; i != pFormatCtx->nb_streams; i++ )
{
   if (pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO) audioStreams.push_back( i );
}

Select which stream you would like to play and get a pointer to the codec and open it:

AVCodecContext* aCodecCtx = pFormatCtx->streams[SelectedAudioStream]->codec;
AVCodec* aCodec = avcodec_find_decoder(aCodecCtx->codec_id);
avcodec_open(aCodecCtx, aCodec);

Decode the stream as usual after that.

Upvotes: 2

Related Questions