matroska
matroska copied to clipboard
Unable to display info.duration as other than seconds
Hello,
I'm pretty new to rust. I can get seconds from a mkv with let d = matroska.info.duration.unwrap();
however I'm unable to use any std::time::Duration functions like num_minutes(). Because it's not an integer, I can't even divide by 60 to get minutes. I would like to be able to output as HH:MM:SS. Would it be possible to add how to do this to the example?
Thanks.
In case this helps anyone, I was able to get this to work:
let d = matroska.info.duration.unwrap();
let mut total_sec = d.as_secs();
if (d.as_millis() % 1000) > 500 {
total_sec = total_sec + 1;
}
if total_sec > 3600 {
print!("{}:", total_sec/3600)
}
if total_sec > 60 {
print!("{:0>2}:", (total_sec/60) % 60);
}
println!("{:0>2}", total_sec % 60);
According to the specification duration is expressed as:
Duration | 2 | 0x4489 | - | - | > 0.0 | - | f | * | * | * | * | * | Duration of the Segment in nanoseconds based on TimestampScale. |
---|
This is very late, but one way of handling this correctly is:
let f = info.duration.unwrap();
let nanos = f * info.timestamp_scale as f64;
let d = Duration::from_nanos(nanos.round() as u64);
info.timestamp_scale
defaults to 1 million, which means the info.duration
is expressed in milliseconds. Other timestamp scales are somewhat uncommon I think.
It seems that @FreezyLemon answer can solve this issue