Determine video codecs

This commit is contained in:
Christoph Hagen
2025-08-31 18:04:00 +02:00
parent 96bd07bdb7
commit 9848de02cb
9 changed files with 93 additions and 17 deletions

View File

@@ -343,6 +343,62 @@ final class FileResource: Item, LocalizedItem {
}
}
private var _videoType: String?
func videoType() -> String? {
if let _videoType {
return _videoType
}
_videoType = determineVideoType()
return _videoType
}
private func determineVideoType() -> String? {
#warning("TODO: Move ffmpeg path to settings")
switch type {
case .webm:
return "video/webm"
case .mp4, .m4v:
if isExternallyStored {
return "video/mp4"
}
break
default:
return nil
}
return content.storage.with(file: identifier) { path in
let process = Process()
let arguments = "-v error -select_streams v:0 -show_entries stream=codec_tag_string -of default=noprint_wrappers=1:nokey=1 \(path.path())"
.components(separatedBy: " ")
process.launchPath = "/opt/homebrew/bin/ffprobe"
process.arguments = Array(arguments)
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe
process.launch()
process.waitUntilExit()
let outputData = pipe.fileHandleForReading.readDataToEndOfFile()
let outputString = String(data: outputData, encoding: .utf8) ?? ""
if process.terminationStatus != 0 {
print("Failed to determine video type for \(identifier)")
print(outputString)
return nil
}
let firstLine = outputString.components(separatedBy: .newlines).first!.trimmed
guard let type = VideoBlock.SourceType.h265(codec: firstLine) else {
print("Unknown codec type for \(identifier): \(firstLine)")
print(outputString)
return "video/mp4"
}
return type.mimeType
}
}
// MARK: Paths
func removeFileFromOutputFolder() {