Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong total duration caused by divided by zero conversion #645

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Methapon2001
Copy link

@Methapon2001 Methapon2001 commented Nov 24, 2024

fixes #601

I came into this issue when trying to create music player as my learning project and notice that total duration is not quite right as the music ended before total duration reached.

The cause seems to be related to 0 division and convert to u32.

I also added test case for the conversion, I'm not sure if it is needed.
Any feedback is welcome.

Code:

        let total_duration = track
            .codec_params
            .time_base
            .zip(track.codec_params.n_frames)
            .map(|(time_base, n_frames)| time_base.calc_time(n_frames));

        total_duration.map(|time| {
            let hours = time.seconds / (60 * 60);
            let mins = (time.seconds % (60 * 60)) / 60;
            let secs = f64::from((time.seconds % 60) as u32) + time.frac;

            println!("Time: {}", format!("{}:{:0>2}:{:0>6.3}", hours, mins, secs))
        });

        total_duration.map(|Time { seconds, frac }| {
            let dur = Duration::new(seconds, (1f64 / frac) as u32).as_secs();

            let hours = dur / (60 * 60);
            let mins = dur / 60;
            let secs = dur % 60;

            println!(
                "Duration: {}",
                format!("{}:{:0>2}:{:0>2}", hours, mins, secs)
            );
        });

        total_duration.map(|Time { seconds, frac }| {
            let dur =
                Duration::new(seconds, if frac > 0.0 { (1f64 / frac) as u32 } else { 0 }).as_secs();

            let hours = dur / (60 * 60);
            let mins = dur / 60;
            let secs = dur % 60;

            println!(
                "Fixed Divided by 0 Duration: {}",
                format!("{}:{:0>2}:{:0>2}", hours, mins, secs)
            );
        });

Result:

Time: 0:00:07.000
Duration: 0:00:11
Fixed Divided by 0 Duration: 0:00:07

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect total_duration while using symphonia features
1 participant