forked from zhangshuai/douyin-go
/
video.go
375 lines (325 loc) · 14.2 KB
/
video.go
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package douyingo
import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"github.com/CriarBrand/douyin-go/conf"
)
const (
defaultWorkers = 4 // 默认的并发上传的块数量
defaultChunkSize = 20 * 1024 * 1024 // 默认的分片大小,20MB
)
// VideoListReq 视频列表请求
type VideoListReq struct {
OpenId string // 通过/oauth/access_token/获取,用户唯一标志
AccessToken string // 调用/oauth/access_token/生成的token,此token需要用户授权。
Cursor int64 // 分页游标, 第一页请求cursor是0, response中会返回下一页请求用到的cursor, 同时response还会返回has_more来表明是否有更多的数据。
Count int64 // 每页数量
}
// Statistics 视频列表
type Statistics struct {
ShareCount int `json:"share_count"` // 分享数
CommentCount int `json:"comment_count"` // 评论数
DiggCount int `json:"digg_count"` // 点赞数
DownloadCount int `json:"download_count"` // 下载数
ForwardCount int `json:"forward_count"` // 转发数
PlayCount int `json:"play_count"` // 播放数,只有作者本人可见。公开视频设为私密后,播放数也会返回0。
}
// Video 视频列表
type Video struct {
Cover string `json:"cover"` // 视频封面
Statistics Statistics `json:"statistics"` // 统计数据
Title string `json:"title"` // 视频标题
CreateTime int64 `json:"create_time"` // 视频创建时间戳
IsReviewed bool `json:"is_reviewed"` // 表示是否审核结束。审核通过或者失败都会返回true,审核中返回false。
IsTop bool `json:"is_top"` // 是否置顶
ItemId string `json:"item_id"` // 视频id
ShareUrl string `json:"share_url"` // 视频播放页面。视频播放页可能会失效,请在观看视频前调用/video/data/获取最新的播放页。
VideoStatus int `json:"video_status"` // 表示视频状态。1:已发布;2:不适宜公开;4:审核中;5:公开视频;6:好友可见;7:私密视频
MediaType int `json:"media_type"` // 媒体类型。2:图集;4:视频
}
// VideoListResData 视频列表
type VideoListResData struct {
List []Video `json:"list"` // 视频列表
Cursor int64 `json:"cursor"` // 用于下一页请求的cursor
HasMore bool `json:"has_more"` // 更多数据
DYError
}
// VideoListRes 视频列表
type VideoListRes struct {
Data VideoListResData `json:"data"`
Extra DYExtra `json:"extra"`
}
// VideoList 查询授权账号视频数据
func (m *Manager) VideoList(req VideoListReq) (res VideoListRes, err error) {
err = m.client.CallWithJson(context.Background(), &res, "GET", m.url("%s?access_token=%s&open_id=%s&cursor=%d&count=%d", conf.API_VIDEO_LIST, req.AccessToken, req.OpenId, req.Cursor, req.Count), nil, nil)
return res, err
}
// VideoUploadReq 上传视频到文件服务器
type VideoUploadReq struct {
OpenId string // 通过/oauth/access_token/获取,用户唯一标志
AccessToken string // 调用/oauth/access_token/生成的token,此token需要用户授权。
FilePath string // 文件路径
}
// VideoUploadResVideo 上传视频到文件服务器
type VideoUploadResVideo struct {
Height int64 `json:"height"` // 视频高度
Width int64 `json:"width"` // 视频宽度
VideoId string `json:"video_id"` // 视频id
}
// VideoUploadResData 上传视频到文件服务器
type VideoUploadResData struct {
Video VideoUploadResVideo `json:"video,omitempty"`
DYError
}
// VideoUploadRes 上传视频到文件服务器
type VideoUploadRes struct {
Data VideoUploadResData `json:"data"`
Extra DYExtra `json:"extra"`
}
// VideoUpload 上传视频到文件服务器
func (m *Manager) VideoUpload(req VideoUploadReq) (res *VideoUploadRes, err error) {
f, err := os.Open(req.FilePath)
if err != nil {
return nil, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return
}
fsize := fi.Size()
fname := fi.Name()
var b bytes.Buffer
writer := multipart.NewWriter(&b)
head := make(textproto.MIMEHeader)
extension := filepath.Ext(req.FilePath)
head.Set("Content-Type", "video/"+strings.Replace(extension, ".", "", -1))
head.Set("Content-Disposition", fmt.Sprintf(`form-data; name="video"; filename="%s"`, fname))
if _, err := writer.CreatePart(head); err != nil {
return nil, err
}
lastLine := fmt.Sprintf("\r\n--%s--\r\n", writer.Boundary())
r := strings.NewReader(lastLine)
bodyLen := int64(b.Len()) + fsize + int64(len(lastLine))
mr := io.MultiReader(&b, f, r)
contentType := writer.FormDataContentType()
headers := http.Header{}
headers.Add("Content-Type", contentType)
err = m.client.CallWith64(context.Background(), &res, "POST", m.url("%s?access_token=%s&open_id=%s", conf.API_VIDEO_UPLOAD, req.AccessToken, req.OpenId), headers, mr, bodyLen)
return res, err
}
// VideoCreateReq 创建抖音视频请求
type VideoCreateReq struct {
OpenId string // 通过/oauth/access_token/获取,用户唯一标志
AccessToken string // 调用/oauth/access_token/生成的token,此token需要用户授权。
Body VideoCreateBody // 创建视频body
}
// VideoCreateBody 创建抖音视频
type VideoCreateBody struct {
TimelinessLabel int64 `json:"timeliness_label,omitempty"` // 时效新闻标签,1表示使用。暂不开放
ArticlId string `json:"article_id,omitempty"` // 文章ID,暂不开放
MicroAppId string `json:"micro_app_id,omitempty"` // 小程序id
PoiId string `json:"poi_id,omitempty"` // 地理位置id
MicroAppUrl string `json:"micro_app_url,omitempty"` // 吊起小程序时的参数
ArticleTitle string `json:"article_title,omitempty"` // 文章自定义标题,暂不开放
CoverTsp float64 `json:"cover_tsp,omitempty"` // 将传入的指定时间点对应帧设置为视频封面(单位:秒)
GameId string `json:"game_id,omitempty"` // 游戏id。暂不开放
MicroAppTitle string `json:"micro_app_title,omitempty"` // 小程序标题
PoiName string `json:"poi_name,omitempty"` // 地理位置名称
TimelinessKeyword string `json:"timeliness_keyword,omitempty"` // 最多可添加3个,用`\|`隔开。暂不开放
VideoId string `json:"video_id"` // video_id, 通过/video/upload/接口得到。注意每次调用/video/create/都要调用/video/upload/生成新的video_id。
AtUsers []string `json:"at_users,omitempty"` // 如果需要at其他用户。将text中@nickname对应的open_id放到这里。
GameContent string `json:"game_content,omitempty"` // 游戏个性化参数
Text string `json:"text,omitempty"` // 视频标题, 可以带话题,@用户。 如title1#话题1 #话题2 @openid1 注意: 1. 话题审核依旧遵循抖音的审核逻辑,强烈建议第三方谨慎拟定话题名称,避免强导流行为。
}
// VideoCreateResData 创建抖音视频
type VideoCreateResData struct {
ItemId string `json:"item_id"` // 视频id
DYError
}
// VideoCreateRes 创建抖音视频
type VideoCreateRes struct {
Data VideoCreateResData `json:"data"`
Extra DYExtra `json:"extra"`
}
// VideoCreate 创建抖音视频
func (m *Manager) VideoCreate(req VideoCreateReq) (res VideoCreateRes, err error) {
err = m.client.CallWithJson(context.Background(), &res, "POST", m.url("%s?access_token=%s&open_id=%s", conf.API_VIDEO_CREATE, req.AccessToken, req.OpenId), nil, req.Body)
return res, err
}
// VideoDeleteReq 删除视频请求
type VideoDeleteReq struct {
OpenId string // 通过/oauth/access_token/获取,用户唯一标志
AccessToken string // 调用/oauth/access_token/生成的token,此token需要用户授权。
Body VideoDeleteBody // 删除视频body
}
// VideoDeleteBody 删除视频
type VideoDeleteBody struct {
ItemId string `json:"item_id,omitempty"` // 抖音视频id
}
// VideoDeleteResData 删除视频
type VideoDeleteResData struct {
DYError
}
// VideoDeleteRes 删除视频
type VideoDeleteRes struct {
Data VideoDeleteResData `json:"data"`
Extra DYExtra `json:"extra"`
}
// VideoDelete 删除授权用户发布的视频
func (m *Manager) VideoDelete(req VideoDeleteReq) (res VideoDeleteRes, err error) {
err = m.client.CallWithJson(context.Background(), &res, "POST", m.url("%s?access_token=%s&open_id=%s", conf.API_VIDEO_DELETE, req.AccessToken, req.OpenId), nil, req.Body)
return res, err
}
// VideoDataReq 视频数据请求
type VideoDataReq struct {
OpenId string // 通过/oauth/access_token/获取,用户唯一标志
AccessToken string // 调用/oauth/access_token/生成的token,此token需要用户授权。
Body VideoDataBody // 视频数据body
}
// VideoDataBody 视频数据
type VideoDataBody struct {
ItemIds []string `json:"item_ids"` // item_id数组,仅能查询access_token对应用户上传的视频
}
// VideoDataResData 视频数据
type VideoDataResData struct {
List []Video `json:"list"` // 视频数据列表
DYError
}
// VideoDataRes 视频数据
type VideoDataRes struct {
Data VideoDataResData `json:"data"`
Extra DYExtra `json:"extra"`
}
// VideoData 查询指定视频数据
func (m *Manager) VideoData(req VideoDataReq) (res VideoDataRes, err error) {
err = m.client.CallWithJson(context.Background(), &res, "POST", m.url("%s?access_token=%s&open_id=%s", conf.API_VIDEO_DATA, req.AccessToken, req.OpenId), nil, req.Body)
return res, err
}
// VideoPartUploadInitReq 初始化分片上传请求
type VideoPartUploadInitReq struct {
OpenId string // 通过/oauth/access_token/获取,用户唯一标志
AccessToken string // 调用/oauth/access_token/生成的token,此token需要用户授权。
}
// VideoPartUploadInitResData 初始化分片上传
type VideoPartUploadInitResData struct {
UploadId string `json:"upload_id"` // 上传id
DYError
}
// VideoPartUploadInitRes 初始化分片上传
type VideoPartUploadInitRes struct {
Data VideoPartUploadInitResData `json:"data"`
Extra DYExtra `json:"extra"`
}
// VideoPartUploadInit 初始化分片上传
func (m *Manager) VideoPartUploadInit(req VideoPartUploadInitReq) (res VideoPartUploadInitRes, err error) {
err = m.client.CallWithJson(context.Background(), &res, "POST", m.url("%s?access_token=%s&open_id=%s", conf.API_VIDEO_UPLOAD_PART_INIT, req.AccessToken, req.OpenId), nil, nil)
return res, err
}
// VideoPartUploadReq 上传视频分片到文件服务器请求
type VideoPartUploadReq struct {
OpenId string // 通过/oauth/access_token/获取,用户唯一标志
AccessToken string // 调用/oauth/access_token/生成的token,此token需要用户授权。
FilePath string // 文件路径
UploadId string // 分片上传的标记。有限时间为2小时。
ChunkSize int64 // 视频分片,建议20MB。但不能小于5MB。
Workers uint // 并发上传的块数量
}
// VideoPartUploadData 上传视频分片到文件服务器
type VideoPartUploadData struct {
DYError
}
// VideoPartUploadRes 上传视频分片到文件服务器
type VideoPartUploadRes struct {
Data VideoPartUploadData `json:"data"`
Extra DYExtra `json:"extra"`
}
// VideoPartUpload 上传视频分片到文件服务器
func (m *Manager) VideoPartUpload(req VideoPartUploadReq) (res *VideoPartUploadRes, err error) {
f, err := os.Open(req.FilePath)
if err != nil {
return nil, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return
}
fsize := fi.Size()
fname := fi.Name()
var (
workers uint = defaultWorkers
chunkSize int64 = defaultChunkSize
wg sync.WaitGroup
)
if req.ChunkSize > 0 {
chunkSize = req.ChunkSize
}
if req.Workers > 0 {
workers = req.Workers
}
tasks := make(chan bool, workers)
chunkTotal := int(fsize/int64(chunkSize) + 1)
for i := 1; i <= chunkTotal; i++ {
wg.Add(1)
tasks <- true
go func(i int) {
defer func() {
wg.Done()
<-tasks
}()
var bodyLength int64 = chunkSize
if i == chunkTotal {
bodyLength = fsize % int64(chunkSize)
}
body := io.NewSectionReader(f, chunkSize*int64(i-1), int64(bodyLength))
var b bytes.Buffer
writer := multipart.NewWriter(&b)
head := make(textproto.MIMEHeader)
extension := filepath.Ext(req.FilePath)
head.Set("Content-Type", "video/"+strings.Replace(extension, ".", "", -1))
head.Set("Content-Disposition", fmt.Sprintf(`form-data; name="video"; filename="%s"`, fname))
if _, err := writer.CreatePart(head); err != nil {
return
}
lastLine := fmt.Sprintf("\r\n--%s--\r\n", writer.Boundary())
r := strings.NewReader(lastLine)
bodyLen := int64(b.Len()) + bodyLength + int64(len(lastLine))
mr := io.MultiReader(&b, body, r)
contentType := writer.FormDataContentType()
headers := http.Header{}
headers.Add("Content-Type", contentType)
uploadId := url.QueryEscape(req.UploadId)
err = m.client.CallWith64(context.Background(), &res, "POST", m.url("%s?access_token=%s&open_id=%s&upload_id=%s&part_number=%d", conf.API_VIDEO_UPLOAD_PART_UPLOAD, req.AccessToken, req.OpenId, uploadId, i), headers, mr, bodyLen)
}(i)
}
wg.Wait()
return res, err
}
// VideoUploadPartCompleteReq 上传视频请求
type VideoUploadPartCompleteReq struct {
OpenId string // 通过/oauth/access_token/获取,用户唯一标志
AccessToken string // 调用/oauth/access_token/生成的token,此token需要用户授权。
UploadId string // 分片上传的标记。有限时间为2小时。
}
// VideoUploadPartCompleteRes 上传视频
type VideoUploadPartCompleteRes struct {
Data VideoUploadResData `json:"data"`
Extra DYExtra `json:"extra"`
}
// VideoUploadPartComplete 完成上传视频
func (m *Manager) VideoUploadPartComplete(req VideoUploadPartCompleteReq) (res VideoUploadPartCompleteRes, err error) {
uploadId := url.QueryEscape(req.UploadId)
err = m.client.CallWithJson(context.Background(), &res, "POST", m.url("%s?access_token=%s&open_id=%s&upload_id=%s", conf.API_VIDEO_UPLOAD_PART_COMPLETE, req.AccessToken, req.OpenId, uploadId), nil, nil)
return res, err
}