matroska icon indicating copy to clipboard operation
matroska copied to clipboard

Unable to display info.duration as other than seconds

Open mabushey opened this issue 4 years ago • 2 comments

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.

mabushey avatar Jun 20 '20 03:06 mabushey

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);

mabushey avatar Jun 20 '20 04:06 mabushey

According to the specification duration is expressed as:

Duration 2 0x4489 - - > 0.0 - f * * * * * Duration of the Segment in nanoseconds based on TimestampScale.

lu-zero avatar Jun 20 '20 04:06 lu-zero

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.

FreezyLemon avatar Apr 09 '23 19:04 FreezyLemon

It seems that @FreezyLemon answer can solve this issue

Luni-4 avatar Apr 11 '23 07:04 Luni-4