Skip to content

Commit

Permalink
Merge pull request #125 from jamebal/recycle
Browse files Browse the repository at this point in the history
perf: 优化nvidia生成vtt缩略图时推送进度
  • Loading branch information
jamebal authored Jul 8, 2024
2 parents 4f6c862 + ac04ade commit ff6a894
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/jmal/clouddisk/video/FFMPEGCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ static ProcessBuilder cpuTranscoding(String fileId, Path fileAbsolutePath, int b
);
}

static ProcessBuilder useNvencCuda(String fileId, Path fileAbsolutePath, int bitrate, int height, String videoCacheDir, String outputPath, int vttInterval, String thumbnailPattern, double frameRate) {
static ProcessBuilder useNvencCuda(String fileId, Path fileAbsolutePath, int bitrate, int height, String videoCacheDir, String outputPath, double frameRate) {
// 使用CUDA硬件加速和NVENC编码器
return new ProcessBuilder(
Constants.FFMPEG,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jmal/clouddisk/video/FFMPEGUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void generateVTT(VideoInfo videoInfo, int interval, String vttFilePath, S
int row = i / columns;
int thumbWidth = FFMPEGCommand.thumbnailWidth;
double tHeight = (double) videoInfo.getHeight() / ((double) videoInfo.getWidth() / FFMPEGCommand.thumbnailWidth);
int thumbHeight = (int) Math.ceil(tHeight);
int thumbHeight = (int) Math.floor(tHeight);
int x = column * thumbWidth;
int y = row * thumbHeight;

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/jmal/clouddisk/video/VideoProcessService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ public class VideoProcessService {
@Resource
MongoTemplate mongoTemplate;

private final static ReentrantLock VTT_LOCK = new ReentrantLock();

/**
* 视频转码线程池
*/
Expand Down Expand Up @@ -547,7 +545,7 @@ private void videoToM3U8(String fileId, String username, String relativePath, St
ProcessBuilder processBuilder = FFMPEGCommand.cpuTranscoding(fileId, fileAbsolutePath, bitrate, targetHeight, videoCacheDir, outputPath, vttInterval, thumbnailPattern, frameRate);
if (!onlyCPU && FFMPEGCommand.checkNvidiaDrive()) {
log.info("use NVENC hardware acceleration");
processBuilder = FFMPEGCommand.useNvencCuda(fileId, fileAbsolutePath, bitrate, targetHeight, videoCacheDir, outputPath, vttInterval, thumbnailPattern, frameRate);
processBuilder = FFMPEGCommand.useNvencCuda(fileId, fileAbsolutePath, bitrate, targetHeight, videoCacheDir, outputPath, frameRate);
generateVttOfNvidia(fileAbsolutePath, vttInterval, thumbnailPattern, videoInfo);
}
if (!onlyCPU && FFMPEGCommand.checkMacAppleSilicon()) {
Expand Down Expand Up @@ -575,7 +573,7 @@ private void videoToM3U8(String fileId, String username, String relativePath, St
startConvert(username, relativePath, fileName, fileId, transcodeConfig);
pushMessage = true;
}
transcodingProgress(fileAbsolutePath, videoInfo.getDuration(), line, "转码进度");
transcodingProgress(fileAbsolutePath, videoInfo.getDuration(), line, "");
}

// 生成vtt缩略图
Expand Down Expand Up @@ -604,7 +602,7 @@ private void videoToM3U8(String fileId, String username, String relativePath, St
}
}

private void generateVttOfNvidia(Path fileAbsolutePath, int vttInterval, String thumbnailPattern, VideoInfo videoInfo) throws IOException, InterruptedException {
private void generateVttOfNvidia(Path fileAbsolutePath, int vttInterval, String thumbnailPattern, VideoInfo videoInfo) throws IOException {
// 生成vtt缩略图, nvidia加速时要单独生成vtt缩略图
taskProgressService.addTaskProgress(fileAbsolutePath.toFile(), TaskType.TRANSCODE_VIDEO, "vtt生成中...");
ProcessBuilder processBuilder = FFMPEGCommand.useNvencCudaVtt(fileAbsolutePath, vttInterval, thumbnailPattern);
Expand All @@ -621,7 +619,7 @@ private void generateVttOfNvidia(Path fileAbsolutePath, int vttInterval, String
if (line.contains("Error")) {
log.error(line);
}
transcodingProgress(fileAbsolutePath, videoInfo.getDuration(), line, "vtt生成进度");
transcodingProgress(fileAbsolutePath, videoInfo.getDuration(), line, "vtt—");
}
int exitCode = process.waitFor();
if (exitCode == 0) {
Expand Down Expand Up @@ -699,8 +697,8 @@ private void transcodingProgress(Path fileAbsolutePath, int videoDuration, Strin
try {
if (line.contains(":")) {
String progressStr = FFMPEGUtils.getProgressStr(videoDuration, line);
log.debug("{}, {}: {}%", fileAbsolutePath.getFileName(), desc, progressStr);
taskProgressService.addTaskProgress(fileAbsolutePath.toFile(), TaskType.TRANSCODE_VIDEO, progressStr + "%");
log.debug("{}, 转码进度: {}%", fileAbsolutePath.getFileName(), progressStr);
taskProgressService.addTaskProgress(fileAbsolutePath.toFile(), TaskType.TRANSCODE_VIDEO, desc + progressStr + "%");
}
} catch (Exception e) {
log.warn(e.getMessage(), e);
Expand Down

0 comments on commit ff6a894

Please sign in to comment.