-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
146 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
backend/src/main/java/com/example/BriefMe/controllers/YoutubeVideoSummaryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/com/example/BriefMe/service/client/VideoToTextConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.example.BriefMe.service.client; | ||
|
||
public interface VideoToTextConverter { | ||
String fetchSubtitlesJsonFileFromVideo(String video); | ||
String readSubtitlesFromJsonFile(String filePath); | ||
} |
5 changes: 0 additions & 5 deletions
5
backend/src/main/java/com/example/BriefMe/service/client/YoutubeVideoSummaryService.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
backend/src/main/java/com/example/BriefMe/service/impl/YoutubeVideoToTextConverterImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.example.BriefMe.service.impl; | ||
|
||
import com.example.BriefMe.service.client.VideoToTextConverter; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Slf4j | ||
public class YoutubeVideoToTextConverterImpl implements VideoToTextConverter { | ||
|
||
public static final String OUTPUT_FILE = "output-"; | ||
public static final String DOT = "."; | ||
|
||
public static final String MP_4 = "mp4"; | ||
public static final String SUBTITLES_FORMAT = "json3"; | ||
|
||
@Override | ||
public String fetchSubtitlesJsonFileFromVideo(String inputVideo) { | ||
String outputFile = OUTPUT_FILE + UUID.randomUUID(); | ||
String subtitlesLanguage = "en"; | ||
|
||
try { | ||
String[] command = { | ||
"yt-dlp", | ||
"--skip-download", | ||
"--write-subs", | ||
"--write-auto-subs", | ||
"--sub-langs", | ||
subtitlesLanguage, | ||
"--sub-format", | ||
SUBTITLES_FORMAT, | ||
"--sleep-interval", | ||
String.valueOf(1), | ||
inputVideo, | ||
"-o", | ||
outputFile + "%(ext)s" | ||
}; | ||
|
||
// Execute the command | ||
ProcessBuilder processBuilder = new ProcessBuilder(command); | ||
processBuilder.redirectErrorStream(true); | ||
Process process = processBuilder.start(); | ||
|
||
// Read the output of the command | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
log.info(line); | ||
} | ||
|
||
// Wait for the process to finish | ||
int exitCode = process.waitFor(); | ||
if (exitCode == 0) { | ||
log.info("Subtitles downloaded successfully."); | ||
} else { | ||
log.error("Failed to download subtitles."); | ||
} | ||
|
||
return outputFile + MP_4 + DOT + subtitlesLanguage + DOT + SUBTITLES_FORMAT; | ||
} catch (IOException | InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
return outputFile + MP_4 + DOT + subtitlesLanguage + DOT + SUBTITLES_FORMAT; | ||
} | ||
|
||
@Override | ||
public String readSubtitlesFromJsonFile(String filePath) { | ||
try { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
StringBuilder subtitles = new StringBuilder(); | ||
|
||
File jsonFile = new File(filePath); | ||
JsonNode rootNode = objectMapper.readTree(jsonFile); | ||
|
||
// Extract "segs" arrays from "events" | ||
List<JsonNode> segsList = new ArrayList<>(); | ||
JsonNode eventsNode = rootNode.get("events"); | ||
if (eventsNode != null && eventsNode.isArray()) { | ||
for (JsonNode eventNode : eventsNode) { | ||
JsonNode segsNode = eventNode.get("segs"); | ||
if (segsNode != null && segsNode.isArray()) { | ||
// Iterate over the "segs" array and extract the "utf8" values | ||
for (JsonNode segNode : segsNode) { | ||
JsonNode utf8Node = segNode.get("utf8"); | ||
if (utf8Node != null) { | ||
String utf8Value = utf8Node.asText(); | ||
// Check if the "utf8" value contains "\n" | ||
if (Objects.equals(utf8Value, "\n")) { | ||
subtitles.append(" "); | ||
}else{ | ||
subtitles.append(utf8Value); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
log.info("Extracted text: {} ", subtitles); | ||
|
||
return subtitles.toString(); | ||
|
||
} catch (Exception e) { | ||
log.error("Exception occurred while trying to extract subtitles: {}", e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
return ""; | ||
} | ||
|
||
|
||
|
||
} |