获取视频文件的大小

This commit is contained in:
燕陇琪 2024-10-19 18:45:26 +08:00
parent edb308a495
commit 112110946d
3 changed files with 43 additions and 1 deletions

View File

@ -20,4 +20,6 @@ public class MadouVideoInfo {
private Boolean m3u8; private Boolean m3u8;
private Long size;
} }

View File

@ -134,7 +134,6 @@ public class MadouVideoService extends ServiceImpl<MadouVideoMapper, MadouVideoI
} }
} }
@PostConstruct
public void download() throws IOException { public void download() throws IOException {
QueryWrapper<MadouVideoInfo> wrapper = new QueryWrapper<>(); QueryWrapper<MadouVideoInfo> wrapper = new QueryWrapper<>();
wrapper.eq("m3u8", true); wrapper.eq("m3u8", true);
@ -176,4 +175,31 @@ public class MadouVideoService extends ServiceImpl<MadouVideoMapper, MadouVideoI
}); });
} }
} }
@PostConstruct
public void getVideoSize() {
QueryWrapper<MadouVideoInfo> wrapper = new QueryWrapper<>();
wrapper.isNull("size");
List<MadouVideoInfo> madouVideoInfos = list(wrapper);
for (MadouVideoInfo videoInfo : madouVideoInfos) {
scheduledThreadPool.submit(() -> {
try {
String result = httpRequestComponent.get(videoInfo.getM3u8Url());
List<String> tss = Arrays.stream(result.split("\n")).filter(t -> t.contains(".ts")).toList();
long size = 0;
for (int i = 0; i < tss.size(); i++) {
String ts = tss.get(i);
log.info("正在处理ts文件已下载{},共:{},当前进度:{}%", i, tss.size(), String.format("%.2f", (i * 100.0 / tss.size())));
size += httpRequestComponent.getFileSize(videoInfo.getM3u8Url().replace("index.m3u8", ts));
}
videoInfo.setSize(size);
updateById(videoInfo);
} catch (IOException e) {
log.error("处理失败,标题:{}, 失败原因:{}", videoInfo.getTitle(), e.getMessage(), e);
}
});
}
}
} }

View File

@ -110,4 +110,18 @@ public class HttpRequestComponent {
Files.copy(entity.getContent(), path); Files.copy(entity.getContent(), path);
} }
public long getFileSize(String url) throws IOException {
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = closeableHttpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (HttpStatus.SC_OK != statusCode) {
log.error("文件下载失败,状态码:{}", statusCode);
throw new RuntimeException("文件下载失败");
}
HttpEntity entity = response.getEntity();
long contentLength = entity.getContentLength();
response.close();
return contentLength;
}
} }