提示词编写指南

编写高质量的提示词是获得优秀视频的关键。以下是各个要素的详细说明:

提示词结构

明确描述视频的主要对象或角色好的示例:
  • “一只橘色的小猫”
  • “穿着红色连衣裙的年轻女孩”
  • “银色的跑车”
避免:
  • “某个东西”
  • “一些动物”

优秀提示词示例

提示词增强功能

开启 enhance_prompt 可以让 AI 自动优化您的提示词,提高生成质量

何时使用提示词增强

推荐使用

  • 初次使用 API
  • 提示词较简单
  • 想要更好的效果
  • 不确定如何描述

可以关闭

  • 需要精确控制
  • 已有完善提示词
  • 特定风格要求
  • 技术性描述

参考图片使用

图片要求

要求说明
格式JPG、PNG、WebP
大小单张不超过 10MB
数量最多 5 张
分辨率建议 1024x1024 以上
内容清晰、相关的参考素材

使用技巧

1

选择高质量图片

使用清晰、高分辨率的图片作为参考
2

保持风格一致

多张图片应保持视觉风格的一致性
3

相关性优先

选择与目标视频最相关的参考图片
4

避免冲突

图片内容不应与文字描述产生冲突

轮询策略

推荐的轮询实现

import time
import math

def exponential_backoff_polling(client, task_id, initial_interval=5, max_interval=60):
    """
    指数退避轮询策略
    """
    interval = initial_interval
    attempt = 0
    
    while True:
        try:
            status_data = client.get_status(task_id)
            status = status_data.get('status')
            
            if status == 'completed':
                return status_data['result']
            elif status == 'failed':
                raise Exception(f"生成失败: {status_data.get('error')}")
            
            # 指数退避
            time.sleep(interval)
            attempt += 1
            interval = min(initial_interval * math.pow(1.5, attempt), max_interval)
            
        except Exception as e:
            print(f"轮询出错: {e}")
            time.sleep(interval)

轮询参数建议

  • 初始间隔: 5 秒
  • 最大间隔: 60 秒
  • 退避因子: 1.5
  • 最大等待: 30 分钟

错误处理

重试策略

def retry_with_backoff(func, max_retries=3, backoff_factor=2):
    """
    带退避的重试机制
    """
    for attempt in range(max_retries):
        try:
            return func()
        except Exception as e:
            if attempt == max_retries - 1:
                raise
            
            wait_time = backoff_factor ** attempt
            print(f"失败,{wait_time}秒后重试...")
            time.sleep(wait_time)

常见错误处理

try:
    result = client.submit_task(prompt)
except requests.exceptions.ConnectionError:
    print("网络连接失败,请检查网络")
except requests.exceptions.Timeout:
    print("请求超时,请稍后重试")

性能优化

批量处理

当需要生成多个视频时,建议使用批量处理:
async def batch_process_videos(prompts, max_concurrent=5):
    """
    批量处理视频生成
    """
    semaphore = asyncio.Semaphore(max_concurrent)
    
    async def process_one(prompt):
        async with semaphore:
            return await client.submit_and_wait(prompt)
    
    tasks = [process_one(prompt) for prompt in prompts]
    return await asyncio.gather(*tasks)

资源管理

注意并发限制:同时最多 10 个任务

成本优化

模型选择策略

def choose_model(requirements):
    """
    根据需求智能选择模型
    """
    if requirements.get('need_fast'):
        return 'veo3-fast'
    elif requirements.get('high_quality'):
        return 'veo3-pro'
    elif requirements.get('precise_control'):
        return 'veo3-pro-frames'
    else:
        return 'veo3'  # 默认选择标准版

测试建议

开发阶段使用 veo3veo3-fast 进行测试,生产环境根据需要选择合适的模型

监控和日志

建议的日志记录

import logging
from datetime import datetime

class VEOLogger:
    def __init__(self):
        self.logger = logging.getLogger('veo_api')
        
    def log_task_submission(self, task_id, prompt, model):
        self.logger.info(f"Task submitted: {task_id}")
        self.logger.debug(f"Prompt: {prompt[:50]}...")
        self.logger.debug(f"Model: {model}")
        
    def log_task_completion(self, task_id, duration, video_url):
        self.logger.info(f"Task completed: {task_id}")
        self.logger.info(f"Duration: {duration}s")
        self.logger.debug(f"Video URL: {video_url}")

监控指标

  • 任务成功率
  • 平均生成时间
  • API 响应时间
  • 错误率统计
  • 费用追踪