I want to launch an ffmpeg process from my code and then process the resulting output. The ffmpeg process will print out progress information and I want to send that info back to another application.
I am trying to use the ProcessBuilder API to achieve this. Based on this answer ProcessBuilder: Forwarding stdout and stderr of started processes without blocking the main thread I have tried the following code
@Service
class ProcessExecutorService(@Autowired private val configProps: AppConfigurationProperties) {
private val logger: Logger = Logger.getLogger(ProcessExecutorService::class.qualifiedName)
fun execute(command: List<String>): Int {
logger.info("Executing command $command")
val processBuilder = ProcessBuilder()
processBuilder.command(command)
processBuilder.directory(File(configProps.encoding.workDirectory))
val process = processBuilder.start()
Thread(StreamGobbler(process.inputStream, logger::info)).start()
return process.waitFor()
}
inner class StreamGobbler(private val inputStream: InputStream, private val consumer: (String) -> Unit): Runnable {
override fun run() {
BufferedReader(InputStreamReader(inputStream) as Reader)
.lines()
.forEach(consumer)
}
}
}
But this doesn't log any output. When I change it to do
@Service
class ProcessExecutorService(@Autowired private val configProps: AppConfigurationProperties) {
private val logger: Logger = Logger.getLogger(ProcessExecutorService::class.qualifiedName)
fun execute(command: List<String>): Int {
logger.info("Executing command $command")
val processBuilder = ProcessBuilder()
processBuilder.command(command)
processBuilder.directory(File(configProps.encoding.workDirectory))
processBuilder.inheritIO()
val process = processBuilder.start()
return process.waitFor()
}
}
then I see the ffmpeg output, but I can't do anything useful with this output.
Any suggestions as to why the first approach might not be working?