[Libav-user] What is the correct way of writing NAL units to file?

JooAun Saw jasaw81 at gmail.com
Wed Sep 4 07:17:38 EEST 2019

Hi all,

I am using a slightly modified encode_video example to feed one frame
at a time to h264_v4l2m2m encoder, and getting encoded packets using
avcodec_receive_packet function, and write to output file using
av_write_frame. The very first packet that comes out of the encoder is
always the NAL units [ 00 00 00 01 67 4d 00 0a da 01 40 16 e4 00 00 00
01 68 ce 01 9e 20 ], probably because the v4l2_m2m encoder sets the

My questions are:

- How do I detect which packet is the NAL unit, and which is I frame
or P frame? Currently I look for packet PTS == 0 and AV_PKT_FLAG_KEY
not set. Is there a better way?

- What is the correct way of writing this NAL unit packet to my output
file? I am using av_write_frame function to write this NAL unit
packet, but when I run ffprobe, I get this error:

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x18efb00] st: 0 edit list: 1 Missing key
frame while searching for timestamp: 0
[h264 @ 0x18f0fb0] no frame!

If I manually copy the NAL units into the first I frame packet (by
doing av_grow_packet, memmove,  memcpy), then ffprobe runs without any
issue. This seems like an ugly way of handling NAL unit packet.

Any help or guidance is greatly appreciated. Thanks in advance.


More information about the Libav-user mailing list