|
|
@@ -82,6 +82,14 @@ import com.fs.system.mapper.SysDictDataMapper;
|
|
|
import com.fs.system.service.ISysConfigService;
|
|
|
import com.fs.voice.utils.StringUtil;
|
|
|
import com.github.binarywang.wxpay.bean.transfer.TransferBillsResult;
|
|
|
+import com.volcengine.service.vod.IVodService;
|
|
|
+import com.volcengine.service.vod.model.business.VodUrlUploadURLSet;
|
|
|
+import com.volcengine.service.vod.model.request.VodGetMediaInfosRequest;
|
|
|
+import com.volcengine.service.vod.model.request.VodQueryUploadTaskInfoRequest;
|
|
|
+import com.volcengine.service.vod.model.request.VodUrlUploadRequest;
|
|
|
+import com.volcengine.service.vod.model.response.VodGetMediaInfosResponse;
|
|
|
+import com.volcengine.service.vod.model.response.VodQueryUploadTaskInfoResponse;
|
|
|
+import com.volcengine.service.vod.model.response.VodUrlUploadResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.redisson.api.RLock;
|
|
|
@@ -97,11 +105,12 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.time.*;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.CompletableFuture;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.concurrent.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -4034,5 +4043,199 @@ public class FsUserCourseVideoServiceImpl extends ServiceImpl<FsUserCourseVideoM
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
+ public R uploadVideoToHuoShanByUrl() {
|
|
|
+ List <FsUserCourseVideo> videos = fsUserCourseVideoMapper.selectVideoByHuaWei();
|
|
|
+ for (FsUserCourseVideo video : videos){
|
|
|
+ uploadVideoByUrl(video);
|
|
|
+ }
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IVodService vodService;
|
|
|
+
|
|
|
+ //通过Url上传视频
|
|
|
+ public void uploadVideoByUrl(FsUserCourseVideo courseVideo) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ VodUrlUploadRequest.Builder reqBuilder = VodUrlUploadRequest.newBuilder();
|
|
|
+ //空间名称
|
|
|
+ reqBuilder.setSpaceName(cloudHostProper.getSpaceName());
|
|
|
+ VodUrlUploadURLSet.Builder uRLSetsBuilder = VodUrlUploadURLSet.newBuilder();
|
|
|
+ //源文件 URL
|
|
|
+ uRLSetsBuilder.setSourceUrl(courseVideo.getLineTwo());//华为云
|
|
|
+ //存储类型。默认为 1。取值如下:
|
|
|
+ //1:标准存储。
|
|
|
+ //2:归档存储。
|
|
|
+ //3:低频存储。
|
|
|
+ uRLSetsBuilder.setStorageClass(1);
|
|
|
+ //文件后缀,即点播存储中文件的类型。
|
|
|
+ uRLSetsBuilder.setFileExtension(".mp4");
|
|
|
+ //用户额外信息,最大长度 512 字节。
|
|
|
+ uRLSetsBuilder.setCallbackArgs("");
|
|
|
+ // 火山云存储路径(文件上传后在火山云的路径)
|
|
|
+ String datePath = new SimpleDateFormat("yyyyMMdd").format(new Date());
|
|
|
+ String fileName = System.currentTimeMillis() + ".mp4";
|
|
|
+ String remoteFileName = "fs/" + datePath + "/" + fileName;
|
|
|
+ uRLSetsBuilder.setFileName(remoteFileName);
|
|
|
+ reqBuilder.addURLSets(uRLSetsBuilder);
|
|
|
+
|
|
|
+ VodUrlUploadResponse resp = vodService.uploadMediaByUrl(reqBuilder.build());
|
|
|
+
|
|
|
+ if (resp.getResponseMetadata().hasError()) {
|
|
|
+ log.info("上传返回异常:{}",resp.getResponseMetadata().getError());
|
|
|
+ System.exit(-1);
|
|
|
+ }else {
|
|
|
+ FsUserCourseVideo video = new FsUserCourseVideo();
|
|
|
+ video.setVideoId(courseVideo.getVideoId());
|
|
|
+ video.setJobId(resp.getResult().getData(0).getJobId());
|
|
|
+ //更新JobId
|
|
|
+ fsUserCourseVideoMapper.updateFsUserCourseVideo(video);
|
|
|
+ }
|
|
|
+ log.info("上传返回参数:{}",resp);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("视频上传失败: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R getVidByJob() {
|
|
|
+ // 查询有JobId的视频
|
|
|
+ List<FsUserCourseVideo> list = fsUserCourseVideoMapper.selectVideoByJobId();
|
|
|
+ if (list.isEmpty()) {
|
|
|
+ log.info("没有待上传的视频任务");
|
|
|
+ return R.error();
|
|
|
+ }
|
|
|
+ // 按五百一批切割
|
|
|
+ List<List<FsUserCourseVideo>> batches = splitList(list, 500);
|
|
|
+ log.info("总任务 {} 条,分成 {} 批", list.size(), batches.size());
|
|
|
+
|
|
|
+ int batchIndex = 1;
|
|
|
+
|
|
|
+ // 批次顺序执行,每批内部多线程并发执行
|
|
|
+ for (List<FsUserCourseVideo> batch : batches) {
|
|
|
+
|
|
|
+ log.info("开始执行批次 {}/{},本批任务 {} 条", batchIndex, batches.size(), batch.size());
|
|
|
+
|
|
|
+ CountDownLatch latch = new CountDownLatch(batch.size());
|
|
|
+
|
|
|
+ for (FsUserCourseVideo video : batch) {
|
|
|
+ uploadExecutor.submit(() -> {
|
|
|
+ try {
|
|
|
+ uploadSingleTaskWithRetry(video);
|
|
|
+ } finally {
|
|
|
+ latch.countDown();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 等待这一批全部完成
|
|
|
+ try {
|
|
|
+ latch.await();
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ log.error("批次等待异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("批次 {}/{} 执行完成", batchIndex, batches.size());
|
|
|
+ batchIndex++;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("全部批次执行完成");
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+ private final ExecutorService uploadExecutor = new ThreadPoolExecutor(
|
|
|
+ 8, // core
|
|
|
+ 16, // max
|
|
|
+ 60L, TimeUnit.SECONDS,
|
|
|
+ new LinkedBlockingQueue<>(2000),
|
|
|
+ new ThreadFactory() {
|
|
|
+ private final AtomicInteger index = new AtomicInteger(1);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Thread newThread(Runnable r) {
|
|
|
+ return new Thread(r, "video-upload-" + index.getAndIncrement());
|
|
|
+ }
|
|
|
+ },
|
|
|
+ new ThreadPoolExecutor.CallerRunsPolicy()
|
|
|
+ );
|
|
|
+
|
|
|
+ public <T> List<List<T>> splitList(List<T> list, int batchSize) {
|
|
|
+ List<List<T>> result = new ArrayList<>();
|
|
|
+ int total = list.size();
|
|
|
+ for (int i = 0; i < total; i += batchSize) {
|
|
|
+ result.add(list.subList(i, Math.min(total, i + batchSize)));
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据jobid查询上传视频的vid
|
|
|
+ public void getVidByJobId(FsUserCourseVideo courseVideo){
|
|
|
+ try {
|
|
|
+ VodQueryUploadTaskInfoRequest.Builder reqBuilder = VodQueryUploadTaskInfoRequest.newBuilder();
|
|
|
+ reqBuilder.setJobIds(courseVideo.getJobId());
|
|
|
+
|
|
|
+ VodQueryUploadTaskInfoResponse resp = vodService.queryUploadTaskInfo(reqBuilder.build());
|
|
|
+ if (resp.getResponseMetadata().hasError()) {
|
|
|
+ System.out.println(resp.getResponseMetadata().getError());
|
|
|
+ System.exit(-1);
|
|
|
+ }else {
|
|
|
+ FsUserCourseVideo video = new FsUserCourseVideo();
|
|
|
+ video.setVideoId(courseVideo.getVideoId());
|
|
|
+ video.setVid(resp.getResult().getData().getMediaInfoList(0).getVid());
|
|
|
+ fsUserCourseVideoMapper.updateFsUserCourseVideo(video);
|
|
|
+ }
|
|
|
+ System.out.println(resp);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("查询 URL 批量上传任务状态: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void getVideoInfoByVid(FsUserCourseVideo courseVideo) {
|
|
|
+ try {
|
|
|
+ VodGetMediaInfosRequest.Builder reqBuilder = VodGetMediaInfosRequest.newBuilder();
|
|
|
+ reqBuilder.setVids(courseVideo.getVid());
|
|
|
+
|
|
|
+ VodGetMediaInfosResponse resp = vodService.getMediaInfos20230701(reqBuilder.build());
|
|
|
+ if (resp.getResponseMetadata().hasError()) {
|
|
|
+ System.out.println(resp.getResponseMetadata().getError());
|
|
|
+ System.exit(-1);
|
|
|
+ }else {
|
|
|
+ FsUserCourseVideo video = new FsUserCourseVideo();
|
|
|
+ video.setVideoId(courseVideo.getVideoId());
|
|
|
+ video.setLineTwo(resp.getResult().getMediaInfoList(0).getSourceInfo().getStoreUri());
|
|
|
+ fsUserCourseVideoMapper.updateFsUserCourseVideo(video);
|
|
|
+ }
|
|
|
+ System.out.println(resp);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void uploadSingleTaskWithRetry(FsUserCourseVideo courseVideo) {
|
|
|
+ int maxRetry = 3;
|
|
|
+ for (int i = 1; i <= maxRetry; i++) {
|
|
|
+ try {
|
|
|
+ //获取上传成功的视频vid,同步到数据库
|
|
|
+ getVidByJobId(courseVideo);
|
|
|
+ //查询需要上传的视频,上传后将视频任务id存到数据库
|
|
|
+// uploadVideoByUrl(fsUserVideo);
|
|
|
+ //根据视频vid获取火山云的视频信息,并同步到数据库
|
|
|
+ /**
|
|
|
+ * 下面这个方法调用前去润天his_java确认一下,是否一致的
|
|
|
+ */
|
|
|
+ //getVideoInfoByVid(fsUserVideo);
|
|
|
+ return;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("视频 {} 上传失败,第 {} 次重试,原因:{}",
|
|
|
+ courseVideo.getVideoId(), i, e.getMessage());
|
|
|
+ if (i == maxRetry) {
|
|
|
+ log.error("视频 {} 上传最终失败!", courseVideo.getVideoId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|