Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/biliass/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod error;
mod proto;
mod python;
mod reader;
mod writer;

use error::BiliassError;
use pyo3::exceptions::PyValueError;
Expand All @@ -24,5 +25,6 @@ fn biliass_pyo3(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<python::PyCommentPosition>()?;
m.add_function(wrap_pyfunction!(python::py_read_comments_from_xml, m)?)?;
m.add_function(wrap_pyfunction!(python::py_read_comments_from_protobuf, m)?)?;
m.add_function(wrap_pyfunction!(python::py_convert_timestamp, m)?)?;
Ok(())
}
2 changes: 2 additions & 0 deletions packages/biliass/rust/src/python/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod comment;
mod proto;
mod reader;
mod writer;

pub use comment::{PyComment, PyCommentPosition};
pub use proto::{PyDanmakuElem, PyDmSegMobileReply};
pub use reader::{py_read_comments_from_protobuf, py_read_comments_from_xml};
pub use writer::py_convert_timestamp;
8 changes: 8 additions & 0 deletions packages/biliass/rust/src/python/writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::writer;

use pyo3::prelude::*;

#[pyfunction(name = "convert_timestamp")]
pub fn py_convert_timestamp(timestamp: f64) -> PyResult<String> {
Ok(writer::utils::convert_timestamp(timestamp))
}
1 change: 1 addition & 0 deletions packages/biliass/rust/src/writer/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod utils;
16 changes: 16 additions & 0 deletions packages/biliass/rust/src/writer/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn divmod(a: f64, b: f64) -> (f64, f64) {
(a / b, a % b)
}

pub fn convert_timestamp(timestamp: f64) -> String {
let timestamp = (timestamp * 100.0).round();
let (hour, minute) = divmod(timestamp, 360000.0);
let (minute, second) = divmod(minute, 6000.0);
let (second, centsecond) = divmod(second, 100.0);
let hour = hour as u32;
let minute = minute as u32;
let second = second as u32;
let centsecond = centsecond as u32;

format!("{}:{:02}:{:02}.{:02}", hour, minute, second, centsecond)
}
1 change: 1 addition & 0 deletions packages/biliass/src/biliass/_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ class Comment:

def read_comments_from_xml(text: str, fontsize: float) -> list[Comment]: ...
def read_comments_from_protobuf(data: bytes, fontsize: float) -> list[Comment]: ...
def convert_timestamp(timestamp: int) -> float: ...
18 changes: 8 additions & 10 deletions packages/biliass/src/biliass/biliass.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
import math
import random
import re
from typing import TYPE_CHECKING, NamedTuple, TypeVar
from typing import TYPE_CHECKING, TypeVar

from biliass._core import Comment, CommentPosition, read_comments_from_protobuf, read_comments_from_xml
from biliass._core import (
Comment,
CommentPosition,
convert_timestamp,
read_comments_from_protobuf,
read_comments_from_xml,
)

if TYPE_CHECKING:
from collections.abc import Callable
Expand Down Expand Up @@ -477,14 +483,6 @@ def replace_leading_space(s):
)


def convert_timestamp(timestamp):
timestamp = round(timestamp * 100.0)
hour, minute = divmod(timestamp, 360000)
minute, second = divmod(minute, 6000)
second, centsecond = divmod(second, 100)
return "%d:%02d:%02d.%02d" % (int(hour), int(minute), int(second), int(centsecond))


def convert_color(RGB, width=1280, height=576):
if RGB == 0x000000:
return "000000"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_biliass/test_corpus