Skip to content

Instantly share code, notes, and snippets.

@fokot
Last active March 29, 2021 21:07
Show Gist options
  • Save fokot/d6b473b6c3ebcc7183a573daa9f83a97 to your computer and use it in GitHub Desktop.
Save fokot/d6b473b6c3ebcc7183a573daa9f83a97 to your computer and use it in GitHub Desktop.
Sublitles
import scala.io.Source
import java.nio.file.{Files, Paths}
/**
* Fixes file where only end of subtitles are set.
* Set start of next line to the end of previous
*/
object ContinuousSubtitles extends scala.App {
val path = "/home/fokot/Downloads"
val filename = "ss.srt"
val lines = Source.fromFile(s"$path/$filename").getLines().toList
val result = new StringBuilder(lines.size)
var lastEnd = "00:0:00,000"
lines.foreach { l =>
if(l.contains("-->")) {
val Array(_, end) = l.split("-->")
result.appendAll(s"\n$lastEnd --> ${end.trim()}")
lastEnd = end.trim()
} else {
result.appendAll(s"\n$l")
}
}
Files.write(Paths.get(s"$path/ss.srt"), result.result().getBytes)
}
import scala.io.Source
import java.nio.file.{Files, Paths}
/**
* Converts text file to subtitle file
*/
object Subtitles extends scala.App {
val path = "/home/fokot/Downloads"
val filename = "s.srt"
val source = Source.fromFile(s"$path/$filename")
val subtitles = source.getLines().zipWithIndex.map(x => s"${x._2 + 1}\n00:10:00,000 --> 00:10:00,000\n${x._1}").mkString("\n\n")
Files.write(Paths.get(s"$path/ss.srt"), subtitles.getBytes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment