Skip to content

Commit

Permalink
subtitles for multimedia feed working. Mention video split produces f…
Browse files Browse the repository at this point in the history
…rozen frames, no ffmpeg options seem to work
  • Loading branch information
isanvicente committed Sep 15, 2017
1 parent c8f76a3 commit 133a8ef
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions src/main/java/elh/eus/MSM/multimediaElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ public void parseForKeywords(Set<Keyword> kwrds, HashMap<Integer,Pattern> kwrdPa
float step = 5;

// create a temporal mkv file
String converted = getMediaURL().replaceFirst("\\.[^\\.]+$",".mp4");
String converted = getMediaURL();//.replaceFirst("\\.[^\\.]+$",".mp4");

System.err.println("MSM::MultimediaElement::parseForKeywords - converted file path: "+converted);

this.convertStream(getMediaURL(), converted);
//this.convertStream(getMediaURL(), converted);

System.err.println("MSM::MultimediaElement::parseForKeywords - stream "+getMediaURL()
+" converted to "+converted);
Expand Down Expand Up @@ -373,7 +373,10 @@ public void parseForKeywords(Set<Keyword> kwrds, HashMap<Integer,Pattern> kwrdPa
if (result != null && !result.isEmpty())
{
String offset = String.valueOf(splitStart);
// cut the video and get the split containing the current mention
String splitPath = getMentionSplit(converted, splitStart);
// generate subtitles for the splite
createSplitSubtitles(transWords, splitStart, splitEnd, splitPath);
String splitURL = Paths.get(splitPath).getFileName().toString();
//BE CAREFUL: isLocal is hardcoded to true
Mention m = new Mention(this.getLang(),splitText,getEmisionDate(),getOriginURL(),getChannelId(),true,String.valueOf(splitStart),splitURL);
Expand Down Expand Up @@ -487,14 +490,15 @@ private String getMentionSplit(String fullVideoPath, float splitStart) throws IO
.setDuration(10, TimeUnit.SECONDS) //set split duration

.setAudioChannels(1) // Mono audio
.setAudioCodec("copy") // using the aac codec
.setAudioCodec("aac") // using the aac codec
.setAudioSampleRate(48_000) // at 48KHz
.setAudioBitRate(32768) // at 32 kbit/s

.setVideoCodec("copy") // Video using x264
.setVideoCodec("libx264") // Video using x264
.setVideoCopyInkf(true) //copy including previous key frames at the begining.
.setVideoFrameRate(24, 1) // at 24 frames per second
.setVideoResolution(640, 480) // at 640x480 resolution
.setStrict(FFmpegBuilder.Strict.EXPERIMENTAL) // Allow FFmpeg to use experimental specs
.setStrict(FFmpegBuilder.Strict.EXPERIMENTAL) // Allow FFmpeg to use experimental specs
.done();

FFmpegExecutor executor = new FFmpegExecutor(ffmpegWrapper, ffprobeWrapper);
Expand All @@ -505,7 +509,12 @@ private String getMentionSplit(String fullVideoPath, float splitStart) throws IO
}

private void createSplitSubtitles(List<Word> wrds, float s, float e, String url){
String fileUrl = url.replaceFirst("\\.[^\\.]+$", ".vtt");

StringBuilder sb = new StringBuilder();
sb.append("WEBVTT\n\n");
//shift stores position of the split with respect to the whole video, to adapt the subtitles accordingly.
int shift = (int)s;
// words are order by time
for (Word w : wrds){
//if start time is previous to the given start time ignore the word
Expand All @@ -515,15 +524,41 @@ private void createSplitSubtitles(List<Word> wrds, float s, float e, String url)
//if the end time previous to the given end time add the word to the window
if (w.end<=e){
/** TODO add here the proper code to format the subtitle.*/
sb.append(w.form).append(" ");
sb.append(secToTime(w.start,shift)).append(" --> ").append(secToTime(w.end, shift))
.append("\n").append(w.form).append("\n\n");
}
//the end time is posterior to the given end time. End loop and return the window
else {
break;
}
}
FileOutputStream out = new FileOutputStream(new File(url));
out.write(sb.toString().getBytes());
out.close();
try{
FileOutputStream out = new FileOutputStream(new File(fileUrl));
out.write(sb.toString().getBytes());
out.close();
} catch (IOException ioe){
System.err.println("MSM::multimediaElement::creatSplitSubtitles - error when writing to file: "+fileUrl);
}

}

private String secToTime(float sec, int shift) {
float seconds = (sec-shift) % 60;
return String.format("00:00:%06.3f", seconds);
}

private String secToTime2(float sec) {
float seconds = sec % 60;
int minutes = (int)sec / 60;
if (minutes >= 60) {
int hours = minutes / 60;
minutes %= 60;
if( hours >= 24) {
int days = hours / 24;
return String.format("%d days %02d:%02d:%02.3f", days,hours%24, minutes, seconds);
}
return String.format("00:00:%02.3f", seconds);
}
return String.format("00:00:%02.3f", seconds);
}
}

0 comments on commit 133a8ef

Please sign in to comment.