Skip to content

Commit

Permalink
add video to text converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Maleehak committed Apr 14, 2024
1 parent 98a0d8c commit 8349295
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 14 deletions.
2 changes: 1 addition & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version> <!-- Or the latest version available -->
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@SpringBootApplication
@Slf4j
public class BriefMeApplication {
public static void main(String[] args){
public static void main(String[] args) {
SpringApplication.run(BriefMeApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.BriefMe.controllers;

import com.example.BriefMe.service.client.YoutubeVideoSummaryService;
import com.example.BriefMe.service.YoutubeVideoSummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.example.BriefMe.service.impl;
package com.example.BriefMe.service;

import com.example.BriefMe.service.client.AudioExtractor;
import com.example.BriefMe.service.client.AudioToTextConverter;
import com.example.BriefMe.service.client.TextSummarizer;
import com.example.BriefMe.service.client.YoutubeVideoSummaryService;
import com.example.BriefMe.service.client.VideoToTextConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class YoutubeVideoSummaryServiceImpl implements YoutubeVideoSummaryService {
public class YoutubeVideoSummaryService {

@Autowired
AudioExtractor audioExtractor;
Expand All @@ -21,14 +21,25 @@ public class YoutubeVideoSummaryServiceImpl implements YoutubeVideoSummaryServic
@Autowired
TextSummarizer textSummarizer;

@Override
@Autowired
VideoToTextConverter videoToTextConverter;

public String generateSummary(String youtubeVideoUrl, int summarizeIn) {
String audioFile = audioExtractor.extractAudio(youtubeVideoUrl);
String text = audioToTextConverter.covertAudioToText(audioFile);
String summary = textSummarizer.generateSummary(text, summarizeIn);

log.info("Text summarization completed...");
log.info("Text summarization completed using custom text summarizer ...");

return summary;
}

public String generateSummaryFromSubtitles(String youtubeVideoUrl, int summarizeIn) {
String subtitlesFile = videoToTextConverter.fetchSubtitlesJsonFileFromVideo(youtubeVideoUrl);
String subtitles = videoToTextConverter.readSubtitlesFromJsonFile(subtitlesFile);
String summary = textSummarizer.generateSummary(subtitles, summarizeIn);

log.info("Text summarization completed using Goggle generative text summarizer...");
return summary;
}
}
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);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public String extractAudio(String inputVideoFile) {
String[] command = {
"yt-dlp",
"--extract-audio",
"--verbose",
"--default-search",
defaultSearch,
"--audio-format",
Expand Down
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 "";
}



}

0 comments on commit 8349295

Please sign in to comment.