Skip to content

Commit

Permalink
introduce api
Browse files Browse the repository at this point in the history
  • Loading branch information
Maleehak committed Apr 11, 2024
1 parent b09986a commit 7bfc3e2
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 37 deletions.
13 changes: 0 additions & 13 deletions backend/src/main/java/com/example/BriefMe/BriefMeApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,5 @@ public class BriefMeApplication {

public static void main(String[] args) throws IOException {
SpringApplication.run(BriefMeApplication.class, args);

AudioExtractor audioExtractor = new YoutubeAudioExtractorImpl();
String audioFile = audioExtractor.extractAudio("https://www.youtube.com/watch?v=Fkd9TWUtFm0");

AudioToTextConverter audioToTextConverter = new AudioToTextConverterImpl();
String text = audioToTextConverter.covertAudioToText(audioFile);

TextSummarizer textSummarizer = new CustomTextSummarizer();
int numberOfLines = 5;
String summary = textSummarizer.generateSummary(text, numberOfLines);

log.info("Text summarization completed...");
log.info("Summary: {}", summary);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.BriefMe.controllers;

import com.example.BriefMe.service.client.YoutubeVideoSummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class YoutubeVideoSummaryController {

@Autowired
YoutubeVideoSummaryService youtubeVideoSummaryService;

@GetMapping("/get-summary")
public ResponseEntity<String> generateSummary(@RequestParam String video, @RequestParam int lines){
String summary = youtubeVideoSummaryService.generateSummary(video, lines);
return ResponseEntity.ok(summary);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import java.io.IOException;

public interface AudioExtractor {
String extractAudio(String inputVideo) throws IOException;
String extractAudio(String inputVideo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import java.io.IOException;

public interface AudioToTextConverter {
String covertAudioToText(String audioFile) throws IOException;
String covertAudioToText(String audioFile);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.BriefMe.service.client;

public interface YoutubeVideoSummaryService {
String generateSummary(String youtubeVideoUrl, int summarizeIn);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AudioToTextConverterImpl implements AudioToTextConverter {

public String covertAudioToText(String audioFile) throws IOException {
public String covertAudioToText(String audioFile){

try (SpeechClient speechClient = SpeechClient.create()) {

log.info("Loading the audio file into memory...");

Path path = Paths.get(audioFile);
Path path = Path.of(audioFile);
byte[] data = Files.readAllBytes(path);
log.info("Audio file loaded...");

Expand Down Expand Up @@ -52,8 +54,14 @@ public String covertAudioToText(String audioFile) throws IOException {

executor.shutdown();

log.info("Audio conversion to text completed. Transcript: {}", completeTranscript);
log.info("Audio conversion to text completed");

Files.deleteIfExists(path);

return completeTranscript.toString();
} catch (IOException e) {
log.error("Exception occurred with audio to text conversion: {}", e.getMessage());
return "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.util.stream.IntStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.similarity.CosineSimilarity;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CustomTextSummarizer implements TextSummarizer {

private static final int MAX_ITERATIONS = 50;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
package com.example.BriefMe.service.impl;

import com.example.BriefMe.service.client.AudioExtractor;
import java.io.IOException;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import net.bramp.ffmpeg.FFmpeg;
import net.bramp.ffmpeg.FFmpegExecutor;
import net.bramp.ffmpeg.FFprobe;
import net.bramp.ffmpeg.builder.FFmpegBuilder;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class Mp4AudioExtractorImpl implements AudioExtractor {

public static final String OUTPUT_FILE = "output-";
public static final String DOT = ".";
public static final String MP_3 = "mp3";

@Override
public String extractAudio(String inputVideoFile) throws IOException {
FFmpeg ffmpeg = new FFmpeg("/opt/homebrew/bin/ffmpeg");
FFprobe ffprobe = new FFprobe();

String outputFile = OUTPUT_FILE + UUID.randomUUID() + DOT + MP_3;

FFmpegBuilder builder = new FFmpegBuilder()
.setInput(inputVideoFile)
.overrideOutputFiles(true)
.addOutput(outputFile)
.setFormat(MP_3)
.done();

FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
executor.createJob(builder).run();

return outputFile;
public String extractAudio(String inputVideoFile) {
try{
FFmpeg ffmpeg = new FFmpeg("/opt/homebrew/bin/ffmpeg");
FFprobe ffprobe = new FFprobe();

String outputFile = OUTPUT_FILE + UUID.randomUUID() + DOT + MP_3;

FFmpegBuilder builder = new FFmpegBuilder()
.setInput(inputVideoFile)
.overrideOutputFiles(true)
.addOutput(outputFile)
.setFormat(MP_3)
.done();

FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
executor.createJob(builder).run();

return outputFile;
}catch(Exception e){
log.error("Unable to extract audio from the link. Exception occured. {}", e.getMessage());
return "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
import java.io.InputStreamReader;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Primary
public class YoutubeAudioExtractorImpl implements AudioExtractor {

public static final String OUTPUT_FILE = "output-";
public static final String DOT = ".";
public static final String MP_3 = "mp3";

@Override
public String extractAudio(String inputVideoFile) throws IOException {
public String extractAudio(String inputVideoFile) {
String outputFile = OUTPUT_FILE + UUID.randomUUID() + DOT;
String defaultSearch = "ytsearch";
String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.BriefMe.service.impl;

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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class YoutubeVideoSummaryServiceImpl implements YoutubeVideoSummaryService {

@Autowired
AudioExtractor audioExtractor;

@Autowired
AudioToTextConverter audioToTextConverter;

@Autowired
TextSummarizer textSummarizer;

@Override
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...");

return summary;
}
}

0 comments on commit 7bfc3e2

Please sign in to comment.