-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo.go
More file actions
279 lines (251 loc) · 10.1 KB
/
video.go
File metadata and controls
279 lines (251 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package together
import (
"context"
"errors"
"fmt"
"net/http"
"slices"
"github.com/togethercomputer/together-go/internal/apijson"
"github.com/togethercomputer/together-go/internal/requestconfig"
"github.com/togethercomputer/together-go/option"
"github.com/togethercomputer/together-go/packages/param"
"github.com/togethercomputer/together-go/packages/respjson"
)
// VideoService contains methods and other services that help with interacting with
// the together API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewVideoService] method instead.
type VideoService struct {
Options []option.RequestOption
}
// NewVideoService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewVideoService(opts ...option.RequestOption) (r VideoService) {
r = VideoService{}
r.Options = opts
return
}
// Create a video
func (r *VideoService) New(ctx context.Context, body VideoNewParams, opts ...option.RequestOption) (res *VideoJob, err error) {
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithBaseURL("https://api.together.xyz/v2/")}, opts...)
path := "videos"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Fetch video metadata
func (r *VideoService) Get(ctx context.Context, id string, opts ...option.RequestOption) (res *VideoJob, err error) {
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithBaseURL("https://api.together.xyz/v2/")}, opts...)
if id == "" {
err = errors.New("missing required id parameter")
return
}
path := fmt.Sprintf("videos/%s", id)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// Structured information describing a generated video job.
type VideoJob struct {
// Unique identifier for the video job.
ID string `json:"id,required"`
// Unix timestamp (seconds) for when the job was created.
CreatedAt float64 `json:"created_at,required"`
// The video generation model that produced the job.
Model string `json:"model,required"`
// Duration of the generated clip in seconds.
Seconds string `json:"seconds,required"`
// The resolution of the generated video.
Size string `json:"size,required"`
// Current lifecycle status of the video job.
//
// Any of "in_progress", "completed", "failed".
Status VideoJobStatus `json:"status,required"`
// Unix timestamp (seconds) for when the job completed, if finished.
CompletedAt float64 `json:"completed_at"`
// Error payload that explains why generation failed, if applicable.
Error VideoJobError `json:"error"`
// The object type, which is always video.
//
// Any of "video".
Object VideoJobObject `json:"object"`
// Available upon completion, the outputs provides the cost charged and the hosted
// url to access the video
Outputs VideoJobOutputs `json:"outputs"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
CreatedAt respjson.Field
Model respjson.Field
Seconds respjson.Field
Size respjson.Field
Status respjson.Field
CompletedAt respjson.Field
Error respjson.Field
Object respjson.Field
Outputs respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r VideoJob) RawJSON() string { return r.JSON.raw }
func (r *VideoJob) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Current lifecycle status of the video job.
type VideoJobStatus string
const (
VideoJobStatusInProgress VideoJobStatus = "in_progress"
VideoJobStatusCompleted VideoJobStatus = "completed"
VideoJobStatusFailed VideoJobStatus = "failed"
)
// Error payload that explains why generation failed, if applicable.
type VideoJobError struct {
Message string `json:"message,required"`
Code string `json:"code"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Message respjson.Field
Code respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r VideoJobError) RawJSON() string { return r.JSON.raw }
func (r *VideoJobError) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The object type, which is always video.
type VideoJobObject string
const (
VideoJobObjectVideo VideoJobObject = "video"
)
// Available upon completion, the outputs provides the cost charged and the hosted
// url to access the video
type VideoJobOutputs struct {
// The cost of generated video charged to the owners account.
Cost int64 `json:"cost,required"`
// URL hosting the generated video
VideoURL string `json:"video_url,required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Cost respjson.Field
VideoURL respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r VideoJobOutputs) RawJSON() string { return r.JSON.raw }
func (r *VideoJobOutputs) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type VideoNewParams struct {
// The model to be used for the video creation request.
Model string `json:"model,required"`
// Frames per second. Defaults to 24.
Fps param.Opt[int64] `json:"fps,omitzero"`
// Controls how closely the video generation follows your prompt. Higher values
// make the model adhere more strictly to your text description, while lower values
// allow more creative freedom. guidence_scale affects both visual content and
// temporal consistency.Recommended range is 6.0-10.0 for most video models. Values
// above 12 may cause over-guidance artifacts or unnatural motion patterns.
GuidanceScale param.Opt[int64] `json:"guidance_scale,omitzero"`
Height param.Opt[int64] `json:"height,omitzero"`
// Similar to prompt, but specifies what to avoid instead of what to include
NegativePrompt param.Opt[string] `json:"negative_prompt,omitzero"`
// Compression quality. Defaults to 20.
OutputQuality param.Opt[int64] `json:"output_quality,omitzero"`
// Text prompt that describes the video to generate.
Prompt param.Opt[string] `json:"prompt,omitzero"`
// Clip duration in seconds.
Seconds param.Opt[string] `json:"seconds,omitzero"`
// Seed to use in initializing the video generation. Using the same seed allows
// deterministic video generation. If not provided a random seed is generated for
// each request.
Seed param.Opt[int64] `json:"seed,omitzero"`
// The number of denoising steps the model performs during video generation. More
// steps typically result in higher quality output but require longer processing
// time.
Steps param.Opt[int64] `json:"steps,omitzero"`
Width param.Opt[int64] `json:"width,omitzero"`
// Array of images to guide video generation, similar to keyframes.
FrameImages []VideoNewParamsFrameImage `json:"frame_images,omitzero"`
// Specifies the format of the output video. Defaults to MP4.
//
// Any of "MP4", "WEBM".
OutputFormat VideoNewParamsOutputFormat `json:"output_format,omitzero"`
// Unlike frame_images which constrain specific timeline positions, reference
// images guide the general appearance that should appear consistently across the
// video.
ReferenceImages []string `json:"reference_images,omitzero"`
paramObj
}
func (r VideoNewParams) MarshalJSON() (data []byte, err error) {
type shadow VideoNewParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *VideoNewParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The property InputImage is required.
type VideoNewParamsFrameImage struct {
// URL path to hosted image that is used for a frame
InputImage string `json:"input_image,required"`
// Optional param to specify where to insert the frame. If this is omitted, the
// following heuristics are applied:
//
// - frame_images size is one, frame is first.
// - If size is two, frames are first and last.
// - If size is larger, frames are first, last and evenly spaced between.
Frame VideoNewParamsFrameImageFrameUnion `json:"frame,omitzero"`
paramObj
}
func (r VideoNewParamsFrameImage) MarshalJSON() (data []byte, err error) {
type shadow VideoNewParamsFrameImage
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *VideoNewParamsFrameImage) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
type VideoNewParamsFrameImageFrameUnion struct {
OfFloat param.Opt[float64] `json:",omitzero,inline"`
// Check if union is this variant with
// !param.IsOmitted(union.OfVideoNewsFrameImageFrameString)
OfVideoNewsFrameImageFrameString param.Opt[string] `json:",omitzero,inline"`
paramUnion
}
func (u VideoNewParamsFrameImageFrameUnion) MarshalJSON() ([]byte, error) {
return param.MarshalUnion(u, u.OfFloat, u.OfVideoNewsFrameImageFrameString)
}
func (u *VideoNewParamsFrameImageFrameUnion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, u)
}
func (u *VideoNewParamsFrameImageFrameUnion) asAny() any {
if !param.IsOmitted(u.OfFloat) {
return &u.OfFloat.Value
} else if !param.IsOmitted(u.OfVideoNewsFrameImageFrameString) {
return &u.OfVideoNewsFrameImageFrameString
}
return nil
}
type VideoNewParamsFrameImageFrameString string
const (
VideoNewParamsFrameImageFrameStringFirst VideoNewParamsFrameImageFrameString = "first"
VideoNewParamsFrameImageFrameStringLast VideoNewParamsFrameImageFrameString = "last"
)
// Specifies the format of the output video. Defaults to MP4.
type VideoNewParamsOutputFormat string
const (
VideoNewParamsOutputFormatMP4 VideoNewParamsOutputFormat = "MP4"
VideoNewParamsOutputFormatWebm VideoNewParamsOutputFormat = "WEBM"
)