VideoUtils.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package com.tzld.piaoquan.longarticle.utils;
  2. import cn.hutool.http.HttpRequest;
  3. import cn.hutool.http.HttpResponse;
  4. import cn.hutool.json.JSONObject;
  5. import cn.hutool.json.JSONUtil;
  6. import com.tzld.piaoquan.longarticle.model.bo.VideoDetail;
  7. import lombok.extern.slf4j.Slf4j;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. @Slf4j
  12. public class VideoUtils {
  13. private static final String VIDEO_DETAIL_URL = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo";
  14. public static VideoDetail getVideoDetail(Long videoId) {
  15. try {
  16. Map<String, Object> data = new HashMap<>();
  17. data.put("videoIdList", new String[]{String.valueOf(videoId)});
  18. HttpResponse response = HttpRequest.post(VIDEO_DETAIL_URL)
  19. .header("Content-Type", "application/json")
  20. .body(JSONUtil.toJsonStr(data))
  21. .execute();
  22. JSONObject jsonObject = JSONUtil.parseObj(response.body());
  23. List<JSONObject> list = jsonObject.get("data", List.class);
  24. if (list == null || list.isEmpty()) {
  25. return null;
  26. }
  27. JSONObject item = list.get(0);
  28. VideoDetail videoDetail = new VideoDetail();
  29. videoDetail.setVideoId(item.getStr("id"));
  30. videoDetail.setUid(item.getStr("uid"));
  31. videoDetail.setTitle(item.getStr("title"));
  32. videoDetail.setOssVideoPath(item.getStr("ossVideoPath"));
  33. videoDetail.setVideoPath(item.getStr("videoPath"));
  34. String shareImgPath = item.getStr("shareImgPath");
  35. videoDetail.setShareImgPath(shareImgPath);
  36. if (shareImgPath != null && !shareImgPath.isEmpty()) {
  37. String coverImgPath = getCoverImgPath(shareImgPath);
  38. videoDetail.setCoverImgPath(coverImgPath);
  39. videoDetail.setSmallPlayImgPath(getSmallPlayImgPath(coverImgPath));
  40. }
  41. return videoDetail;
  42. } catch (Exception e) {
  43. log.error("VideoUtils getVideoDetail error:{}", e.getMessage());
  44. }
  45. return null;
  46. }
  47. private static String getCoverImgPath(String shareImgPath) {
  48. String[] split = shareImgPath.split("/");
  49. StringBuilder stringBuilder = new StringBuilder();
  50. for (String s : split) {
  51. if (!s.startsWith("watermark")) {
  52. stringBuilder.append(s).append("/");
  53. }
  54. }
  55. if (stringBuilder.length() > 0) {
  56. stringBuilder.deleteCharAt(stringBuilder.length() - 1);
  57. }
  58. return stringBuilder.toString();
  59. }
  60. private static String getSmallPlayImgPath(String coverImgPath) {
  61. return coverImgPath + "/watermark,image_eXNoL3BpYy93YXRlcm1hcmtlci9pY29uX3BsYXlfd2hpdGUucG5nP3gtb3NzLXByb2Nlc3M9aW1hZ2UvcmVzaXplLHdfNzI=,g_center";
  62. }
  63. public static void main(String[] args) {
  64. System.out.println(getVideoDetail(11466300L).getOssVideoPath());
  65. }
  66. }