4

私はffmpegの初心者です。一部のメディアに複数のオーディオ ストリームがある場合に問題が発生します。MKV ファイルに 3 つのオーディオ ストリーム (MP3、WMA、WMAPro) があるとします。

次を使用してデマルチプレクサするときにストリーム インデックスを変更するにはどうすればよいですか。

AVPacket inputPacket;
ret = av_read_frame(avInputFmtCtx, &inputPacket)

そのため、change_stream_index(int streamindex) のようなものを検索しています。その関数を呼び出すと (change_stream_index(2) とします)、次に av_read_frame を呼び出すと、MP3 ではなく WMAPro フレームが demux されます。

みんなありがとう!

4

3 に答える 3

1

まず、入力内のストリームの数を確認します。次に、それらをいくつかのバッファに書き込みます(私の場合、ストリームは2つしかありませんが、簡単に拡張できます)

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);
    }

ストリームがどの順序で入ってくるかわからないため、コーデックの名前も確認する必要がありptrFormatContext->streams[i]->codec->codec_nameます。次に、関連する target_format のインデックスを保存します。次に、指定されたインデックスを介してストリームにアクセスできます。

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
        }
    }

私は私のプログラムからいくつかの抜粋をコピーしましたが、これが入力からストリームを選択する方法を理解するのに役立つことを願っています. 完全なコードではなく抜粋のみを投稿したので、自分で初期化などを行う必要がありますが、質問があれば喜んでお手伝いします!

于 2012-01-20T23:57:37.610 に答える