Skip to content

Commit

Permalink
Merge pull request #8 from goormthon-Univ/develope
Browse files Browse the repository at this point in the history
feat: TTS를 활용한 발화 예상시간 추출
  • Loading branch information
Sirius506775 authored Mar 23, 2024
2 parents e7dc128 + 34a2679 commit a8bbb04
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ dependencies {
/* 검증을 위한 validation 의존성 추가*/
implementation 'org.springframework.boot:spring-boot-starter-validation'

// https://mvnrepository.com/artifact/org/jaudiotagger
implementation 'org:jaudiotagger:2.0.3'

// https://mvnrepository.com/artifact/org.apache.tika/tika-core
implementation 'org.apache.tika:tika-core:2.9.1'

// https://mvnrepository.com/artifact/javazoom/jlayer
implementation 'javazoom:jlayer:1.0.1'



}

tasks.named('test') {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/site/balpyo/ai/controller/PollyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.amazonaws.util.IOUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jaudiotagger.audio.exceptions.CannotReadException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.tag.TagException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -79,7 +83,7 @@ public ResponseEntity<?> synthesizeText(@RequestBody PollyDTO pollyDTO) {
}

@PostMapping("/uploadSpeech")
public ResponseEntity<UploadResultDTO> synthesizeAndUploadSpeech(@RequestBody PollyDTO pollyDTO) throws IOException {
public ResponseEntity<UploadResultDTO> synthesizeAndUploadSpeech(@RequestBody PollyDTO pollyDTO) throws IOException, CannotReadException, TagException, InvalidAudioFrameException, ReadOnlyFileException {
UploadResultDTO uploadResultDTO = pollyService.synthesizeAndUploadSpeech(pollyDTO);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/site/balpyo/ai/dto/upload/UploadResultDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
@NoArgsConstructor
public class UploadResultDTO {
private String profileUrl;
}

private int playTime;
}
74 changes: 63 additions & 11 deletions src/main/java/site/balpyo/ai/service/PollyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@
import com.amazonaws.services.s3.model.Permission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import site.balpyo.ai.dto.PollyDTO;
import site.balpyo.ai.dto.upload.UploadResultDTO;
import site.balpyo.common.s3.S3Client;

import java.io.InputStream;

import java.io.*;
import java.net.URL;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
Expand Down Expand Up @@ -148,33 +158,75 @@ public UploadResultDTO synthesizeAndUploadSpeech(PollyDTO pollyDTO) {
log.info("--------------------- " + fileName);

// S3에 업로드
String profileUrl = uploadToS3(audioStream, fileName);
Map<String, Object> audioInfo = uploadToS3(audioStream, fileName);

log.info("--------------------- " + profileUrl);
String baseUploadURL = audioInfo.get("baseUploadURL").toString();
int durationInSeconds = (int) audioInfo.get("durationInSeconds");
log.info("--------------------- " + baseUploadURL);
log.info("--------------------- " + durationInSeconds);


return UploadResultDTO.builder()
.profileUrl(profileUrl)
.profileUrl(baseUploadURL)
.playTime(durationInSeconds)
.build();
}

private String uploadToS3(InputStream inputStream, String fileName) {
String objectPath = "/" + fileName; // S3에 저장될 경로
private Map<String, Object> uploadToS3(InputStream inputStream, String fileName) {

log.info("--------------------- " + fileName);

// S3에 업로드
s3Client.getAmazonS3().putObject(bucketName, objectPath, inputStream, new ObjectMetadata());
s3Client.getAmazonS3().putObject(bucketName, fileName, inputStream, new ObjectMetadata());

// ACL 설정
setAcl(s3Client.getAmazonS3(), objectPath);
setAcl(s3Client.getAmazonS3(), fileName);

// 업로드된 파일의 URL 생성
String baseUploadURL = "https://balpyo-bucket.s3.ap-northeast-2.amazonaws.com/audio";
String baseUploadURL = "https://balpyo-bucket.s3.ap-northeast-2.amazonaws.com/" + fileName;

log.info("업로드 위치------" + baseUploadURL);

// 임시 파일로 저장하여 처리
int durationInSeconds = 0; // 초기화

try {

URL url = new URL(baseUploadURL);
InputStream targetStream = url.openStream();
fileName = Paths.get(url.getPath()).getFileName().toString();
File localFile = new File(System.getProperty("java.io.tmpdir"), fileName);

log.info("업로드 위치------" + baseUploadURL + objectPath);
// File localFile = new File(baseUploadURL);
log.info("Download------" + localFile);
Files.copy(targetStream, localFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
targetStream.close(); // 스트림 닫기

return baseUploadURL + objectPath;
// MP3 파일의 재생 시간 계산
MP3File mp3File = new MP3File(localFile);

log.info("mp3 file" + mp3File);

MP3AudioHeader audioHeader = (MP3AudioHeader) mp3File.getAudioHeader();
durationInSeconds = audioHeader.getTrackLength();

log.info("------------ 재생시간: " + durationInSeconds + "초");

// 임시 파일 삭제
localFile.delete();

} catch (Exception e) {
e.printStackTrace();
}

// 결과를 Map에 담아 반환
Map<String, Object> result = new HashMap<>();
result.put("baseUploadURL", baseUploadURL);
result.put("durationInSeconds", durationInSeconds);
return result;
}


public void setAcl(AmazonS3 s3, String objectPath) {
AccessControlList objectAcl = s3.getObjectAcl(bucketName, objectPath);
objectAcl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
Expand Down

0 comments on commit a8bbb04

Please sign in to comment.