Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I encode KLV packets to an H.264 video using libav*

Currently I am using libav* to encode H.264 videos. I want to add KLVPackets to the bitstream but do not know where to implement it.

There is a struct within avcodec, but I am unsure of how to write it out to the frame metadata

typedef struct {
    UID key;
    int64_t offset;
    uint64_t length;
} KLVPacket;

Current FFMPEG code (only left relevant code):

av_register_all();

pOutputFormat = av_guess_format(NULL, fileName, NULL);
pFormatCtx=avformat_alloc_context();
pVideoStream = av_new_stream(pFormatCtx,0);
pCodecCtx=pVideoStream->codec;
...
av_dump_format(pFormatCtx, 0, fileName,1);
pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
avio_open(&pFormatCtx->pb, fileName, AVIO_FLAG_READ_WRITE)
avformat_write_header(pFormatCtx, &pDict);
...
avcodec_encode_video(pCodecCtx,outbuf,outbuf_size,ppicture);
...
int ret = av_interleaved_write_frame(pFormatCtx, &pkt);

Anyone know of any examples I can work from?

like image 304
JonnyCplusplus Avatar asked Nov 13 '22 14:11

JonnyCplusplus


1 Answers

KLV Metadata is meant to be a stream separate to the video. You mux in the stream into an MPEG-2 Transport Stream with its own PID.

An alternative implementation is to send the KLV as a separate stream. That is, broadcast your video on one IP/Port and your KLV on another IP/Port.

Either way, your biggest issue will be synchronizing the KLV data to the video. I have yet to find an open source library that muxes in KLV to video well. There are a few libraries you can pay for, but I have yet to use any one of them.

like image 134
It'sPete Avatar answered Nov 15 '22 05:11

It'sPete