init
This commit is contained in:
192
pkg/video/minimax_client.go
Normal file
192
pkg/video/minimax_client.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// MinimaxClient Minimax视频生成客户端
|
||||
type MinimaxClient struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
Model string
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
type MinimaxSubjectReference struct {
|
||||
Type string `json:"type"`
|
||||
Image []string `json:"image"`
|
||||
}
|
||||
|
||||
type MinimaxRequest struct {
|
||||
Prompt string `json:"prompt"`
|
||||
FirstFrameImage string `json:"first_frame_image,omitempty"`
|
||||
LastFrameImage string `json:"last_frame_image,omitempty"`
|
||||
SubjectReference []MinimaxSubjectReference `json:"subject_reference,omitempty"`
|
||||
Model string `json:"model"`
|
||||
Duration int `json:"duration,omitempty"`
|
||||
Resolution string `json:"resolution,omitempty"`
|
||||
}
|
||||
|
||||
type MinimaxResponse struct {
|
||||
TaskID string `json:"task_id"`
|
||||
Status string `json:"status"`
|
||||
BaseResp struct {
|
||||
StatusCode int `json:"status_code"`
|
||||
StatusMsg string `json:"status_msg"`
|
||||
} `json:"base_resp"`
|
||||
Video struct {
|
||||
URL string `json:"url"`
|
||||
Duration int `json:"duration"`
|
||||
} `json:"video"`
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
func NewMinimaxClient(baseURL, apiKey, model string) *MinimaxClient {
|
||||
return &MinimaxClient{
|
||||
BaseURL: baseURL,
|
||||
APIKey: apiKey,
|
||||
Model: model,
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: 300 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateVideo 生成视频(支持首尾帧和主体参考)
|
||||
func (c *MinimaxClient) GenerateVideo(imageURL, prompt string, opts ...VideoOption) (*VideoResult, error) {
|
||||
options := &VideoOptions{
|
||||
Duration: 6,
|
||||
Resolution: "1080P",
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
model := c.Model
|
||||
if options.Model != "" {
|
||||
model = options.Model
|
||||
}
|
||||
|
||||
reqBody := MinimaxRequest{
|
||||
Prompt: prompt,
|
||||
Model: model,
|
||||
Duration: options.Duration,
|
||||
}
|
||||
|
||||
// 设置分辨率
|
||||
if options.Resolution != "" {
|
||||
reqBody.Resolution = options.Resolution
|
||||
}
|
||||
|
||||
// 如果有首帧图片(从imageURL或FirstFrameURL)
|
||||
if options.FirstFrameURL != "" {
|
||||
reqBody.FirstFrameImage = options.FirstFrameURL
|
||||
} else if imageURL != "" {
|
||||
reqBody.FirstFrameImage = imageURL
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal request: %w", err)
|
||||
}
|
||||
|
||||
endpoint := c.BaseURL + "/v1/video_generation"
|
||||
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||
return nil, fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var result MinimaxResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
if result.Error.Message != "" {
|
||||
return nil, fmt.Errorf("minimax error: %s", result.Error.Message)
|
||||
}
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.TaskID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "completed",
|
||||
Duration: result.Video.Duration,
|
||||
}
|
||||
|
||||
if result.Video.URL != "" {
|
||||
videoResult.VideoURL = result.Video.URL
|
||||
videoResult.Completed = true
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
|
||||
func (c *MinimaxClient) GetTaskStatus(taskID string) (*VideoResult, error) {
|
||||
endpoint := c.BaseURL + "/v1/video_generation/" + taskID
|
||||
req, err := http.NewRequest("GET", endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
var result MinimaxResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.TaskID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "completed",
|
||||
Duration: result.Video.Duration,
|
||||
}
|
||||
|
||||
if result.Error.Message != "" {
|
||||
videoResult.Error = result.Error.Message
|
||||
}
|
||||
|
||||
if result.Video.URL != "" {
|
||||
videoResult.VideoURL = result.Video.URL
|
||||
videoResult.Completed = true
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
178
pkg/video/openai_sora_client.go
Normal file
178
pkg/video/openai_sora_client.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type OpenAISoraClient struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
Model string
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
type OpenAISoraResponse struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
Model string `json:"model"`
|
||||
Status string `json:"status"`
|
||||
Progress int `json:"progress"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
CompletedAt int64 `json:"completed_at"`
|
||||
Size string `json:"size"`
|
||||
Seconds string `json:"seconds"`
|
||||
Quality string `json:"quality"`
|
||||
VideoURL string `json:"video_url"` // 直接的video_url字段
|
||||
Video struct {
|
||||
URL string `json:"url"`
|
||||
} `json:"video"` // 嵌套的video.url字段(兼容)
|
||||
Error struct {
|
||||
Message string `json:"message"`
|
||||
Type string `json:"type"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
func NewOpenAISoraClient(baseURL, apiKey, model string) *OpenAISoraClient {
|
||||
return &OpenAISoraClient{
|
||||
BaseURL: baseURL,
|
||||
APIKey: apiKey,
|
||||
Model: model,
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: 300 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *OpenAISoraClient) GenerateVideo(imageURL, prompt string, opts ...VideoOption) (*VideoResult, error) {
|
||||
options := &VideoOptions{
|
||||
Duration: 4,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
model := c.Model
|
||||
if options.Model != "" {
|
||||
model = options.Model
|
||||
}
|
||||
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
|
||||
writer.WriteField("model", model)
|
||||
writer.WriteField("prompt", prompt)
|
||||
|
||||
if imageURL != "" {
|
||||
writer.WriteField("input_reference", imageURL)
|
||||
}
|
||||
|
||||
if options.Duration > 0 {
|
||||
writer.WriteField("seconds", fmt.Sprintf("%d", options.Duration))
|
||||
}
|
||||
|
||||
if options.Resolution != "" {
|
||||
writer.WriteField("size", options.Resolution)
|
||||
}
|
||||
|
||||
writer.Close()
|
||||
|
||||
endpoint := c.BaseURL + "/v1/videos"
|
||||
req, err := http.NewRequest("POST", endpoint, body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||
return nil, fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(respBody))
|
||||
}
|
||||
|
||||
var result OpenAISoraResponse
|
||||
if err := json.Unmarshal(respBody, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
if result.Error.Message != "" {
|
||||
return nil, fmt.Errorf("openai error: %s", result.Error.Message)
|
||||
}
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.ID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "completed",
|
||||
}
|
||||
|
||||
// 优先使用video_url字段,兼容video.url嵌套结构
|
||||
if result.VideoURL != "" {
|
||||
videoResult.VideoURL = result.VideoURL
|
||||
} else if result.Video.URL != "" {
|
||||
videoResult.VideoURL = result.Video.URL
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
|
||||
func (c *OpenAISoraClient) GetTaskStatus(taskID string) (*VideoResult, error) {
|
||||
endpoint := c.BaseURL + "/v1/videos/" + taskID
|
||||
req, err := http.NewRequest("GET", endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
var result OpenAISoraResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.ID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "completed",
|
||||
}
|
||||
|
||||
if result.Error.Message != "" {
|
||||
videoResult.Error = result.Error.Message
|
||||
}
|
||||
|
||||
// 优先使用video_url字段,兼容video.url嵌套结构
|
||||
if result.VideoURL != "" {
|
||||
videoResult.VideoURL = result.VideoURL
|
||||
} else if result.Video.URL != "" {
|
||||
videoResult.VideoURL = result.Video.URL
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
427
pkg/video/video_client.go
Normal file
427
pkg/video/video_client.go
Normal file
@@ -0,0 +1,427 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type VideoClient interface {
|
||||
GenerateVideo(imageURL, prompt string, opts ...VideoOption) (*VideoResult, error)
|
||||
GetTaskStatus(taskID string) (*VideoResult, error)
|
||||
}
|
||||
|
||||
type VideoResult struct {
|
||||
TaskID string
|
||||
Status string
|
||||
VideoURL string
|
||||
ThumbnailURL string
|
||||
Duration int
|
||||
Width int
|
||||
Height int
|
||||
Error string
|
||||
Completed bool
|
||||
}
|
||||
|
||||
type VideoOptions struct {
|
||||
Model string
|
||||
Duration int
|
||||
FPS int
|
||||
Resolution string
|
||||
AspectRatio string
|
||||
Style string
|
||||
MotionLevel int
|
||||
CameraMotion string
|
||||
Seed int64
|
||||
FirstFrameURL string
|
||||
LastFrameURL string
|
||||
ReferenceImageURLs []string
|
||||
}
|
||||
|
||||
type VideoOption func(*VideoOptions)
|
||||
|
||||
func WithModel(model string) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.Model = model
|
||||
}
|
||||
}
|
||||
|
||||
func WithDuration(duration int) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.Duration = duration
|
||||
}
|
||||
}
|
||||
|
||||
func WithFPS(fps int) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.FPS = fps
|
||||
}
|
||||
}
|
||||
|
||||
func WithResolution(resolution string) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.Resolution = resolution
|
||||
}
|
||||
}
|
||||
|
||||
func WithAspectRatio(ratio string) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.AspectRatio = ratio
|
||||
}
|
||||
}
|
||||
|
||||
func WithStyle(style string) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.Style = style
|
||||
}
|
||||
}
|
||||
|
||||
func WithMotionLevel(level int) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.MotionLevel = level
|
||||
}
|
||||
}
|
||||
|
||||
func WithCameraMotion(motion string) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.CameraMotion = motion
|
||||
}
|
||||
}
|
||||
|
||||
func WithSeed(seed int64) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.Seed = seed
|
||||
}
|
||||
}
|
||||
|
||||
func WithFirstFrame(url string) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.FirstFrameURL = url
|
||||
}
|
||||
}
|
||||
|
||||
func WithLastFrame(url string) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.LastFrameURL = url
|
||||
}
|
||||
}
|
||||
|
||||
func WithReferenceImages(urls []string) VideoOption {
|
||||
return func(o *VideoOptions) {
|
||||
o.ReferenceImageURLs = urls
|
||||
}
|
||||
}
|
||||
|
||||
type RunwayClient struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
Model string
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
type RunwayRequest struct {
|
||||
Model string `json:"model"`
|
||||
PromptImage string `json:"prompt_image"`
|
||||
PromptText string `json:"prompt_text"`
|
||||
Duration int `json:"duration,omitempty"`
|
||||
AspectRatio string `json:"aspect_ratio,omitempty"`
|
||||
Seed int64 `json:"seed,omitempty"`
|
||||
}
|
||||
|
||||
type RunwayResponse struct {
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
Output struct {
|
||||
URL string `json:"url"`
|
||||
} `json:"output"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func NewRunwayClient(baseURL, apiKey, model string) *RunwayClient {
|
||||
return &RunwayClient{
|
||||
BaseURL: baseURL,
|
||||
APIKey: apiKey,
|
||||
Model: model,
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: 180 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RunwayClient) GenerateVideo(imageURL, prompt string, opts ...VideoOption) (*VideoResult, error) {
|
||||
options := &VideoOptions{
|
||||
Duration: 5,
|
||||
AspectRatio: "16:9",
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
model := c.Model
|
||||
if options.Model != "" {
|
||||
model = options.Model
|
||||
}
|
||||
|
||||
reqBody := RunwayRequest{
|
||||
Model: model,
|
||||
PromptImage: imageURL,
|
||||
PromptText: prompt,
|
||||
Duration: options.Duration,
|
||||
AspectRatio: options.AspectRatio,
|
||||
Seed: options.Seed,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal request: %w", err)
|
||||
}
|
||||
|
||||
endpoint := c.BaseURL + "/v1/video/generate"
|
||||
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||
return nil, fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var result RunwayResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
if result.Error != "" {
|
||||
return nil, fmt.Errorf("runway error: %s", result.Error)
|
||||
}
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.ID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "succeeded",
|
||||
}
|
||||
|
||||
if result.Output.URL != "" {
|
||||
videoResult.VideoURL = result.Output.URL
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
|
||||
func (c *RunwayClient) GetTaskStatus(taskID string) (*VideoResult, error) {
|
||||
endpoint := c.BaseURL + "/v1/video/status/" + taskID
|
||||
req, err := http.NewRequest("GET", endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
var result RunwayResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.ID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "succeeded",
|
||||
}
|
||||
|
||||
if result.Error != "" {
|
||||
videoResult.Error = result.Error
|
||||
}
|
||||
|
||||
if result.Output.URL != "" {
|
||||
videoResult.VideoURL = result.Output.URL
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
|
||||
type PikaClient struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
Model string
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
type PikaRequest struct {
|
||||
Model string `json:"model"`
|
||||
Image string `json:"image"`
|
||||
Prompt string `json:"prompt"`
|
||||
Duration int `json:"duration,omitempty"`
|
||||
AspectRatio string `json:"aspect_ratio,omitempty"`
|
||||
Motion int `json:"motion,omitempty"`
|
||||
CameraMotion string `json:"camera_motion,omitempty"`
|
||||
Seed int64 `json:"seed,omitempty"`
|
||||
}
|
||||
|
||||
type PikaResponse struct {
|
||||
JobID string `json:"job_id"`
|
||||
Status string `json:"status"`
|
||||
Result struct {
|
||||
VideoURL string `json:"video_url"`
|
||||
} `json:"result"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func NewPikaClient(baseURL, apiKey, model string) *PikaClient {
|
||||
return &PikaClient{
|
||||
BaseURL: baseURL,
|
||||
APIKey: apiKey,
|
||||
Model: model,
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: 180 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *PikaClient) GenerateVideo(imageURL, prompt string, opts ...VideoOption) (*VideoResult, error) {
|
||||
options := &VideoOptions{
|
||||
Duration: 3,
|
||||
AspectRatio: "16:9",
|
||||
MotionLevel: 50,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
model := c.Model
|
||||
if options.Model != "" {
|
||||
model = options.Model
|
||||
}
|
||||
|
||||
reqBody := PikaRequest{
|
||||
Model: model,
|
||||
Image: imageURL,
|
||||
Prompt: prompt,
|
||||
Duration: options.Duration,
|
||||
AspectRatio: options.AspectRatio,
|
||||
Motion: options.MotionLevel,
|
||||
CameraMotion: options.CameraMotion,
|
||||
Seed: options.Seed,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal request: %w", err)
|
||||
}
|
||||
|
||||
endpoint := c.BaseURL + "/v1/video/generate"
|
||||
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||
return nil, fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var result PikaResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
if result.Error != "" {
|
||||
return nil, fmt.Errorf("pika error: %s", result.Error)
|
||||
}
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.JobID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "completed",
|
||||
}
|
||||
|
||||
if result.Result.VideoURL != "" {
|
||||
videoResult.VideoURL = result.Result.VideoURL
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
|
||||
func (c *PikaClient) GetTaskStatus(taskID string) (*VideoResult, error) {
|
||||
endpoint := c.BaseURL + "/v1/video/status/" + taskID
|
||||
req, err := http.NewRequest("GET", endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
var result PikaResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.JobID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "completed",
|
||||
}
|
||||
|
||||
if result.Error != "" {
|
||||
videoResult.Error = result.Error
|
||||
}
|
||||
|
||||
if result.Result.VideoURL != "" {
|
||||
videoResult.VideoURL = result.Result.VideoURL
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
288
pkg/video/volces_ark_client.go
Normal file
288
pkg/video/volces_ark_client.go
Normal file
@@ -0,0 +1,288 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// VolcesArkClient 火山引擎ARK视频生成客户端
|
||||
type VolcesArkClient struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
Model string
|
||||
Endpoint string
|
||||
QueryEndpoint string
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
type VolcesArkContent struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text,omitempty"`
|
||||
ImageURL map[string]interface{} `json:"image_url,omitempty"`
|
||||
Role string `json:"role,omitempty"`
|
||||
}
|
||||
|
||||
type VolcesArkRequest struct {
|
||||
Model string `json:"model"`
|
||||
Content []VolcesArkContent `json:"content"`
|
||||
GenerateAudio bool `json:"generate_audio,omitempty"`
|
||||
}
|
||||
|
||||
type VolcesArkResponse struct {
|
||||
ID string `json:"id"`
|
||||
Model string `json:"model"`
|
||||
Status string `json:"status"`
|
||||
Content struct {
|
||||
VideoURL string `json:"video_url"`
|
||||
} `json:"content"`
|
||||
Usage struct {
|
||||
CompletionTokens int `json:"completion_tokens"`
|
||||
TotalTokens int `json:"total_tokens"`
|
||||
} `json:"usage"`
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
Seed int `json:"seed"`
|
||||
Resolution string `json:"resolution"`
|
||||
Ratio string `json:"ratio"`
|
||||
Duration int `json:"duration"`
|
||||
FramesPerSecond int `json:"framespersecond"`
|
||||
ServiceTier string `json:"service_tier"`
|
||||
ExecutionExpiresAfter int `json:"execution_expires_after"`
|
||||
GenerateAudio bool `json:"generate_audio"`
|
||||
Error interface{} `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func NewVolcesArkClient(baseURL, apiKey, model, endpoint, queryEndpoint string) *VolcesArkClient {
|
||||
if endpoint == "" {
|
||||
endpoint = "/api/v3/contents/generations/tasks"
|
||||
}
|
||||
if queryEndpoint == "" {
|
||||
queryEndpoint = endpoint
|
||||
}
|
||||
return &VolcesArkClient{
|
||||
BaseURL: baseURL,
|
||||
APIKey: apiKey,
|
||||
Model: model,
|
||||
Endpoint: endpoint,
|
||||
QueryEndpoint: queryEndpoint,
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: 300 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateVideo 生成视频(支持首帧、首尾帧、参考图等多种模式)
|
||||
func (c *VolcesArkClient) GenerateVideo(imageURL, prompt string, opts ...VideoOption) (*VideoResult, error) {
|
||||
options := &VideoOptions{
|
||||
Duration: 5,
|
||||
AspectRatio: "adaptive",
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
|
||||
model := c.Model
|
||||
if options.Model != "" {
|
||||
model = options.Model
|
||||
}
|
||||
|
||||
// 构建prompt文本(包含duration和ratio参数)
|
||||
promptText := prompt
|
||||
if options.AspectRatio != "" {
|
||||
promptText += fmt.Sprintf(" --ratio %s", options.AspectRatio)
|
||||
}
|
||||
if options.Duration > 0 {
|
||||
promptText += fmt.Sprintf(" --dur %d", options.Duration)
|
||||
}
|
||||
|
||||
content := []VolcesArkContent{
|
||||
{
|
||||
Type: "text",
|
||||
Text: promptText,
|
||||
},
|
||||
}
|
||||
|
||||
// 处理不同的图片模式
|
||||
// 1. 组图模式(多个reference_image)
|
||||
if len(options.ReferenceImageURLs) > 0 {
|
||||
for _, refURL := range options.ReferenceImageURLs {
|
||||
content = append(content, VolcesArkContent{
|
||||
Type: "image_url",
|
||||
ImageURL: map[string]interface{}{
|
||||
"url": refURL,
|
||||
},
|
||||
Role: "reference_image",
|
||||
})
|
||||
}
|
||||
} else if options.FirstFrameURL != "" && options.LastFrameURL != "" {
|
||||
// 2. 首尾帧模式
|
||||
content = append(content, VolcesArkContent{
|
||||
Type: "image_url",
|
||||
ImageURL: map[string]interface{}{
|
||||
"url": options.FirstFrameURL,
|
||||
},
|
||||
Role: "first_frame",
|
||||
})
|
||||
content = append(content, VolcesArkContent{
|
||||
Type: "image_url",
|
||||
ImageURL: map[string]interface{}{
|
||||
"url": options.LastFrameURL,
|
||||
},
|
||||
Role: "last_frame",
|
||||
})
|
||||
} else if imageURL != "" {
|
||||
// 3. 单图模式(默认)
|
||||
content = append(content, VolcesArkContent{
|
||||
Type: "image_url",
|
||||
ImageURL: map[string]interface{}{
|
||||
"url": imageURL,
|
||||
},
|
||||
// 单图模式不需要role
|
||||
})
|
||||
} else if options.FirstFrameURL != "" {
|
||||
// 4. 只有首帧
|
||||
content = append(content, VolcesArkContent{
|
||||
Type: "image_url",
|
||||
ImageURL: map[string]interface{}{
|
||||
"url": options.FirstFrameURL,
|
||||
},
|
||||
Role: "first_frame",
|
||||
})
|
||||
}
|
||||
|
||||
// 只有 seedance-1-5-pro 模型支持 generate_audio 参数
|
||||
generateAudio := false
|
||||
if strings.Contains(strings.ToLower(model), "seedance-1-5-pro") {
|
||||
generateAudio = true
|
||||
}
|
||||
|
||||
reqBody := VolcesArkRequest{
|
||||
Model: model,
|
||||
Content: content,
|
||||
GenerateAudio: generateAudio,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal request: %w", err)
|
||||
}
|
||||
|
||||
endpoint := c.BaseURL + c.Endpoint
|
||||
fmt.Printf("[VolcesARK] Generating video - Endpoint: %s, FullURL: %s, Model: %s\n", c.Endpoint, endpoint, model)
|
||||
fmt.Printf("[VolcesARK] Request body: %s\n", string(jsonData))
|
||||
|
||||
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("[VolcesARK] Response status: %d, body: %s\n", resp.StatusCode, string(body))
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
|
||||
return nil, fmt.Errorf("API error (status %d): %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var result VolcesArkResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("[VolcesARK] Video generation initiated - TaskID: %s, Status: %s\n", result.ID, result.Status)
|
||||
|
||||
if result.Error != nil {
|
||||
errorMsg := fmt.Sprintf("%v", result.Error)
|
||||
return nil, fmt.Errorf("volces error: %s", errorMsg)
|
||||
}
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.ID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "completed" || result.Status == "succeeded",
|
||||
Duration: result.Duration,
|
||||
}
|
||||
|
||||
if result.Content.VideoURL != "" {
|
||||
videoResult.VideoURL = result.Content.VideoURL
|
||||
videoResult.Completed = true
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
|
||||
func (c *VolcesArkClient) GetTaskStatus(taskID string) (*VideoResult, error) {
|
||||
// 替换占位符{taskId}或直接拼接
|
||||
queryPath := c.QueryEndpoint
|
||||
if contains := bytes.Contains([]byte(queryPath), []byte("{taskId}")); contains {
|
||||
queryPath = string(bytes.ReplaceAll([]byte(queryPath), []byte("{taskId}"), []byte(taskID)))
|
||||
} else {
|
||||
queryPath = queryPath + "/" + taskID
|
||||
}
|
||||
|
||||
endpoint := c.BaseURL + queryPath
|
||||
fmt.Printf("[VolcesARK] Querying task status - TaskID: %s, QueryEndpoint: %s, FullURL: %s\n", taskID, c.QueryEndpoint, endpoint)
|
||||
|
||||
req, err := http.NewRequest("GET", endpoint, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
||||
|
||||
resp, err := c.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("send request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("[VolcesARK] Response body: %s\n", string(body))
|
||||
|
||||
var result VolcesArkResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse response: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("[VolcesARK] Parsed result - ID: %s, Status: %s, VideoURL: %s\n", result.ID, result.Status, result.Content.VideoURL)
|
||||
|
||||
videoResult := &VideoResult{
|
||||
TaskID: result.ID,
|
||||
Status: result.Status,
|
||||
Completed: result.Status == "completed" || result.Status == "succeeded",
|
||||
Duration: result.Duration,
|
||||
}
|
||||
|
||||
if result.Error != nil {
|
||||
videoResult.Error = fmt.Sprintf("%v", result.Error)
|
||||
}
|
||||
|
||||
if result.Content.VideoURL != "" {
|
||||
videoResult.VideoURL = result.Content.VideoURL
|
||||
videoResult.Completed = true
|
||||
}
|
||||
|
||||
return videoResult, nil
|
||||
}
|
||||
Reference in New Issue
Block a user