sora

package
v1.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 24 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultSoraClientID = "app_1LOVEceTvrP2tHFDNnrPLQkJ"

DefaultSoraClientID 默认的 Sora 客户端 ID

Variables

View Source
var ValidStyles = []string{
	"festive", "kakalaka", "news", "selfie", "handheld",
	"golden", "anime", "retro", "nostalgic", "comic",
}

ValidStyles 有效的视频风格列表

Functions

func ExtFromURL added in v1.2.0

func ExtFromURL(rawURL, fallback string) string

ExtFromURL 从 URL 中提取文件扩展名,默认返回 fallback

func ExtractRemixID

func ExtractRemixID(text string) string

ExtractRemixID 从文本或 URL 中提取 Remix 视频 ID 支持: https://sora.chatgpt.com/p/s_[hex32] 或直接 s_[hex32]

func ExtractStyle

func ExtractStyle(prompt string) (string, string)

ExtractStyle 从提示词中提取 {style} 风格标记 返回清理后的提示词和风格 ID(无风格时为空字符串) 示例: "一只猫 {anime}" -> ("一只猫", "anime")

func ExtractVideoID added in v1.0.1

func ExtractVideoID(text string) string

ExtractVideoID 从 Sora 分享链接或文本中提取视频 ID 支持: https://sora.chatgpt.com/p/s_xxx 或直接 s_xxx 等格式

func FormatStoryboardPrompt added in v1.0.5

func FormatStoryboardPrompt(prompt string) string

FormatStoryboardPrompt 将分镜格式的提示词转换为 API 所需格式 输入: "总体描述\n[5.0s]场景1 [5.0s]场景2" 输出: "current timeline:\nShot 1:\nduration: 5.0sec\nScene: 场景1\n..."

func IsDataURI added in v1.2.0

func IsDataURI(s string) bool

IsDataURI 判断字符串是否为 data URI(data:...;base64,...)

func IsStoryboardPrompt added in v1.0.5

func IsStoryboardPrompt(prompt string) bool

IsStoryboardPrompt 检测提示词是否为分镜模式格式 格式: [5.0s]场景描述 [5.0s]场景描述

func ParseDataURI added in v1.2.0

func ParseDataURI(dataURI string) (data []byte, ext string, err error)

ParseDataURI 解析 data URI,返回二进制数据和对应的文件扩展名 支持格式: data:image/png;base64,iVBOR... 或 data:video/mp4;base64,AAAA...

func ParseProxy

func ParseProxy(proxy string) string

ParseProxy 解析代理字符串

支持格式:

Types

type CameoStatus added in v1.0.5

type CameoStatus struct {
	ID              string // 角色 cameo ID
	Status          string // 处理状态
	DisplayNameHint string // 推荐的显示名称
	UsernameHint    string // 推荐的用户名
	ProfileAssetURL string // 角色头像 URL
}

CameoStatus 角色处理状态

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client Sora API 客户端

func New

func New(proxyURL string) (*Client, error)

New 创建客户端,proxyURL 为空则不使用代理

func (*Client) CreateImageTask

func (c *Client) CreateImageTask(ctx context.Context, accessToken, sentinelToken, prompt string, width, height int) (string, error)

CreateImageTask 创建图片生成任务(文生图) 如需图生图,先调用 UploadImage 获取 mediaID,再传入 CreateImageTaskWithImage

func (*Client) CreateImageTaskWithImage

func (c *Client) CreateImageTaskWithImage(ctx context.Context, accessToken, sentinelToken, prompt string, width, height int, mediaID string) (string, error)

CreateImageTaskWithImage 创建图生图任务 mediaID 为空表示文生图,非空表示图生图

func (*Client) CreateStoryboardTask added in v1.0.5

func (c *Client) CreateStoryboardTask(ctx context.Context, accessToken, sentinelToken, prompt, orientation string, nFrames int, mediaID, styleID string) (string, error)

CreateStoryboardTask 创建分镜视频任务 prompt 应为分镜格式(会自动调用 FormatStoryboardPrompt 转换)

func (*Client) CreateVideoTask

func (c *Client) CreateVideoTask(ctx context.Context, accessToken, sentinelToken, prompt, orientation string, nFrames int, model, size string) (string, error)

CreateVideoTask 创建视频生成任务(文生视频) 如需图生视频,先调用 UploadImage 获取 mediaID,再传入 CreateVideoTaskWithImage

func (*Client) CreateVideoTaskWithImage

func (c *Client) CreateVideoTaskWithImage(ctx context.Context, accessToken, sentinelToken, prompt, orientation string, nFrames int, model, size, mediaID string) (string, error)

CreateVideoTaskWithImage 创建图生视频任务

func (*Client) CreateVideoTaskWithOptions

func (c *Client) CreateVideoTaskWithOptions(ctx context.Context, accessToken, sentinelToken, prompt, orientation string, nFrames int, model, size, mediaID, styleID string) (string, error)

