Hello,

I need to read mp4 video files, perform basic (simple file cut) on-line editing functions and write edited files to disk.

Question: Is there a means to programmatically access the video file playing time file information without having to play the file on media video player such as Quick time player and similar?

Without the exact knowledge of the playing time i will have to use unrelliable approximations.

I looked at the "video file inspector report " from the presentation tab of the Quick time player and similar player , and tried to establish a relation between file size, frame per seconds, sampling rates , throughput (kbits/secs) and playing time, but calculation results are not consistant with real playing time.

Thanks

monere

Dani AI

Generated

The simplest reliable answer: you do not need to play the file to get its playback time — the container usually records it. was pointing in the right direction (read the container), and was right to suggest using a library instead of guessing from filesize/fps (which fails for VBR, container overhead and fragmented files). 's layout link is the kind of reference that shows where the fields live.

For MP4/ISO-BMFF files the movie header box contains the fields you want. Inside the moov atom the mvhd (movie header) box holds a timescale and a duration. Compute seconds as:

seconds = duration / timescale

mvhd has two versions: version 0 uses 32-bit duration, version 1 uses 64-bit. All multi-byte fields are big-endian. If moov is missing (or the file is a fragmented MP4), duration may not be present or may be in other boxes (mdhd per-track, sidx/tfdt for fragments), so handle those cases.

Minimal pseudo-workflow (manual parse or library):

  • locate moov, find mvhd
  • read version, then timescale (32-bit) and duration (32- or 64-bit)
  • compute seconds = (double)duration / timescale

Example libraries/tools that do this robustly: FFmpeg/libavformat (use ffprobe or lib API), Bento4 (mp4info), and MediaInfo. These handle large files, fragmented mp4, and corner cases. See FFmpeg docs for ffprobe usage (FFmpeg ffprobe) and MediaInfo for a simple CLI/API approach (MediaInfo).

Notes/cautions: some producers put moov at file end (slow to scan), some files intentionally omit global duration (streaming/fragmented), and some tools overwrite incorrect metadata — always validate results (check per-track mdhd if global duration looks wrong).

Recommended Answers

All 3 Replies

You need to search out the binary AVI file format and read the file header. The length should be in the header.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.