diff --git a/modelscope/hub/utils/aigc.py b/modelscope/hub/utils/aigc.py index d7587e8f..62df6cf8 100644 --- a/modelscope/hub/utils/aigc.py +++ b/modelscope/hub/utils/aigc.py @@ -8,7 +8,7 @@ import requests from tqdm.auto import tqdm from modelscope.hub.utils.utils import (MODELSCOPE_URL_SCHEME, - encode_image_to_base64, get_endpoint) + encode_media_to_base64, get_endpoint) from modelscope.utils.logger import get_logger logger = get_logger() @@ -117,7 +117,7 @@ class AigcModel: or img.startswith('data:')): try: # Convert local path to base64 data URL - processed_img = encode_image_to_base64(img) + processed_img = encode_media_to_base64(img) processed_cover_images.append(processed_img) logger.info('Converted local image to base64: %s', os.path.basename(img)) diff --git a/modelscope/hub/utils/utils.py b/modelscope/hub/utils/utils.py index d32f9ce0..161f866b 100644 --- a/modelscope/hub/utils/utils.py +++ b/modelscope/hub/utils/utils.py @@ -381,40 +381,40 @@ def convert_timestamp(time_stamp: Union[int, str, datetime], ) -def encode_image_to_base64(image_path: str) -> str: +def encode_media_to_base64(media_file_path: str) -> str: """ - Encode image file to base64 string. + Encode image or video file to base64 string. Args: - image_path (str): Path to the image file + media_file_path (str): Path to the image or video file Returns: str: Base64 encoded string with data URL prefix Raises: - FileNotFoundError: If image file doesn't exist - ValueError: If file is not a valid image format + FileNotFoundError: If image/video file doesn't exist + ValueError: If file is not a valid format """ import base64 import mimetypes # Expand user path - image_path = os.path.expanduser(image_path) + media_file_path = os.path.expanduser(media_file_path) - if not os.path.exists(image_path): - raise FileNotFoundError(f'Image file not found: {image_path}') + if not os.path.exists(media_file_path): + raise FileNotFoundError(f'Image file not found: {media_file_path}') - if not os.path.isfile(image_path): - raise ValueError(f'Path is not a file: {image_path}') + if not os.path.isfile(media_file_path): + raise ValueError(f'Path is not a file: {media_file_path}') # Get MIME type - mime_type, _ = mimetypes.guess_type(image_path) - if not mime_type or not mime_type.startswith('image/'): - raise ValueError(f'File is not a valid image format: {image_path}') + mime_type, _ = mimetypes.guess_type(media_file_path) + if not mime_type: + raise ValueError(f'File is not a valid format: {media_file_path}') # Read and encode file - with open(image_path, 'rb') as image_file: - image_data = image_file.read() + with open(media_file_path, 'rb') as media_file: + image_data = media_file.read() base64_data = base64.b64encode(image_data).decode('utf-8') # Return data URL format