CreateVideoTaskWithOptions 创建视频任务的完整方法 mediaID 为空表示文生视频,非空表示图生视频 styleID 为空表示无风格,可选值见 ValidStyles

func (*Client) DeleteCharacter added in v1.0.5

func (c *Client) DeleteCharacter(ctx context.Context, accessToken, characterID string) error

DeleteCharacter 删除角色

func (*Client) DeletePost added in v1.0.5

func (c *Client) DeletePost(ctx context.Context, accessToken, postID string) error

DeletePost 删除已发布的帖子

func (*Client) DownloadCharacterImage added in v1.0.5

func (c *Client) DownloadCharacterImage(ctx context.Context, imageURL string) ([]byte, error)

DownloadCharacterImage 下载角色头像图片

func (*Client) DownloadFile added in v1.2.0

func (c *Client) DownloadFile(ctx context.Context, fileURL string) ([]byte, error)

DownloadFile 下载 URL 内容并返回字节数据

func (*Client) EnhancePrompt

func (c *Client) EnhancePrompt(ctx context.Context, accessToken, prompt, expansionLevel string, durationSec int) (string, error)

EnhancePrompt 使用 Sora 的提示词优化 API 增强提示词 expansionLevel: "medium" 或 "long" durationSec: 5、10、15 或 25

func (*Client) FinalizeCharacter added in v1.0.5

func (c *Client) FinalizeCharacter(ctx context.Context, accessToken, cameoID, username, displayName, profileAssetPointer string) (string, error)

FinalizeCharacter 定稿角色,返回 characterID

func (*Client) GenerateSentinelToken

func (c *Client) GenerateSentinelToken(ctx context.Context, accessToken string) (string, error)

GenerateSentinelToken 获取 sentinel token(含 PoW 计算)

func (*Client) GetCameoStatus added in v1.0.5

func (c *Client) GetCameoStatus(ctx context.Context, accessToken, cameoID string) (CameoStatus, error)

GetCameoStatus 获取角色处理状态

func (*Client) GetCreditBalance added in v1.0.4

func (c *Client) GetCreditBalance(ctx context.Context, accessToken string) (CreditBalance, error)

GetCreditBalance 获取当前账号的可用次数和配额信息

func (*Client) GetDownloadURL

func (c *Client) GetDownloadURL(ctx context.Context, accessToken, taskID string) (string, error)

GetDownloadURL 从 drafts 接口获取下载链接

func (*Client) GetGenerationID added in v1.2.0

func (c *Client) GetGenerationID(ctx context.Context, accessToken, taskID string) (string, error)

GetGenerationID 从 recent_tasks 或 drafts 接口获取任务的 generation ID 用于发布帖子时传入 PublishVideo

func (*Client) GetSubscriptionInfo added in v1.0.4

func (c *Client) GetSubscriptionInfo(ctx context.Context, accessToken string) (SubscriptionInfo, error)

GetSubscriptionInfo 获取当前账号的订阅信息(套餐类型、到期时间)

func (*Client) GetUserInfo added in v1.2.0

func (c *Client) GetUserInfo(ctx context.Context, accessToken string) (UserInfo, error)

GetUserInfo 获取当前用户信息(邮箱、名称等)

func (*Client) GetWatermarkFreeURL added in v1.0.1

func (c *Client) GetWatermarkFreeURL(ctx context.Context, accessToken, videoID string) (string, error)

GetWatermarkFreeURL 获取 Sora 视频的无水印下载链接 需要使用 RefreshAccessToken 获取的 token,普通 ChatGPT access_token 不支持 videoID 为 Sora 分享链接中的视频 ID,也可以传入完整链接(自动提取 ID)

func (*Client) PollCameoStatus added in v1.0.5

func (c *Client) PollCameoStatus(ctx context.Context, accessToken, cameoID string, pollInterval, pollTimeout time.Duration, onProgress ProgressFunc) (CameoStatus, error)

PollCameoStatus 轮询角色处理状态直到完成

func (*Client) PollImageTask

func (c *Client) PollImageTask(ctx context.Context, accessToken, taskID string, pollInterval, pollTimeout time.Duration, onProgress ProgressFunc) (string, error)

PollImageTask 轮询图片任务进度,返回图片 URL onProgress 可为 nil,非 nil 时在每次轮询后回调进度

func (*Client) PollVideoTask

func (c *Client) PollVideoTask(ctx context.Context, accessToken, taskID string, pollInterval, pollTimeout time.Duration, onProgress ProgressFunc) error

PollVideoTask 轮询视频任务进度 onProgress 可为 nil,非 nil 时在每次轮询后回调进度

func (*Client) PublishVideo added in v1.0.5

func (c *Client) PublishVideo(ctx context.Context, accessToken, sentinelToken, generationID string) (string, error)

PublishVideo 发布视频帖子,返回 postID generationID 为视频的生成 ID(格式如 gen_xxx)

func (*Client) QueryImageTaskOnce added in v1.0.3

func (c *Client) QueryImageTaskOnce(ctx context.Context, accessToken, taskID string, startTime time.Time) ImageTaskResult

QueryImageTaskOnce 单次查询图片任务状态(非阻塞,供 TUI 使用)

func (*Client) QueryVideoTaskOnce added in v1.0.3

func (c *Client) QueryVideoTaskOnce(ctx context.Context, accessToken, taskID string, startTime time.Time, maxProgress int) VideoTaskResult

QueryVideoTaskOnce 单次查询视频任务状态(非阻塞,供 TUI 使用) maxProgress 应传入之前的最大进度值,返回的结果中会包含更新后的进度

func (*Client) RefreshAccessToken added in v1.0.1

func (c *Client) RefreshAccessToken(ctx context.Context, refreshToken, clientID string) (newAccessToken, newRefreshToken string, err error)

RefreshAccessToken 使用 refresh_token 获取新的 access_token 返回新的 accessToken 和 refreshToken(OpenAI 每次刷新都会返回新的 refresh_token)

func (*Client) RemixVideo

func (c *Client) RemixVideo(ctx context.Context, accessToken, sentinelToken, remixTargetID, prompt, orientation string, nFrames int, styleID string) (string, error)

RemixVideo 基于已有视频创建 Remix 任务 remixTargetID 为 Sora 分享链接中的视频 ID,格式: s_[hex32]

func (*Client) SetCharacterPrivate added in v1.2.0

func (c *Client) SetCharacterPrivate(ctx context.Context, accessToken, cameoID string) error

SetCharacterPrivate 设置角色为私密

func (*Client) SetCharacterPublic added in v1.0.5

func (c *Client) SetCharacterPublic(ctx context.Context, accessToken, cameoID string) error

SetCharacterPublic 设置角色为公开

func (*Client) SetCharacterVisibility added in v1.2.0

func (c *Client) SetCharacterVisibility(ctx context.Context, accessToken, cameoID, visibility string) error

SetCharacterVisibility 设置角色可见性(public/private)

func (*Client) TestConnectivity added in v1.2.0

func (c *Client) TestConnectivity(ctx context.Context, targetURL string) (statusCode int, err error)

TestConnectivity 测试代理连通性,向目标 URL 发送 GET 请求,只要收到响应即视为成功

func (*Client) UploadCharacterImage added in v1.0.5

func (c *Client) UploadCharacterImage(ctx context.Context, accessToken string, imageData []byte) (string, error)

UploadCharacterImage 上传角色头像图片,返回 assetPointer imageData 为图片二进制数据(webp 格式)

func (*Client) UploadCharacterVideo added in v1.0.5

func (c *Client) UploadCharacterVideo(ctx context.Context, accessToken string, videoData []byte) (string, error)

UploadCharacterVideo 上传角色视频,返回 cameoID videoData 为视频二进制数据(mp4 格式),timestamps 默认 "0,3"

func (*Client) UploadImage

func (c *Client) UploadImage(ctx context.Context, accessToken string, imageData []byte, filename string) (string, error)

UploadImage 上传图片,返回 mediaID,用于图生图/图生视频 imageData 为图片二进制数据,filename 为文件名(如 "image.png")

type CreditBalance added in v1.0.4

type CreditBalance struct {
	RemainingCount    int  // 剩余可用次数
	RateLimitReached  bool // 是否触发速率限制
	AccessResetsInSec int  // 访问权限重置时间(秒)
}

CreditBalance 配额信息

type ImageTaskResult added in v1.0.3

type ImageTaskResult struct {
	Progress Progress
	Done     bool
	ImageURL string
	Err      error
}

ImageTaskResult 图片任务单次查询结果

type Progress

type Progress struct {
	Percent int    // 进度百分比 0-100
	Status  string // 任务状态
	Elapsed int    // 已耗时(秒)
}

Progress 任务进度信息

type ProgressFunc

type ProgressFunc func(Progress)

ProgressFunc 进度回调函数类型

type SubscriptionInfo added in v1.0.4

type SubscriptionInfo struct {
	PlanID    string // 套餐类型,例如 "chatgptplusplan"
	PlanTitle string // 套餐名称,例如 "ChatGPT Plus"
	EndTs     int64  // 订阅到期时间戳(秒)
}

SubscriptionInfo 订阅信息

type UserInfo added in v1.2.0

type UserInfo struct {
	Email string
	Name  string
}

UserInfo 用户信息

type VideoTaskResult added in v1.0.3

type VideoTaskResult struct {
	Progress Progress
	Done     bool
	Err      error
}

VideoTaskResult 视频任务单次查询结果

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL