简体   繁体   English

如何使用 FFmpeg 和 C/C++ 将数据流添加到 MXF(使用 mpeg2video)文件中

[英]How add Data Stream into MXF(using mpeg2video) file with FFmpeg and C/C++

I'm a little bit stuck here trying create a MXF file with data stream on it.我有点卡在这里尝试创建一个带有数据流的 MXF 文件。 I have several MXF video files that contain this standard我有几个包含此标准的 MXF 视频文件

**1 Video Stream:
     Stream #0:0: Video: mpeg2video (4:2:2), yuv422p(tv, bt709, top first), 1920x1080 [SAR 1:1 DAR 16:9], 50000 kb/s, 29.9
16 audio streams
     Audio: pcm_s24le, 48000 Hz, 1 channels, s32 (24 bit), 1152 kb/s
1 Data Stream:
     Data: none**

This data stream, contain personal data inside video file.此数据流,包含视频文件中的个人数据。 I can open this stream and data is really there.我可以打开这个流,数据真的就在那里。 Is all ok.一切正常。 But, when i try to create a file exactly like this, everytime i call "avformat_write_header" it returns an error.但是,当我尝试创建一个完全像这样的文件时,每次我调用“avformat_write_header”时它都会返回一个错误。

If i do comment the creation of this data streams the video file is succeffully created.如果我评论此数据流的创建,则视频文件已成功创建。

If i change to "mpegts" with this data stream, the video file is also succeffully created.如果我将此数据流更改为“mpegts”,则视频文件也会成功创建。

But, i can't use mpets and i need this data stream.但是,我不能使用 mpets,我需要这个数据流。

I know that is possible MXF with data stream cause i have this originals files that have this combination.我知道 MXF 可能带有数据流,因为我有具有这种组合的原始文件。

So, i know that i missing something in my code.所以,我知道我的代码中遗漏了一些东西。

This is the way i create this Data Stream:这是我创建此数据流的方式:

 void CFFmpegVideoWriter::addDataStream(EOutputStream *ost, AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id)
    {
        AVCodecParameters *par;

        ost->stream = avformat_new_stream(oc, NULL);
        if (ost->stream == NULL)
        {
            fprintf(stderr, "OOooohhh man: avformat_new_stream() failed.\n");
            return;
        }

        par = ost->stream->codecpar;
        ost->stream->index = 17;
        par->codec_id = AV_CODEC_ID_NONE;
        par->codec_type = AVMEDIA_TYPE_DATA;

        ost->stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    }

the file openning is this:文件打开是这样的:

CFFMpegVideoWriter::CFFMpegVideoWriter(QString outputfilename) : QThread()
{
    av_register_all();
    avcodec_register_all();

    isOpen = false;
    shouldClose = false;

    frameIndex = 0;

#ifdef __linux__
    QByteArray bFilename = outputfilename.toUtf8();
#else
    QByteArray bFilename = outputfilename.toLatin1();
#endif

    const char* filename = bFilename.data();

    codecContext = NULL;

    //encontra o formato desejado...
    outputFormat = av_guess_format("mp2v", filename, nullptr);
    if (!outputFormat)
    {
        qDebug("Could not find suitable output format\n");
        return;
    }

    //encontra o codec...
    codec = avcodec_find_encoder(outputFormat->video_codec);
    if (!codec)
    {
        qDebug( "Codec not found\n");
        return;
    }

    //aloca o contexto do codec...
    codecContext = avcodec_alloc_context3(codec);
    codecContext->field_order = AV_FIELD_TT;
    codecContext->profile = FF_PROFILE_MPEG2_422;

    //aloca o contexto do formato...
    formatContext = avformat_alloc_context();
    formatContext->oformat = outputFormat;

    //aloca o contexto da midia de saida...
    avformat_alloc_output_context2(&formatContext, NULL, NULL, filename);
    if (!formatContext)
    {
        qDebug("Erro");
        return;
    }

    videoStream.tmp_frame = NULL;
    videoStream.swr_ctx = NULL;

    //adiciona a stream de video...
    if (outputFormat->video_codec != AV_CODEC_ID_NONE)
    {
        addVideoStream(&videoStream, formatContext, &video_codec, outputFormat->video_codec);       
    }

    //adiciona as 16 streams de audio...
    if (outputFormat->audio_codec != AV_CODEC_ID_NONE)
    {
        for (int i = 0; i < 16; i++)
        {
            addAudioStream(&audioStream[i], formatContext, &audio_codec, outputFormat->audio_codec);
        }       
    }

    addDataStream(&datastream, formatContext, &video_codec, outputFormat->video_codec);     

    videoStream.sws_ctx = NULL;
    for (int i = 0; i < 16; i++)
    {
        audioStream[i].sws_ctx = NULL;
    }   
    opt = NULL;


    //carreca o codec de video para stream de video...      
    initVideoCodec(formatContext, video_codec, &videoStream, opt);


    //carrega o codec de audio para stream de audio...s
    for (int i = 0; i < 16; i++)
    {
        initAudioCodec(formatContext, audio_codec, &audioStream[i], opt);
    }


    av_dump_format(formatContext, 0, filename, 1);

    //abrea o arquivo de saida..
    if (!(outputFormat->flags & AVFMT_NOFILE))
    {
        ret = avio_open(&formatContext->pb, filename, AVIO_FLAG_WRITE);
        if (ret < 0)
        {
            qDebug("Could not open'%s", filename);
            return;
        }
    }

    //escreve o cabecalho do arquivo...
    ret = avformat_write_header(formatContext, &opt);
    if (ret < 0)
    {
        qDebug("Error occurred when opening output file");
        return;
    }

    isOpen = true;

    QThread::start();
}

The code always fails at "avformat_write_header" call.代码总是在“avformat_write_header”调用失败。

But if i remove "datastream" or change it to mpegts everything runs fine.但是,如果我删除“数据流”或将其更改为 mpegts,则一切正常。

Any ideia of what am i doing wrong here?我在这里做错了什么?

Thanks for reading this.感谢您阅读本文。

Helmuth赫尔穆斯

After some long hours trying a lot of solutions i found what was wrong.经过很长时间尝试了很多解决方案后,我发现了问题所在。 I had to add a metadata item specifing data type.我必须添加一个指定数据类型的元数据项。

In my case, data type was "vbi_vanc_smpte_436M" wich is supported by MXF.就我而言,数据类型是"vbi_vanc_smpte_436M" ,MXF 支持。

so, i dit with:所以,我用:

av_dict_set(&out_stream->metadata, "data_type", "vbi_vanc_smpte_436M",  AV_DICT_IGNORE_SUFFIX);  

Then everything works fine.然后一切正常。

I hope this can help anyone else with same problem.我希望这可以帮助其他有同样问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM