How can i know the duration of any h264 file?

牧云@^-^@ 提交于 2019-12-10 08:24:30

问题


I have one file in which only h264 frames are there in form of NAL unit. So now is there any method so i can count the duration of that file?

I dont know how many frames are there in file. I have only file size.

PS: All i want to do is in C language and on Linux platform.


回答1:


Your question is meaningless, that's why someone down vote you.

Container file

First, there is no such a thing as H264 file. There can be a container file (MP4, AVI, MKV) that holds the H264 encoded video. That container format usually also holds the data telling you the contained video duration. So you have to parse that file, check the format documentation to see where the duration is saved, and then extract it.

Easiest way is to use FFMPEG like Augusto suggested.

NAL Byte Stream

If your file is only NAL units saved one after another, like in a NAL byte stream, there IS NO WAY that you can get the video duration! Since H264 encoded picture doesn't hold timing information.

If that is the case, you still can make an approximation if you know the video FPS... You need to count all frames in the file excluding:

  • Sequence Parameter Sets
  • Picture Parameter Sets
  • End Of Stream
  • End Of Sequence
  • Filter Data
  • Access Unit Delimiter

And then do the math: NUMBER_OF_FRAMES / FPS = DURATION_IN_SECONDS

RTP stream

If you are wrong and there is RTP header on top of each NAL unit, you can easily get the video duration by checking the RTP time of each RTP header. What the bytes in RTP Header mean, you can find out here: http://www.networksorcery.com/enp/protocol/rtp.htm. You want to get the TIMESTAMP part (4 bytes integer) from each one. The code should do this:

int last_timestamp = GetNextTimestamp();
double duration_in_ms = 0;

while(true)
{
    int next = GetNextTimestamp();
    duration_in_ms += (next - last_timestamp)/90000.0;
    last_timestamp = next;
}



回答2:


The dirty way of getting the duration, which many, many devs use, is to run ffmpeg to get the information from the file, and parse what ffmpeg prints on stdout. I know it's not ideal, but it works.

If you run ffmpeg -i Sintel.2010.1080p.mkv (you can download Sintel from here), one of the output lines is Duration: 00:14:48.03, start: 0.000000, bitrate: 640 kb/s. You can parse the line and extract the duration.

You might be able to get the same information by calling some C function from ffmpeg directly, but that's outside of my knowledge.




回答3:


h.264 does contain timing information.

Raw annexb h.264 data in some form (memory or file) will contain some "Sequence Parameter Sets" or SPS frames. These will typically contain timing information, specifically "time_scale" and "num_units_in_tick". In most scenarios the frame rate is fixed which is specified by "fixed_frame_rate_flag" in the SPS. Duration for each frame will be 2 * num_units_in_tick / time_scale (2 == 1 + nuit_field_based_flag). Frames are typically separated by a "Access unit delimiter".

Number of frames times the frame duration will give you the total duration.



来源:https://stackoverflow.com/questions/7429382/how-can-i-know-the-duration-of-any-h264-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!