94 lines
3.3 KiB
Python
94 lines
3.3 KiB
Python
from typing import Union, Any
|
|
from pydantic import BaseModel, Field, computed_field, field_validator, model_validator
|
|
from pydantic.json_schema import JsonSchemaValue
|
|
from ..utils.TimeUtils import TimeDelta
|
|
|
|
|
|
class FFMpegSliceSegment(BaseModel):
|
|
start: TimeDelta = Field(
|
|
description="视频切割的开始时间点秒数, 可为浮点小数(精确到小数点后3位,毫秒级)或者为标准格式的时间戳")
|
|
end: TimeDelta = Field(
|
|
description="视频切割的结束时间点秒数, 可为浮点小数(精确到小数点后3位,毫秒级)或者标准格式的时间戳")
|
|
|
|
@computed_field
|
|
@property
|
|
def duration(self) -> TimeDelta:
|
|
return self.end - self.start
|
|
|
|
@field_validator('start', mode='before')
|
|
@classmethod
|
|
def parse_start(cls, v: Union[float, str, TimeDelta]):
|
|
if isinstance(v, float):
|
|
if v < 0.0:
|
|
raise ValueError("开始时间点不能小于0")
|
|
return TimeDelta(seconds=v)
|
|
elif isinstance(v, int):
|
|
if v < 0:
|
|
raise ValueError("开始时间点不能小于0")
|
|
return TimeDelta(seconds=v)
|
|
elif isinstance(v, str):
|
|
timedelta = TimeDelta.from_format_string(v)
|
|
if timedelta.total_seconds() < 0.0:
|
|
raise ValueError("开始时间点不能小于0")
|
|
return timedelta
|
|
elif isinstance(v, TimeDelta):
|
|
if v.total_seconds() < 0.0:
|
|
raise ValueError("开始时间点不能小于0")
|
|
return v
|
|
else:
|
|
raise TypeError(v)
|
|
|
|
@field_validator('end', mode='before')
|
|
@classmethod
|
|
def parse_end(cls, v: Union[float, TimeDelta]):
|
|
if isinstance(v, float):
|
|
if v < 0.0:
|
|
raise ValueError("结束时间点不能小于0")
|
|
return TimeDelta(seconds=v)
|
|
elif isinstance(v, int):
|
|
if v < 0:
|
|
raise ValueError("结束时间点不能小于0")
|
|
return TimeDelta(seconds=v)
|
|
elif isinstance(v, str):
|
|
timedelta = TimeDelta.from_format_string(v)
|
|
if timedelta.total_seconds() < 0.0:
|
|
raise ValueError("结束时间点不能小于0")
|
|
return timedelta
|
|
elif isinstance(v, TimeDelta):
|
|
if v.total_seconds() < 0.0:
|
|
raise ValueError("结束时间点不能小于0")
|
|
return v
|
|
else:
|
|
raise TypeError(v)
|
|
|
|
@model_validator(mode='after')
|
|
def validate_end_after_start(self) -> 'FFMpegSliceSegment':
|
|
if self.end <= self.start:
|
|
raise ValueError("end time must be greater than start time")
|
|
return self
|
|
|
|
@classmethod
|
|
def __get_pydantic_json_schema__(cls, core_schema: Any, handler: Any) -> JsonSchemaValue:
|
|
# Override the schema to represent it as a string
|
|
return {
|
|
"type": "object",
|
|
"properties": {
|
|
"start": {
|
|
"type": "number",
|
|
"examples": [5, 10.5, '00:00:10.500'],
|
|
},
|
|
"end": {
|
|
"type": "number",
|
|
"examples": [8, 12.5, '00:00:12.500'],
|
|
}
|
|
},
|
|
"required": [
|
|
"start",
|
|
"end"
|
|
]
|
|
}
|
|
|
|
model_config = {
|
|
"arbitrary_types_allowed": True
|
|
}
|