Extract I-frames from H264 to MPEG-TS in C

I am experimenting with video and would like to know how I can extract I-frames from H264 contained in an MPEG-TS container. What I want to do is generate preview images from the video stream. Since the I-frame should be the complete picture from which the P- and B-Frames are derived, is it possible to simply extract image data without the need for decoding using a codec?

I have already done some work with the MPEG-TS container format, but I'm not so specialized in codecs.

I rather seek information.

Many thanks.

+4
source share
3 answers

I am not an expert in this field, but I believe that the answer to your question is NO.

If you want to save the I-frame as a JPEG image, you still need to “transcode” the video frame, that is, you first need to decode the I-frame using an H264 decoder, and then encode it using a JPEG encoder. This is due to the fact that the JPEG encoder does not understand the H264 frame, it only accepts uncompressed video frames as input.

Aside, since the entrance to the JPEG encoder is an uncompressed frame, you can generate a JPEG image from any type of frame (I / P / B), since it will already be decoded (using the reference I-frame, if necessary) before being fed to the encoder.

+3
source

As others have noted, h.264 decoding is complicated. You can write your own decoder, but this is a serious effort. Why not use an existing decoder?

The Intel IPP Library has the basic building blocks for a decoder and a sample decoder:

Code Samples for Intel® Embedded Performances

There libavcodec:

Using libavformat and libavcodec

Revised avcodec_sample.0.4.9.cPP

+3
source

I am also not an expert in this domain. But I played with decryption. Use this gstreamer pipeline to extract the preview from video.mp4:

gst-launch -v filesrc location=./video.mp4 ! qtdemux name=demux demux.video_00 ! ffdec_h264 ! videorate ! 'video/x-raw-yuv,framerate=1/1' ! jpegenc ! multifilesink location=image-%05d.jpeg 

If you want to write some code, replace the video with appsrc / appsink elements. Write a conveyor management program (see example ):

 filesrc location=./video.mp4 ! qtdemux name=demux demux.video_00 ! ffdec_h264 ! appsink appsrc ! 'video/x-raw-yuv,framerate=1/1' ! jpegenc ! multifilesink location=image-%05d.jpeg 

Buffers without a set of flags GST_BUFFER_FLAG_DELTA_UNIT are I-frames. You can safely skip many frames and trigger the decoding stream in any I-frame.

+2
source

Source: https://habr.com/ru/post/1403842/


All Articles