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
31 changes: 29 additions & 2 deletions astrbot/core/message/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,22 @@ async def convert_to_file_path(self) -> str:
pass
raise Exception(f"not a valid file: {file_source}")

async def convert_to_base64(self) -> str:
"""将视频统一转换为 base64 编码。

Returns:
str: 视频的 base64 编码,不以 base64:// 开头。

"""
file_source = await self._resolve_file_source()
if not file_source:
raise Exception(f"not a valid file: {self.file}")
return await MediaResolver(
file_source,
media_type="video",
default_suffix=".mp4",
).to_base64()

async def register_to_file_service(self) -> str:
"""将视频注册到文件服务。

Expand Down Expand Up @@ -793,8 +809,7 @@ def __init__(self, content: list[BaseMessageComponent], **_) -> None:
async def to_dict(self) -> dict:
data_content = []
for comp in self.content:
if isinstance(comp, Image | Record):
# For Image and Record segments, we convert them to base64
if isinstance(comp, Image | Record | Video | File):
bs64 = await comp.convert_to_base64()
data_content.append(
{
Expand Down Expand Up @@ -1043,6 +1058,18 @@ async def get_file(self, allow_return_url: bool = False) -> str:

return ""

async def convert_to_base64(self) -> str:
"""将文件统一转换为 base64 编码。

Returns:
str: 文件的 base64 编码,不以 base64:// 开头。

"""
file_source = await self.get_file(allow_return_url=True)
if not file_source:
raise Exception(f"not a valid file: {self.file_ or self.url}")
return await MediaResolver(file_source, media_type="file").to_base64()

async def _download_file(self) -> None:
"""下载文件"""
if not self.url:
Expand Down
2 changes: 1 addition & 1 deletion astrbot/core/platform/message_capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
media=_MEDIA,
mixed_parts=False,
quote=False,
forward=True,
forward=False,
mention=True,
native_namespaces=frozenset({"webchat"}),
),
Expand Down
Loading
Loading