huawei.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package api
  2. import (
  3. "errors"
  4. "fmt"
  5. "spu3d/log"
  6. "strings"
  7. "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
  8. mpc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mpc/v1"
  9. hwmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mpc/v1/model"
  10. region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/mpc/v1/region"
  11. )
  12. var (
  13. Ak = "RA1D7CFVCVKUFX1FHHPB"
  14. Endpoint = "mpc.cn-east-3.myhuaweicloud.com"
  15. Sk = "cDrR2NgAF2KaA4MRkcK19r93P3P6hLKOPhHvPu9p"
  16. SourceObjectUrl = "https://sku3d-test.obs.cn-east-3.myhuaweicloud.com/queenshow/"
  17. VideoThumbnailUrl = "https://sku3d-test.obs.cn-east-3.myhuaweicloud.com/thumb-video/"
  18. )
  19. func NewHwClient() *mpc.MpcClient {
  20. ak := Ak
  21. sk := Sk
  22. auth := basic.NewCredentialsBuilder().
  23. WithAk(ak).
  24. WithSk(sk).
  25. Build()
  26. return mpc.NewMpcClient(
  27. mpc.MpcClientBuilder().
  28. WithRegion(region.ValueOf("cn-east-3")).
  29. WithCredential(auth).
  30. Build())
  31. }
  32. func VideoThumbnails(videoURL string) (string, error) {
  33. request := &hwmodel.CreateThumbnailsTaskRequest{}
  34. var listDotsThumbnailPara = []int32{
  35. int32(2),
  36. }
  37. _thum := strings.Split(videoURL, "/")
  38. videoFileName := _thum[len(_thum)-1]
  39. thumImageName := fmt.Sprintf("%s%s", strings.Split(videoFileName, ".")[0], ".jpg")
  40. fmt.Println(videoFileName)
  41. outputFilenameThumbnailPara := thumImageName
  42. typeThumbnailPara := hwmodel.GetThumbnailParaTypeEnum().DOTS
  43. thumbnailParabody := &hwmodel.ThumbnailPara{
  44. Type: &typeThumbnailPara,
  45. Dots: &listDotsThumbnailPara,
  46. OutputFilename: &outputFilenameThumbnailPara,
  47. }
  48. outputbody := &hwmodel.ObsObjInfo{
  49. Bucket: "sku3d-test",
  50. Location: "cn-east-3",
  51. Object: "thumb-video", // jpg
  52. }
  53. outPutImageUrl := fmt.Sprintf("%s%s", VideoThumbnailUrl, thumImageName)
  54. inputbody := &hwmodel.ObsObjInfo{
  55. Bucket: "sku3d-test",
  56. Location: "cn-east-3",
  57. Object: "queenshow/" + videoFileName, // mp4地址
  58. }
  59. request.Body = &hwmodel.CreateThumbReq{
  60. ThumbnailPara: thumbnailParabody,
  61. Output: outputbody,
  62. Input: inputbody,
  63. }
  64. client := NewHwClient()
  65. if client != nil {
  66. res, err := client.CreateThumbnailsTask(request)
  67. if err != nil {
  68. log.Error(err)
  69. fmt.Println(err)
  70. return "", errors.New("视频截图失败!")
  71. }
  72. log.Infof("视频截图[%s]=>taskId:", videoURL, *res.TaskId)
  73. } else {
  74. return "", errors.New("获取华为客户端错误")
  75. }
  76. return outPutImageUrl, nil
  77. }
  78. type VideoCutParams struct {
  79. SourceVideoUrl string
  80. DestVideoName string
  81. Start string
  82. End string
  83. }
  84. type VideoCutOutRes struct {
  85. Url string `json:"url"`
  86. JobId string `json:"jobId"`
  87. }
  88. func VideoCutOut(params *VideoCutParams) (VideoCutOutRes, error) {
  89. _thum := strings.Split(params.SourceVideoUrl, "/")
  90. sourceVideoName := _thum[len(_thum)-1]
  91. request := &hwmodel.CreateEditingJobRequest{}
  92. // 不带后缀
  93. fileNameOutput := params.DestVideoName
  94. outputOutputSetting := &hwmodel.ObsObjInfo{
  95. Bucket: "sku3d-test",
  96. Location: "cn-east-3",
  97. Object: "queenshow",
  98. FileName: &fileNameOutput,
  99. }
  100. outputSettingbody := &hwmodel.OutputSetting{
  101. Output: outputOutputSetting,
  102. }
  103. inputClips := &hwmodel.ObsObjInfo{
  104. Bucket: "sku3d-test",
  105. Location: "cn-east-3",
  106. Object: "queenshow/" + sourceVideoName,
  107. }
  108. timelineStartClips := params.Start
  109. timelineEndClips := params.End
  110. var listClipsbody = []hwmodel.ClipInfo{
  111. {
  112. Input: inputClips,
  113. TimelineStart: &timelineStartClips,
  114. TimelineEnd: &timelineEndClips,
  115. },
  116. }
  117. var listEditTypebody = []string{
  118. "CLIP",
  119. }
  120. request.Body = &hwmodel.CreateEditingJobReq{
  121. OutputSetting: outputSettingbody,
  122. Clips: &listClipsbody,
  123. EditType: &listEditTypebody,
  124. }
  125. outPutVideoUrl := fmt.Sprintf("%s%s.mp4", SourceObjectUrl, fileNameOutput)
  126. client := NewHwClient()
  127. jobId := ""
  128. if client != nil {
  129. res, err := client.CreateEditingJob(request)
  130. if err != nil {
  131. log.Error(err)
  132. fmt.Println(err)
  133. return VideoCutOutRes{}, errors.New("视频裁剪失败!")
  134. }
  135. jobId = *res.JobId
  136. log.Infof("视频裁剪[%s]=>jobId:", params.SourceVideoUrl, *res.JobId)
  137. } else {
  138. return VideoCutOutRes{}, errors.New("获取华为客户端错误!")
  139. }
  140. return VideoCutOutRes{Url: outPutVideoUrl, JobId: jobId}, nil
  141. }
  142. // 查询裁剪任务
  143. func QueryByJobId(jobId string) (*hwmodel.ListEditingJobResponse, error) {
  144. client := NewHwClient()
  145. if client == nil {
  146. return nil, errors.New("获取华为客户端错误!")
  147. }
  148. request := &hwmodel.ListEditingJobRequest{}
  149. var listJobId = []string{
  150. jobId,
  151. }
  152. request.JobId = &listJobId
  153. return client.ListEditingJob(request)
  154. }