Eh bien, vous vérifiez d'abord le nombre de flux dans l'entrée. Ensuite, vous écrivez dans une mémoire tampon (dans mon cas, je n'ai que 2 cours d'eau, mais vous pouvez facilement étendre cette)
ptrFormatContext = avformat_alloc_context();
if(avformat_open_input(&ptrFormatContext, filename, NULL, NULL) != 0)
{
qDebug("Error opening the input");
exit(-1);
}
if(av_find_stream_info(ptrFormatContext) < 0)
{
qDebug("Could not find any stream info");
exit(-2);
}
dump_format(ptrFormatContext, 0, filename, (int) NULL);
for(i=0; i<ptrFormatContext->nb_streams; i++)
{
switch(ptrFormatContext->streams[i]->codec->codec_type)
{
case AVMEDIA_TYPE_VIDEO:
{
if(videoStream < 0) videoStream = i;
break;
}
case AVMEDIA_TYPE_AUDIO:
{
if(audioStream < 0) audioStream = i;
}
}
}
if(audioStream == -1)
{
qDebug("Could not find any audio stream");
exit(-3);
}
if(videoStream == -1)
{
qDebug("Could not find any video stream");
exit(-4);
}
Puisque vous ne savez pas dans quel ordre les cours d'eau viennent, vous aurez également pour vérifier le nom du codec: ptrFormatContext->streams[i]->codec->codec_name
, puis enregistrez l'index pour le format target_format. Ensuite, vous pouvez simplement accéder au flux à travers l'index donné:
while(av_read_frame(ptrFormatContext,&ptrPacket) >= 0)
{
if(ptrPacket.stream_index == videoStream)
{
//decode the video stream to raw format
if(avcodec_decode_video2(ptrCodecCtxt, ptrFrame, &frameFinished, &ptrPacket) < 0)
{
qDebug("Error decoding the Videostream");
exit(-13);
}
if(frameFinished)
{
printf("%s\n", (char*) ptrPacket.data);
//encode the video stream to target format
// av_free_packet(&ptrPacket);
}
}
else if (ptrPacket.stream_index == audioStream)
{
//decode the audio stream to raw format
// if(avcodec_decode_audio3(aCodecCtx, , ,&ptrPacket) < 0)
// {
// qDebug("Error decoding the Audiostream");
// exit(-14);
// }
//encode the audio stream to target format
}
}
Je viens de copier quelques extraits d'un programme de la mienne, mais cela vous aidera à comprendre comment je l'espère pour sélectionner les flux de l'entrée. Je n'ai pas posté de code complet, seulement des extraits, donc vous devrez faire une initialisation, etc par vous-même, mais si vous avez des questions, je serai ravi de vous aider!