Skip to content

Instantly share code, notes, and snippets.

@benkio
Last active March 16, 2024 19:12
Show Gist options
  • Save benkio/6fac65fdaec44842712be716f46169e1 to your computer and use it in GitHub Desktop.
Save benkio/6fac65fdaec44842712be716f46169e1 to your computer and use it in GitHub Desktop.
Convert FFMpeg commands for scaub lorenz tv
#!/usr/bin/env scala-cli
// to import stuff
//> using dep "com.lihaoyi::os-lib:0.9.3"
//> using dep "org.scala-lang.modules::scala-parallel-collections:1.0.4"
// args is the command line input
import scala.collection.parallel.CollectionConverters._
import scala.collection.parallel._
val endingPath = os.home/"temp"/"convertedFilms"
def tempPath(inputFile:String) =
if (inputFile.isEmpty) os.home/"temp" else os.home/"temp"/inputFile
def startingPath(inputFile:String): os.Path =
if (inputFile.isEmpty) os.home/"temp"/"filmToBeConverted" else os.home/"temp"/"filmToBeConverted"/inputFile
def ffmpegCommand(inputFile: String, outputFilename: String): (os.CommandResult, os.Shellable) = {
val command: os.Shellable = os.Shellable(Seq("ffmpeg",
"-i",
startingPath(inputFile).toString,
"-c:v",
"libxvid",
"-b:v",
"1500k",
"-c:a",
"libmp3lame",
"-qscale:a",
"4",
"-vf",
"scale=720:-1",
tempPath(s"$outputFilename.avi").toString))
(os.proc(command).call(check = false), command)
}
def splitCommand(inputFilename: String): (os.CommandResult, os.Shellable) = {
val command: os.Shellable = os.Shellable(Seq(
"ffmpeg",
"-v",
"quiet",
"-y",
"-i",
tempPath(s"$inputFilename.avi").toString,
"-vcodec",
"copy",
"-acodec",
"copy",
"-ss",
"00:00:00",
"-t",
"01:00:00",
"-sn",
tempPath(s"${inputFilename}_1.avi").toString,
"-vcodec",
"copy",
"-acodec",
"copy",
"-ss",
"01:00:00",
"-t",
"01:00:00",
"-sn",
tempPath(s"${inputFilename}_2.avi").toString,
"-vcodec",
"copy",
"-acodec",
"copy",
"-ss",
"02:00:00",
"-t",
"01:00:00",
"-sn",
tempPath(s"${inputFilename}_3.avi").toString
))
(os.proc(command).call(check = false), command)
}
def removeCommand(inputFilename: String): Unit =
os.remove(tempPath(s"$inputFilename.avi"))
def moveCommand(inputFilename: String): Unit =
os.list(tempPath(""))
.filter(p => os.isFile(p) && p.last.startsWith(inputFilename))
.foreach(os.move.into(_, endingPath))
val inputFiles: ParSeq[(String, String)] = List(
("","")
).par
val forkJoinPool = new java.util.concurrent.ForkJoinPool(2)
inputFiles.tasksupport = new ForkJoinTaskSupport(forkJoinPool)
inputFiles.foreach {
case (inputFile, filename) => {
val (ffmpegEC, ffmpegC) = ffmpegCommand(inputFile, filename)
if (ffmpegEC.exitCode == 0) {
val (splitEC, splitC) = splitCommand(filename)
if (splitEC.exitCode == 0)
removeCommand(filename)
moveCommand(filename)
else println(s"split failed for $filename - ${splitC.value.mkString(" ")}")
} else {
println(s"ffmpeg command failed for $inputFile - ${ffmpegC.value.mkString(" ")}")
removeCommand(filename)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment