Skip to content

fix: sanitize ffprobe input parameters #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import se.svt.oss.mediaanalyzer.mediainfo.MediaInfo
import se.svt.oss.mediaanalyzer.mediainfo.OtherTrack
import se.svt.oss.mediaanalyzer.mediainfo.TextTrack
import se.svt.oss.mediaanalyzer.mediainfo.VideoTrack
import java.util.concurrent.ConcurrentHashMap

private val log = KotlinLogging.logger {}

Expand All @@ -52,15 +53,18 @@ private val log = KotlinLogging.logger {}
)
class MediaAnalyzerService(private val mediaAnalyzer: MediaAnalyzer) {

val ffprobeValidParams = getValidFfprobeParams()

fun analyzeInput(input: Input) {
log.debug { "Analyzing input $input" }
val probeInterlaced = input is VideoIn && input.probeInterlaced
val useFirstAudioStreams = (input as? AudioIn)?.channelLayout?.channels?.size
val ffprobeInputParams = LinkedHashMap(input.params.filterKeys { ffprobeValidParams.contains(it) })

input.analyzed = mediaAnalyzer.analyze(
file = input.uri,
probeInterlaced = probeInterlaced,
ffprobeInputParams = input.params,
ffprobeInputParams = ffprobeInputParams,
).let {
val selectedVideoStream = (input as? VideoIn)?.videoStream
val selectedAudioStream = (input as? AudioIn)?.audioStream
Expand All @@ -75,3 +79,22 @@ class MediaAnalyzerService(private val mediaAnalyzer: MediaAnalyzer) {
}
}
}

fun getValidFfprobeParams(): Set<String> {
val process = ProcessBuilder("ffprobe", "-h")
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().use { it.readText() }
val exitCode = process.waitFor()
if (exitCode != 0) {
log.error { "Failed to get valid ffprobe parameters, ffprobe failed with exit code: $exitCode" }
return emptySet()
}
val result = ConcurrentHashMap.newKeySet<String>()
output.lines().filter { it.startsWith(" -") || it.startsWith("-") }
.forEach { line ->
val param = line.substringAfter("-").substringBefore(" ")
result.add(param)
}
return result
}