Issue with Delayed Packet Writing to MP4 Container Using Hardware Encoder (h264_v4l2m2m) in FFmpeg API
Hello. I am trying to store the encoded packets to a mp4 container file using av_interlenced_write_packet() ffmpeg api. With Hardware encoder[h264_v4l2m2m] there is an issue when I add 1 second delay to write encoded Packets to the mp4 container. While writing the packets there are no errors but the encoded video is not playing.When verified with *mediainfo* CLI, got some partial metadata about the video. NOTE: Without delay there is no issue with recorded video. - *Mediainfo output of video without delay* [image: mediainfo_of_no_delay.png] - *Mediainfo output of video with delay* [image: mediainfo_of_with_delay.png] The flow of my code is as described in the text and illustrated in the flow diagram. ... ->send packet to encode ->Receive packet ->then clone the packet with av_packet_clone(sourcePacket) ->modified the pts & dts and ->writing frames to disk using av_interleaved_write_frame <https://www.ffmpeg.org/doxygen/4.0/group__lavf__encoding.html#ga37352ed2c63493c38219d935e71db6c1> (AVFormatContext <https://www.ffmpeg.org/doxygen/4.0/structAVFormatContext.html> *s <https://www.ffmpeg.org/doxygen/4.0/llviddspenc_8c.html#a517a4433fe5466cc6af1c80c538b0881> , AVPacket <https://www.ffmpeg.org/doxygen/4.0/structAVPacket.html> *pkt <https://www.ffmpeg.org/doxygen/4.0/demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090> ) ... [image: ffmpeg.png] 1. Why is the issue occurring with a delay of about 1 second? 2. How can this issue be resolved?
After debugging, I found that with the software encoder, the encoded packet data includes the header information, whereas with the hardware encoder, a lot of this information is missing. I tried to debug and print all the AVPacket structure members, but that did not provide any useful insights. The issue arises when I miss the first packet while storing it in an MP4 container using the hardware encoder. This results in the video file becoming corrupt. https://ffmpeg.org/doxygen/3.2/structAVPacket.html Is there any approach to solve this issue? Or is there a way to ensure that the headers in hardware-encoded packets are similar to those in software-encoded packets? (With the software encoder, there is no issue, even when we store delayed packets.) I even tried to hard-code the first packet using data generated by a software-encoded packet, but that did not work either. Can anyone please help solve this issue? Where might I be going wrong? Am I following the correct flow? On Mon, Jan 27, 2025 at 2:12 PM Lisa <lisas965624@gmail.com> wrote:
Hello. I am trying to store the encoded packets to a mp4 container file using av_interlenced_write_packet() ffmpeg api.
With Hardware encoder[h264_v4l2m2m] there is an issue when I add 1 second delay to write encoded Packets to the mp4 container. While writing the packets there are no errors but the encoded video is not playing.When verified with *mediainfo* CLI, got some partial metadata about the video. NOTE: Without delay there is no issue with recorded video.
- *Mediainfo output of video without delay*
[image: mediainfo_of_no_delay.png]
- *Mediainfo output of video with delay*
[image: mediainfo_of_with_delay.png]
The flow of my code is as described in the text and illustrated in the flow diagram. ... ->send packet to encode ->Receive packet ->then clone the packet with av_packet_clone(sourcePacket) ->modified the pts & dts and ->writing frames to disk using av_interleaved_write_frame <https://www.ffmpeg.org/doxygen/4.0/group__lavf__encoding.html#ga37352ed2c63493c38219d935e71db6c1> (AVFormatContext <https://www.ffmpeg.org/doxygen/4.0/structAVFormatContext.html> *s <https://www.ffmpeg.org/doxygen/4.0/llviddspenc_8c.html#a517a4433fe5466cc6af1c80c538b0881> , AVPacket <https://www.ffmpeg.org/doxygen/4.0/structAVPacket.html> *pkt <https://www.ffmpeg.org/doxygen/4.0/demuxing__decoding_8c.html#a3d4c6562f0b27cf0cacbbea5c038c090> ) ... [image: ffmpeg.png]
1. Why is the issue occurring with a delay of about 1 second? 2. How can this issue be resolved?
participants (1)
-
Lisa