Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 显示视频的格式码率等信息 #90

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.jmal.clouddisk.ocr.OcrService;
import com.jmal.clouddisk.service.impl.UserLoginHolder;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,10 +19,11 @@
public class OcrController {

private final OcrService ocrService;
private final UserLoginHolder userLoginHolder;

@GetMapping("/ocr")
public String performOcr(@RequestParam String fileUrl) {
String tempImagePath = ocrService.generateOrcTempImagePath();
String tempImagePath = ocrService.generateOrcTempImagePath(userLoginHolder.getUsername());
try {
HttpUtil.downloadFile(fileUrl, tempImagePath);
TimeInterval timeInterval = new TimeInterval();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.hutool.core.io.FileUtil;
import com.jmal.clouddisk.ocr.OcrService;
import com.jmal.clouddisk.service.impl.CommonFileService;
import com.jmal.clouddisk.util.FileContentUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -20,6 +21,7 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

@Service
@RequiredArgsConstructor
Expand All @@ -28,10 +30,13 @@ public class ReadPDFContentService {

private final OcrService ocrService;

public final CommonFileService commonFileService;

public final TaskProgressService taskProgressService;

public String read(File file) {
try (PDDocument document = Loader.loadPDF(new RandomAccessReadBufferedFile(file))) {
String username = commonFileService.getUsernameByAbsolutePath(Path.of(file.getAbsolutePath()));
StringBuilder content = new StringBuilder();
// 提取每一页的内容
PDFTextStripper pdfStripper = new PDFTextStripper();
Expand All @@ -50,11 +55,11 @@ public String read(File file) {
if (xObject instanceof PDImageXObject image) {
BufferedImage bufferedImage = image.getImage();
// 将图像保存到临时文件
String tempImageFile = ocrService.generateOrcTempImagePath();
String tempImageFile = ocrService.generateOrcTempImagePath(username);
ImageIO.write(bufferedImage, "png", new File(tempImageFile));
try {
// 使用 Tesseract 进行 OCR 识别
String ocrResult = ocrService.doOCR(tempImageFile, null);
String ocrResult = ocrService.doOCR(tempImageFile, ocrService.generateOrcTempImagePath(username));
content.append(ocrResult);
} finally {
// 删除临时文件
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,7 @@ private void delayResetIndex() {
throttleExecutor = new ThrottleExecutor(3000);
}
}
if (INDEXED_TASK_SIZE.get() == NOT_INDEX_TASK_SIZE.get()) {
// 3秒钟后重置索引任务数据
throttleExecutor.schedule(this::rebuildingIndexCompleted);
} else {
throttleExecutor.cancel();
}
throttleExecutor.schedule(this::rebuildingIndexCompleted);
}

private void updatePercent() {
Expand Down Expand Up @@ -348,7 +343,7 @@ private double getSyncPercent() {
* 获取索引进度
*/
private double getIndexedPercent() {
if (totalCount == 0 || NOT_INDEX_TASK_SIZE.get() == 0 || isSyncFile()) {
if (NOT_INDEX_TASK_SIZE.get() == 0 || isSyncFile()) {
return 100;
}
return getIndexedPercentValue();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/jmal/clouddisk/model/FileDocument.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jmal.clouddisk.model;

import com.jmal.clouddisk.service.impl.FileServiceImpl;
import com.jmal.clouddisk.video.VideoInfoDO;
import lombok.Data;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
Expand Down Expand Up @@ -129,6 +130,10 @@ public class FileDocument extends FileBase {
* 照片exif信息
*/
private ExifInfo exif;
/**
* 视频信息
*/
private VideoInfoDO video;
/**
* 媒体封面
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/jmal/clouddisk/model/FileIntroVO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jmal.clouddisk.model;

import com.jmal.clouddisk.video.VideoInfoDO;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand Down Expand Up @@ -67,6 +68,10 @@ public class FileIntroVO extends FileBase {
* 照片exif信息
*/
private ExifInfo exif;
/**
* 视频信息
*/
private VideoInfoDO video;
/**
* 媒体封面
*/
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/jmal/clouddisk/ocr/OcrService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public String doOCR(String imagePath, String tempImagePath) {
return "";
}
if (StrUtil.isBlank(tempImagePath)) {
tempImagePath = generateOrcTempImagePath();
tempImagePath = generateOrcTempImagePath(null);
}
System.out.println("tempImageFile: " + tempImagePath);
// 预处理后的图片
String preprocessedOCRImage = getPreprocessedOCRImage(imagePath, tempImagePath);
if (StrUtil.isBlank(preprocessedOCRImage)) {
Expand All @@ -57,12 +58,17 @@ public String doOCR(String imagePath, String tempImagePath) {
/**
* 生成一个临时的图片路径
*/
public String generateOrcTempImagePath() {
Path tempPath = Paths.get(fileProperties.getRootDir(), fileProperties.getChunkFileDir());
public String generateOrcTempImagePath(String username) {
Path tempPath;
if (StrUtil.isBlank(username)) {
tempPath = Paths.get(fileProperties.getRootDir(), fileProperties.getChunkFileDir());
} else {
tempPath = Paths.get(fileProperties.getRootDir(), fileProperties.getChunkFileDir(), username);
}
if (!FileUtil.exist(tempPath.toString())) {
FileUtil.mkdir(tempPath.toString());
}
return Paths.get(fileProperties.getRootDir(), fileProperties.getChunkFileDir(), ObjectId.next(true) + "_temp_ocr.png").toString();
return Paths.get(tempPath.toString(), ObjectId.next(true) + "_temp_ocr.png").toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import com.jmal.clouddisk.oss.OssConfigService;
import com.jmal.clouddisk.service.Constants;
import com.jmal.clouddisk.service.IUserService;
import com.jmal.clouddisk.video.VideoProcessService;
import com.jmal.clouddisk.util.*;
import com.jmal.clouddisk.video.VideoInfo;
import com.jmal.clouddisk.video.VideoProcessService;
import com.jmal.clouddisk.webdav.MyWebdavServlet;
import com.luciad.imageio.webp.WebPWriteParam;
import com.mongodb.client.AggregateIterable;
Expand Down Expand Up @@ -324,6 +325,7 @@ public String createFile(String username, File file, String userId, Boolean isPu
// 添加文件索引
// 获取tagName
updateExifInfo(file, fileExists, contentType, suffix, query);
updateVideoInfo(file, fileExists, contentType, query);
luceneService.pushCreateIndexQueue(fileExists.getId());
return fileExists.getId();
}
Expand Down Expand Up @@ -381,16 +383,20 @@ private String getRelativePath(String username, String fileAbsolutePath, String
* @param query 查询条件
*/
private void updateExifInfo(File file, FileDocument fileExists, String contentType, String suffix, Query query) {
if (fileExists.getExif() == null) {
if (ImageExifUtil.isImageType(contentType, suffix)) {
// 更新图片Exif信息
ExifInfo exifInfo = ImageExifUtil.getExif(file);
if (exifInfo != null) {
Update update = new Update();
update.set("exif", exifInfo);
mongoTemplate.updateFirst(query, update, COLLECTION_NAME);
}
}
if (fileExists.getExif() == null && ImageExifUtil.isImageType(contentType, suffix)) {
// 更新图片Exif信息
Update update = new Update();
update.set("exif", ImageExifUtil.getExif(file));
mongoTemplate.updateFirst(query, update, COLLECTION_NAME);
}
}

private void updateVideoInfo(File file, FileDocument fileExists, String contentType, Query query) {
if (contentType.contains(Constants.VIDEO) && fileExists.getVideo() == null) {
VideoInfo videoInfo = videoProcessService.getVideoInfo(file);
Update update = new Update();
update.set("video", videoInfo.toVideoInfoDO());
mongoTemplate.updateFirst(query, update, COLLECTION_NAME);
}
}

Expand Down Expand Up @@ -510,7 +516,7 @@ private void processImage(File file, Update update) {
update.set("h", imageInfo.getHeight());
}
// 获取图片Exif信息
ImageExifUtil.setExifInfo(file, update);
update.set("exif", ImageExifUtil.getExif(file));
// 生成缩略图
generateThumbnail(file, update);
}
Expand Down Expand Up @@ -545,14 +551,16 @@ private static void setMusic(File file, Update update) {
}

private void setMediaCover(String fileId, String username, String fileName, String relativePath, Update update) {
String coverPath = videoProcessService.getVideoCover(fileId, username, relativePath, fileName);
log.info("\r\ncoverPath:{}", coverPath);
VideoInfo videoInfo = videoProcessService.getVideoCover(fileId, username, relativePath, fileName);
String coverPath = videoInfo.getCovertPath();
log.debug("\r\ncoverPath:{}", coverPath);
if (!CharSequenceUtil.isBlank(coverPath)) {
if (update == null) {
update = new Update();
}
update.set("content", PathUtil.readBytes(Paths.get(coverPath)));
videoProcessService.convertToM3U8(fileId, username, relativePath, fileName);
update.set("video", videoInfo.toVideoInfoDO());
videoProcessService.convertToM3U8(fileId);
update.set("mediaCover", true);
FileUtil.del(coverPath);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import com.jmal.clouddisk.service.Constants;
import com.jmal.clouddisk.service.IFileService;
import com.jmal.clouddisk.service.IFileVersionService;
import com.jmal.clouddisk.video.VideoProcessService;
import com.jmal.clouddisk.util.*;
import com.jmal.clouddisk.video.VideoInfo;
import com.jmal.clouddisk.video.VideoProcessService;
import com.jmal.clouddisk.webdav.MyWebdavServlet;
import com.mongodb.client.AggregateIterable;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -624,7 +625,8 @@ private void setMediaCover(String id, String username, FileDocument fileDocument
if (contentType.contains(Constants.VIDEO)) {
// 视频文件
Query query = new Query().addCriteria(Criteria.where("_id").is(id));
String imagePath = videoProcessService.getVideoCover(id, username, fileDocument.getPath(), fileDocument.getName());
VideoInfo videoInfo = videoProcessService.getVideoCover(id, username, fileDocument.getPath(), fileDocument.getName());
String imagePath = videoInfo.getCovertPath();
if (!CharSequenceUtil.isBlank(imagePath)) {
fileDocument.setContent(FileUtil.readBytes(imagePath));
if (hasOldFileDocument) {
Expand Down
23 changes: 4 additions & 19 deletions src/main/java/com/jmal/clouddisk/util/ImageExifUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.jmal.clouddisk.model.ExifInfo;
import com.jmal.clouddisk.service.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Update;

import java.io.File;
import java.util.Date;
Expand All @@ -24,23 +23,15 @@ public static boolean isImageType(String contentType, String suffix) {
return contentType.startsWith(Constants.CONTENT_TYPE_IMAGE) && (!"ico".equals(suffix) && !"svg".equals(suffix));
}

public static void setExifInfo(File file, Update update) {
// 获取照片Exif信息
ExifInfo exifInfo = ImageExifUtil.getExif(file);
if (exifInfo != null) {
update.set("exif", exifInfo);
}
}

public static ExifInfo getExif(File file) {
ExifInfo exifInfo = new ExifInfo();
if (file == null) {
return null;
return exifInfo;
}
if (!file.exists()) {
return null;
return exifInfo;
}
try {
ExifInfo exifInfo = null;
Metadata metadata = ImageMetadataReader.readMetadata(file);
// 获取图片基础信息
ExifIFD0Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
Expand All @@ -67,9 +58,6 @@ public static ExifInfo getExif(File file) {
// 获取图片的Exif信息
ExifSubIFDDirectory exifDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
if (exifDirectory != null) {
if (exifInfo == null) {
exifInfo = new ExifInfo();
}
ExifSubIFDDescriptor descriptor = new ExifSubIFDDescriptor(exifDirectory);
// 内容创建时间
if (exifDirectory.containsTag(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)) {
Expand Down Expand Up @@ -118,9 +106,6 @@ public static ExifInfo getExif(File file) {
// 获取GPS信息
GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);
if (gpsDirectory != null) {
if (exifInfo == null) {
exifInfo = new ExifInfo();
}
if (gpsDirectory.getGeoLocation() != null) {
// 经度
exifInfo.setLongitude(gpsDirectory.getGeoLocation().getLongitude());
Expand All @@ -135,7 +120,7 @@ public static ExifInfo getExif(File file) {
// 获取图片EXIF信息失败
log.warn("获取图片EXIF信息失败: {}, {}", e.getMessage(), file);
}
return null;
return exifInfo;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/jmal/clouddisk/video/TranscodeStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.jmal.clouddisk.video;

import lombok.Getter;

/**
* 转码状态
*/
@Getter
public enum TranscodeStatus {
/**
* 待转码
*/
NOT_TRANSCODE(0),
/**
* 正在进行转码
*/
TRANSCODING(1),
/**
* 已完成转码
*/
TRANSCENDED(2);

private final int status;

TranscodeStatus(int status) {
this.status = status;
}

}
Loading