Tone-map HDR before stabilization and prioritize stable conservative framing
This commit is contained in:
1 parent
031dc50b99
commit
a4919bedd0
8 files changed
+113
-61
No files matched your search
@@ -186,15 +186,8 @@ struct ContentView: View {
|
||||
.help("Stärkere Werte glätten mehr Bewegung. GrowthLapse hält den Sicherheits-Zoom dabei konstant, damit das Bild nicht pumpt.")
|
||||
.disabled(!settings.stabilizationEnabled)
|
||||
|
||||
Toggle("Auf Hintergrundpunkt fokussieren", isOn: $settings.stabilizationAnchorEnabled)
|
||||
.help("Beschränkt nur die VidStab-Analyse auf einen Bereich um einen festen Hintergrundpunkt. Der Transform bleibt auf dem vollen Bild.")
|
||||
.disabled(!settings.stabilizationEnabled || settings.stabilizationMethod != .vidstab)
|
||||
|
||||
if settings.stabilizationAnchorEnabled {
|
||||
percentSlider("Punkt X", value: $settings.stabilizationAnchorX, range: 0...1, help: "Horizontale Position des festen Hintergrundpunkts im Bild. 0% links, 100% rechts.")
|
||||
percentSlider("Punkt Y", value: $settings.stabilizationAnchorY, range: 0...1, help: "Vertikale Position des festen Hintergrundpunkts im Bild. 0% oben, 100% unten.")
|
||||
percentSlider("Analysebereich", value: $settings.stabilizationAnchorSize, range: 0.15...0.80, help: "Größe des Bereichs um den Hintergrundpunkt, den VidStab zur Bewegungsanalyse verwendet.")
|
||||
}
|
||||
Text("Kamerabewegung wird im ganzen Bild ermittelt. Der Gesichtsausschnitt bleibt innerhalb jedes Clips fest.")
|
||||
.font(.caption).foregroundStyle(.secondary)
|
||||
}
|
||||
.disabled(processor.state.isRunning)
|
||||
|
||||
@@ -214,13 +207,13 @@ struct ContentView: View {
|
||||
.help("Analysiert das Originalvideo vor dem Schneiden und sucht das Fenster, in dem Gesicht und beide Augen am besten sichtbar sind. Wenn nichts Brauchbares gefunden wird, nutzt GrowthLapse die normale Mitte/Start-Offset-Logik.")
|
||||
|
||||
Toggle("Gemeinsame Gesichtsausrichtung", isOn: $settings.continuityAlignmentEnabled)
|
||||
.help("Ermittelt für den gesamten Film eine gemeinsame Gesichtsgröße und Augenlinie. Unterschiedliche Kameraabstände werden durch den Ausschnitt ausgeglichen; fehlende Bildränder oder sehr kleine Gesichter werden markiert.")
|
||||
.help("Verwendet deine Zielgröße ohne automatische Vergrößerung für Nahaufnahmen. Ein fester Ausschnitt mit Kopfrand hat Vorrang; zusätzlicher Gesichtszoom ist auf 12 % begrenzt.")
|
||||
.disabled(!settings.faceNormalizationEnabled)
|
||||
percentSlider("Augenlinie von oben", value: $settings.targetEyeY, range: 0.25...0.6,
|
||||
help: "Gemeinsame vertikale Position der Augen im Ergebnis. 40 % liegt etwas über der Bildmitte. Gilt nur, soweit genug Bildrand vorhanden ist.")
|
||||
.disabled(!settings.faceNormalizationEnabled)
|
||||
Toggle("Gesichtsgröße normalisieren", isOn: $settings.faceNormalizationEnabled)
|
||||
.help("Verfolgt Gesicht und Augen mit Apple Vision und bewegt den Bildausschnitt geglättet mit. Das Bild bleibt immer vollflächig; fehlt Rand für die Zielgröße oder Drehung, hat ein randloses Bild Vorrang.")
|
||||
.help("Nutzt Apple Vision für einen festen, zurückhaltenden Ausschnitt pro Clip. Bewegt den Ausschnitt nicht mit dem Kopf und dreht keine Kopfneigung gerade. Entwacklung und Platz um den Kopf haben Vorrang.")
|
||||
|
||||
HStack {
|
||||
Text("Ziel-Gesichtshöhe")
|
||||
|
||||
@@ -1320,6 +1320,7 @@ final class VideoProcessor: ObservableObject {
|
||||
let stabilizedURL = tempDirectory.appendingPathComponent(String(format: "stabilized_%04d.mp4", clipNumber))
|
||||
try await extractSegment(
|
||||
source: video.url,
|
||||
sourceIsHDR: sourceIsHDR,
|
||||
output: segmentURL,
|
||||
start: safeStart,
|
||||
duration: actualSegmentLength,
|
||||
@@ -1365,7 +1366,7 @@ final class VideoProcessor: ObservableObject {
|
||||
clipIndex: projectClip.index,
|
||||
ffmpeg: ffmpeg,
|
||||
sourceHasAudio: sourceHasAudio,
|
||||
sourceIsHDR: sourceIsHDR,
|
||||
sourceIsHDR: false,
|
||||
clipNumber: clipNumber,
|
||||
totalClips: videos.count
|
||||
)
|
||||
@@ -1422,6 +1423,7 @@ final class VideoProcessor: ObservableObject {
|
||||
|
||||
private func extractSegment(
|
||||
source: URL,
|
||||
sourceIsHDR: Bool,
|
||||
output: URL,
|
||||
start: Double,
|
||||
duration: Double,
|
||||
@@ -1441,7 +1443,9 @@ final class VideoProcessor: ObservableObject {
|
||||
"-i", source.path,
|
||||
"-map", "0:v:0"
|
||||
]
|
||||
arguments.append(contentsOf: videoEncoderArguments(settings))
|
||||
// Tone-map at source precision, before any 8-bit encoder or stabilization.
|
||||
arguments.append(contentsOf: ["-vf", sdrFilters(sourceIsHDR: sourceIsHDR).joined(separator: ",")])
|
||||
arguments.append(contentsOf: ["-c:v", "libx264", "-crf", "12", "-preset", "fast", "-pix_fmt", "yuv420p"])
|
||||
if preserveAudio {
|
||||
arguments.append(contentsOf: ["-map", "0:a:0", "-c:a", "aac", "-b:a", "192k"])
|
||||
} else {
|
||||
@@ -1545,7 +1549,7 @@ final class VideoProcessor: ObservableObject {
|
||||
progressText = "VidStab Analyse \(clipNumber) von \(totalClips)"
|
||||
appendLog("VidStab Analyse nur auf Segment \(segment.lastPathComponent) -> \(transformFileName)")
|
||||
if settings.stabilizationAnchorEnabled {
|
||||
appendLog("VidStab Hintergrund-Anker: X \(Int(settings.stabilizationAnchorX * 100))%, Y \(Int(settings.stabilizationAnchorY * 100))%, Bereich \(Int(settings.stabilizationAnchorSize * 100))%")
|
||||
appendLog("Alter Hintergrund-Anker wird durch Vollbildanalyse ersetzt; Analyse und Transform verwenden dieselben Bildkoordinaten.")
|
||||
}
|
||||
let detectArguments = [
|
||||
"-y",
|
||||
@@ -1968,7 +1972,7 @@ final class VideoProcessor: ObservableObject {
|
||||
|
||||
private func renderFingerprint(settings: RenderSettings, targetDimensions: VideoDimensions) -> String {
|
||||
[
|
||||
"continuity-v1",
|
||||
"continuity-v2-sdr-first-static-framing",
|
||||
"\(settings.continuityAlignmentEnabled)",
|
||||
"\(settings.colorMatchingEnabled)",
|
||||
decimal(settings.colorMatchingStrength),
|
||||
@@ -2071,18 +2075,9 @@ final class VideoProcessor: ObservableObject {
|
||||
let strength = settings.stabilizationStrength
|
||||
let detect = "vidstabdetect=shakiness=\(strength.shakiness):accuracy=\(strength.accuracy):result=\(transformFileName)"
|
||||
|
||||
guard settings.stabilizationAnchorEnabled else {
|
||||
return detect
|
||||
}
|
||||
|
||||
let width = max(2, evenDimension(Int(Double(video.width) * settings.stabilizationAnchorSize)))
|
||||
let height = max(2, evenDimension(Int(Double(video.height) * settings.stabilizationAnchorSize)))
|
||||
let centerX = Double(video.width) * settings.stabilizationAnchorX
|
||||
let centerY = Double(video.height) * settings.stabilizationAnchorY
|
||||
let x = evenDimension(Int(clamp(centerX - Double(width) / 2, min: 0, max: Double(max(0, video.width - width)))))
|
||||
let y = evenDimension(Int(clamp(centerY - Double(height) / 2, min: 0, max: Double(max(0, video.height - height)))))
|
||||
|
||||
return "crop=\(width):\(height):\(x):\(y),\(detect)"
|
||||
// Detect and transform in the same full-frame coordinate system.
|
||||
// Cropping detection alone gives rotations the wrong center on the full frame.
|
||||
return detect
|
||||
}
|
||||
|
||||
private func clamp(_ value: Double, min minimum: Double, max maximum: Double) -> Double {
|
||||
@@ -2099,7 +2094,7 @@ final class VideoProcessor: ObservableObject {
|
||||
sourceIsHDR: Bool,
|
||||
colorAdjustment: ColorAdjustment? = nil
|
||||
) -> String {
|
||||
var filters: [String] = []
|
||||
var filters = sdrFilters(sourceIsHDR: sourceIsHDR)
|
||||
if let alignment = faceAlignment {
|
||||
if abs(alignment.rotationRadians) > 0.001 {
|
||||
filters.append("rotate=\(decimal(alignment.rotationRadians)):ow=iw:oh=ih:fillcolor=black")
|
||||
@@ -2119,7 +2114,6 @@ final class VideoProcessor: ObservableObject {
|
||||
filters.append("scale=\(targetDimensions.width):\(targetDimensions.height):force_original_aspect_ratio=decrease")
|
||||
filters.append("pad=\(targetDimensions.width):\(targetDimensions.height):(ow-iw)/2:(oh-ih)/2")
|
||||
filters.append("setsar=1")
|
||||
filters.append(contentsOf: sdrFilters(sourceIsHDR: sourceIsHDR))
|
||||
if let colorAdjustment { filters.append(colorAdjustment.filter) }
|
||||
if burnInClipNumber {
|
||||
filters.append(clipNumberOverlayFilter(clipIndex))
|
||||
@@ -2365,6 +2359,10 @@ final class VideoProcessor: ObservableObject {
|
||||
"zscale=primaries=bt709",
|
||||
"tonemap=tonemap=hable:desat=0",
|
||||
"zscale=transfer=bt709:matrix=bt709:range=tv",
|
||||
"format=yuv444p16le",
|
||||
// Tiny nonzero luma values avoid hqdn3d's automatic luma defaults.
|
||||
"hqdn3d=0.0001:3:0.0001:4.5",
|
||||
"zscale=dither=error_diffusion",
|
||||
"format=yuv420p",
|
||||
"setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709"
|
||||
]
|
||||
@@ -3174,20 +3172,18 @@ private enum FaceAlignmentAnalyzer {
|
||||
}
|
||||
|
||||
let outputAspect = Double(targetDimensions.width) / Double(targetDimensions.height)
|
||||
var cropHeight = faceHeightPixels / targetFaceHeightRatio
|
||||
var cropWidth = cropHeight * outputAspect
|
||||
if cropWidth > Double(imageWidth) {
|
||||
cropWidth = Double(imageWidth)
|
||||
cropHeight = cropWidth / outputAspect
|
||||
}
|
||||
if cropHeight > Double(imageHeight) {
|
||||
cropHeight = Double(imageHeight)
|
||||
cropWidth = cropHeight * outputAspect
|
||||
}
|
||||
let fullHeight = min(Double(imageHeight), Double(imageWidth) / outputAspect)
|
||||
let headTop = max(0, samples.map { (1 - $0.rect.maxY - $0.rect.height * 0.4) * Double(imageHeight) }.min() ?? 0)
|
||||
let headBottom = min(Double(imageHeight), samples.map { (1 - $0.rect.minY + $0.rect.height * 0.12) * Double(imageHeight) }.max() ?? Double(imageHeight))
|
||||
let headLeft = max(0, samples.map { ($0.rect.minX - $0.rect.width * 0.2) * Double(imageWidth) }.min() ?? 0)
|
||||
let headRight = min(Double(imageWidth), samples.map { ($0.rect.maxX + $0.rect.width * 0.2) * Double(imageWidth) }.max() ?? Double(imageWidth))
|
||||
let neededHeight = max(headBottom - headTop, (headRight - headLeft) / outputAspect)
|
||||
let cropHeight = min(fullHeight, max(fullHeight / 1.12, faceHeightPixels / targetFaceHeightRatio, neededHeight))
|
||||
let cropWidth = cropHeight * outputAspect
|
||||
let evenCropWidth = max(2, even(Int(cropWidth.rounded(.down))))
|
||||
let evenCropHeight = max(2, even(Int(cropHeight.rounded(.down))))
|
||||
let eyeAngles = samples.compactMap(\.eyeAngle)
|
||||
let proposedRotation = clamp(-median(eyeAngles), min: -0.14, max: 0.14)
|
||||
let proposedRotation: Double = 0 // Head tilt is not camera shake; leave leveling to stabilization.
|
||||
let safeFrame = safeFrameAfterRotation(
|
||||
imageWidth: imageWidth,
|
||||
imageHeight: imageHeight,
|
||||
@@ -3226,13 +3222,17 @@ private enum FaceAlignmentAnalyzer {
|
||||
y: clamp(anchor.y - Double(evenCropHeight) * desiredY, min: minY, max: maxY)
|
||||
)
|
||||
}
|
||||
let positions = smoothed(
|
||||
rawPositions,
|
||||
minX: minX,
|
||||
maxX: maxX,
|
||||
minY: minY,
|
||||
maxY: maxY
|
||||
)
|
||||
// A constant crop does not chase head movement or undo camera stabilization.
|
||||
let headMinX = max(minX, headRight - Double(evenCropWidth))
|
||||
let headMaxX = min(maxX, headLeft)
|
||||
let headMinY = max(minY, headBottom - Double(evenCropHeight))
|
||||
let headMaxY = min(maxY, headTop)
|
||||
let preferredX = median(rawPositions.map(\.x))
|
||||
let preferredY = median(rawPositions.map(\.y))
|
||||
let fixedX = headMinX <= headMaxX ? clamp(preferredX, min: headMinX, max: headMaxX) : clamp(preferredX, min: minX, max: maxX)
|
||||
let fixedY = headMinY <= headMaxY ? clamp(preferredY, min: headMinY, max: headMaxY) : clamp(preferredY, min: minY, max: maxY)
|
||||
boundaryLimited = boundaryLimited || headMinX > headMaxX || headMinY > headMaxY
|
||||
let positions = [FaceAlignmentSample(time: 0, x: fixedX, y: fixedY)]
|
||||
|
||||
return FaceAlignment(
|
||||
sourceWidth: imageWidth,
|
||||
|
||||
@@ -189,16 +189,8 @@ enum ContinuityPlanner {
|
||||
|
||||
static func sharedProfile(clips: [GrowthLapseProjectClip], dimensions: VideoDimensions,
|
||||
requested: Double, eyeY: Double) -> ContinuityProfile {
|
||||
let aspect = Double(dimensions.width) / Double(dimensions.height)
|
||||
let required = clips.compactMap { clip -> Double? in
|
||||
let poses = clip.visualAnalysis?.window(start: clip.segmentStart, length: clip.segmentLength).compactMap(\.pose) ?? []
|
||||
guard !poses.isEmpty else { return nil }
|
||||
let sourceAspect = Double(clip.width) / Double(clip.height)
|
||||
// Reserve a small border for leveling the eye line. Outliers are flagged, not imposed on all clips.
|
||||
return median(poses.map(\.height)) / (min(1, sourceAspect / aspect) * 0.94)
|
||||
}.sorted()
|
||||
let feasible = required.isEmpty ? requested : required[min(required.count - 1, Int(ceil(Double(required.count - 1) * 0.8)))]
|
||||
return ContinuityProfile(faceHeightRatio: max(requested, min(requested + 0.12, feasible)), eyeY: eyeY)
|
||||
// Framing must not enlarge every clip to accommodate close-up outliers.
|
||||
return ContinuityProfile(faceHeightRatio: requested, eyeY: eyeY)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user