Skip to content

[Bug] srt字幕时间码,毫秒的计算错误 #412

@yck201

Description

@yck201

🔎 是否已搜索类似问题?

是,未找到相同问题

🚨 问题详情与复现方式

问题现象

srt字幕文件中,时间码的毫秒部分计算错误,例如,73.23应该转换为00:01:13,230,但实际转换为00:01:13,023

关键代码

https://github.com/yaobiao131/downkyicore/blame/main/DownKyi.Core/BiliApi/Models/Json/SubtitleJson.cs

/// <summary>
/// 秒数转 时:分:秒 格式
/// </summary>
/// <param name="seconds"></param>
/// <returns></returns>
private static string Second2hms(float seconds)
{
    if (seconds < 0)
    {
        return "00:00:00,000";
    }

    int i = (int)Math.Floor(seconds / 1.0);
    int dec = (int)(Math.Round(seconds % 1.0f, 2) * 100);
    if (dec >= 100)
    {
        dec = 99;
    }

    int min = (int)Math.Floor(i / 60.0);
    int second = (int)(i % 60.0f);

    int hour = (int)Math.Floor(min / 60.0);
    min = (int)Math.Floor(min % 60.0f);

    return $"{hour:D2}:{min:D2}:{second:D2},{dec:D3}";
}

建议修改

int dec = (int)(Math.Round(seconds % 1.0f, 3) * 1000);
if (dec >= 1000)
{
    dec = 999;
}

或者以字符串解析

📂 环境信息(必填)

  • downkyiCore 版本:1.0.23
  • 操作系统:Windows 10

⚠️ 已尝试的解决方案

  • 更新至最新版本
  • 检查网络连接(尝试切换网络环境):cite[2]:cite[6]
  • 更改解析模式(API/网页解析):cite[4]
  • 更换下载器(Aria2/内建下载器)

📎 补充信息

错误格式

Image

正确格式

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions