Skip to content

Commit

Permalink
feat: 표준 해상도를 기준으로 인코딩
Browse files Browse the repository at this point in the history
  • Loading branch information
SJ70 committed Oct 12, 2024
1 parent af2f0ad commit 4f66637
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/main/java/com/j9/bestmoments/service/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.j9.bestmoments.repository.VideoRepository;
import com.j9.bestmoments.service.storageService.LocalStorageService;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.criteria.CriteriaBuilder.In;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
Expand All @@ -27,6 +28,8 @@ public class VideoService {
private final LocalStorageService storageService;
private final FfmpegService ffmpegService;

private final static int[] STANDARD_RESOLUTION_HEIGHTS = {144, 240, 360, 480, 720, 1080, 1440, 2160};

@Transactional
public Video upload(Member member, VideoCreateDto createDto) {
Video video = Video.builder()
Expand All @@ -47,27 +50,25 @@ public Video upload(Member member, VideoCreateDto createDto) {
video.setThumbnailUrl(thumbnailUrl);

// 원본 사이즈 인코딩
String resolution = ffmpegService.getVideoResolution(originVideoUrl);
String encodedVideoUrl = uploadEncodedVideo(originVideoUrl, resolution);
video.addEncodedVideoUrl(encodedVideoUrl);

// 1/2 사이즈 인코딩
String halfResolution = Arrays.stream(resolution.split("x"))
.mapToInt(Integer::parseInt)
.map(value -> value / 2)
.mapToObj(String::valueOf)
.collect(Collectors.joining("x"));
String halfEncodedVideoUrl = uploadEncodedVideo(originVideoUrl, halfResolution);
video.addEncodedVideoUrl(halfEncodedVideoUrl);

// 1/4 사이즈 인코딩
String quarterResolution = Arrays.stream(resolution.split("x"))
.mapToInt(Integer::parseInt)
.map(value -> value / 4)
.mapToObj(String::valueOf)
.collect(Collectors.joining("x"));
String quarterEncodedVideoUrl = uploadEncodedVideo(originVideoUrl, quarterResolution);
video.addEncodedVideoUrl(quarterEncodedVideoUrl);
String originalResolution = ffmpegService.getVideoResolution(originVideoUrl);
String originalSizeEncodedVideoUrl = uploadEncodedVideo(originVideoUrl, originalResolution);

int originalWidth = Integer.parseInt(originalResolution.split("x")[0]);
int originalHeight = Integer.parseInt(originalResolution.split("x")[1]);

for (int height : STANDARD_RESOLUTION_HEIGHTS) {
// 원본 화질보다 작은 화질로만 인코딩
if (height >= originalHeight) {
break;
}
int width = originalWidth * originalHeight / height;
String resolution = width + "x" + height;
String encodedVideoUrl = uploadEncodedVideo(originVideoUrl, resolution);
video.addEncodedVideoUrl(encodedVideoUrl);
}

// 가장 큰 원본 화질은 가장 마지막에 추가
video.addEncodedVideoUrl(originalSizeEncodedVideoUrl);

videoRepository.save(video);
return video;
Expand Down

0 comments on commit 4f66637

Please sign in to comment.