K
K
Kalombyr2020-06-23 20:26:38
Video
Kalombyr, 2020-06-23 20:26:38

How to "cut" h264 file?

Good afternoon.
The DVR writes files in the "*.h264" format to the disk in the bare form.
They are perfectly reproduced by the VLC player.
I need to cut these files into several. As I understand it, these are just frames from the camera.
The problem is that if I do it "inside", i.e. I just cut out a part in an arbitrary place and save it as a new file, VLC no longer plays them (when you click on the play, nothing happens). But sometimes it still succeeds and the file plays normally. Are there any headers needed?
In general, how can you cut without using video editors and so on (planning to write in C ++)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Armenian Radio, 2020-06-23
@gbg

In short, you'll be swollen by hand sorting out all that nonsense from bits, which is a file in h264 format. There, a container in a container drives a container.
Get libav and don't break your head.
Long:
h264 is a video compression format that contains anchor and all sorts of different intermediate frames. The reference frame contains everything that is needed to initialize the codec, so if you guess carefully, you can ensure that the decoder can initialize and continue working. However, for this, you need to carefully analyze the entire bitstream.
Further, the compressed video may lie in some kind of media container, of which there are many. Most likely, the regik writes to h264-annex B (this is what you need to google), which is a chain of NAL blocks of various sorts. Among them there are blocks containing codec settings, by which you can understand that this frame is an I-Frame, and you can cut the video on it. But there you still need to come up with something with timestamps, otherwise, instead of rewinding, you will end up with a madhouse.
Each NAL block contains a header, and ends with the value 00 00 00 01
So the first step is to write a dump that will pick out the NAL blocks and write their size and type. You can google such a dump (they exist) and sit and compare.

P
Pavel K, 2020-06-24
@PavelK

A simple option: from the main file to each "cut" copy the first 100-200 bytes to the beginning.
VLC, in principle, will eat. It will not work with all equipment, but most Chinese do not bother with compliance with the entire RTSP standard of the RTP stream and h.264.
A more correct and working option:
1. You cannot cut the file anywhere.
In the file "frames" (the so-called NAL unit), separated by 4 bytes: "00 00 00 01" - find the closest "cuts" from the place.
2. To play h.264, you need to transfer the decoding settings. Most likely they will be at the very beginning of the file. It should look something like this:
00000001
SPS (Sequence parameter set)
00000001
PPS (Picture parameter set)
00000001
and then everything will work fine.
But you will have to study the h.264 specification well (especially about NAL), because, perhaps, VLC may also require an I-frame (key, without references to previous frames) in the first place.
In short, here: https://yumichan.net/video-processing/video-compre...

A
Alexander Skusnov, 2020-06-23
@AlexSku

There are examples in the DirectShow and Media Foundation libraries. You can first visually build a flow graph using the GraphEdit and TopoEdit applications.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question