build-a-video-container-parser / lesson-26.md
Lesson 26 · Putting it together

Duration in seconds

A raw duration is meaningless without its timescale - together they give seconds, and a little formatting turns those seconds into a readable clock time. Today you compute and present a real duration, the first genuinely human-facing output the parser makes.

The goal

Convert a duration and timescale into seconds, and format seconds as a minutes:seconds string.

Start here - the target
TO DO
Scenario: Duration becomes seconds and a readable clock
Givena track with mdhd duration 132300 and timescale 44100
Whenits duration in seconds is computed
Thenit is exactly 3.0, and a movie with mvhd duration 5000 at timescale 1000 is exactly 5.0
Andformatting seconds as minutes:seconds gives "0:03" for 3.0 and "2:05" for 125.0
Background

Every duration in the file is expressed in timescale units, not seconds, so on its own a number like 132300 says nothing. The conversion is simply duration / timescale: at a timescale of 44100, 132300 units is exactly 3.0 seconds; at the movie’s 1000 timescale, 5000 units is 5.0 seconds. The division must be floating point, since durations rarely land on whole seconds.

A raw 3.0 is still not what a person wants to read, so the companion step formats it as a clock: split the whole seconds into minutes and seconds and print minutes:zero-padded-seconds, so 3.0 becomes 0:03 and 125.0 becomes 2:05. This is why mvhd and mdhd each carry their own timescale - a track’s media clock is independent of the movie clock - and both of these small functions feed straight into the track summary you assemble next.

Make it work
// seconds = duration / timescale (as floating point)
func seconds(duration uint64, timescale uint32) float64 {
return float64(duration) / float64(timescale)
}
// format whole seconds as M:SS (minutes, zero-padded seconds)
func formatDuration(sec float64) string { /* fill in */ }
CheckpointDONE
You can report durations in seconds and as a clock. Commit and stop here.