Anonymous
Видео -кадм -шоу перевернуто
Сообщение
Anonymous » 25 май 2025, 14:52
Func CreateComposition (с активом: Avasset, Sizeratio: CGPoint, Centerratio: CGPoint) -> (Mixcomposition: AvmutableCoposion, VideoComposition: AvmutableVideOcomposition)? {
let mixcomposition = avmutablecomposion () < /p>
Код: Выделить всё
guard let videoTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid),
let vTrack = asset.tracks(withMediaType: .video).first else {
return nil
}
do {
try videoTrack.insertTimeRange(CMTimeRange(start: .zero, duration: asset.duration), of: vTrack, at: .zero)
} catch {
return nil
}
let videoComposition = AVMutableVideoComposition()
let instruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
// Get video orientation and natural size
let naturalSize = vTrack.naturalSize
let transform = vTrack.preferredTransform
let isPortrait = abs(transform.b) == 1 && abs(transform.c) == 1
let videoSize = isPortrait ? CGSize(width: naturalSize.height, height: naturalSize.width) : naturalSize
// Calculate crop area
let cropWidth = round(videoSize.width * sizeRatio.x)
let cropHeight = round(videoSize.height * sizeRatio.y)
let cropX = videoSize.width * centerRatio.x - cropWidth / 2
let cropY = videoSize.height * centerRatio.y - cropHeight / 2
let oriantation = orientation(from: transform)
var finalTransform : CGAffineTransform = .identity
switch oriantation {
case .down:
finalTransform = finalTransform
.translatedBy(x: 0 - cropX, y: naturalSize.width - cropY)
.rotated(by: -.pi / 2)
case .right:
finalTransform = finalTransform.translatedBy(x: 0 - cropX, y: 0 - cropY)
case .up:
finalTransform = finalTransform
.translatedBy(x: naturalSize.height - cropX, y: 0 - cropY)
.rotated(by: .pi / 2)
case .left:
finalTransform = finalTransform
.translatedBy(x: naturalSize.width - cropX, y: naturalSize.height - cropY)
.rotated(by: -.pi)
case .upMirrored:
finalTransform = finalTransform
.translatedBy(x: naturalSize.height - cropX, y: 0 - cropY)
.rotated(by: .pi / 2)
case .downMirrored:
finalTransform = finalTransform
.translatedBy(x: 0 - cropX, y: naturalSize.width - cropY)
.rotated(by: -.pi / 2)
case .leftMirrored:
finalTransform = finalTransform
.translatedBy(x: naturalSize.width - cropX, y: naturalSize.height - cropY)
.rotated(by: -.pi / 2)
case .rightMirrored:
finalTransform = finalTransform.translatedBy(x: 0 - cropX, y: 0 - cropY)
}
instruction.setTransform(finalTransform, at: .zero)
// Instruction setup
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRange(start: .zero, duration: asset.duration)
mainInstruction.layerInstructions = [instruction]
// Final video composition setup
videoComposition.instructions = [mainInstruction]
videoComposition.renderSize = CGSize(width: cropWidth, height: cropHeight)
videoComposition.frameDuration = CMTimeMake(value: 1, timescale: 30) // Safe fallback
return (mixComposition, videoComposition)
}
enum Orientation {
case up
case down
case left
case right
case upMirrored
case downMirrored
case leftMirrored
case rightMirrored
}
func orientation(from transform: CGAffineTransform) -> Orientation {
// Round small floating-point errors
let a = round(transform.a * 1000) / 1000
let b = round(transform.b * 1000) / 1000
let c = round(transform.c * 1000) / 1000
let d = round(transform.d * 1000) / 1000
switch (a, b, c, d) {
case (0, 1, -1, 0): return .up // 90° rotation
case (0, 1, 1, 0): return .upMirrored // 90° rotation + mirror
case (0, -1, 1, 0): return .down // 270° rotation
case (0, -1, -1, 0): return .downMirrored // 270° rotation + mirror
case (1, 0, 0, 1): return .right // 0° (identity)
case (-1, 0, 0, 1): return .rightMirrored // 0° + mirror horizontally
case (-1, 0, 0, -1): return .left // 180° rotation
case (1, 0, 0, -1): return .leftMirrored // 180° + mirror
default: return .up // fallback
}
}
вот код
Я не могу изменить проблему с переворотом видео, когда ориентация на видео - это зеркало, я не могу рассчитать фактическое преобразование.
Подробнее здесь:
https://stackoverflow.com/questions/796 ... ow-flipped
1748173950
Anonymous
Func CreateComposition (с активом: Avasset, Sizeratio: CGPoint, Centerratio: CGPoint) -> (Mixcomposition: AvmutableCoposion, VideoComposition: AvmutableVideOcomposition)? { let mixcomposition = avmutablecomposion () < /p> [code] guard let videoTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid), let vTrack = asset.tracks(withMediaType: .video).first else { return nil } do { try videoTrack.insertTimeRange(CMTimeRange(start: .zero, duration: asset.duration), of: vTrack, at: .zero) } catch { return nil } let videoComposition = AVMutableVideoComposition() let instruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack) // Get video orientation and natural size let naturalSize = vTrack.naturalSize let transform = vTrack.preferredTransform let isPortrait = abs(transform.b) == 1 && abs(transform.c) == 1 let videoSize = isPortrait ? CGSize(width: naturalSize.height, height: naturalSize.width) : naturalSize // Calculate crop area let cropWidth = round(videoSize.width * sizeRatio.x) let cropHeight = round(videoSize.height * sizeRatio.y) let cropX = videoSize.width * centerRatio.x - cropWidth / 2 let cropY = videoSize.height * centerRatio.y - cropHeight / 2 let oriantation = orientation(from: transform) var finalTransform : CGAffineTransform = .identity switch oriantation { case .down: finalTransform = finalTransform .translatedBy(x: 0 - cropX, y: naturalSize.width - cropY) .rotated(by: -.pi / 2) case .right: finalTransform = finalTransform.translatedBy(x: 0 - cropX, y: 0 - cropY) case .up: finalTransform = finalTransform .translatedBy(x: naturalSize.height - cropX, y: 0 - cropY) .rotated(by: .pi / 2) case .left: finalTransform = finalTransform .translatedBy(x: naturalSize.width - cropX, y: naturalSize.height - cropY) .rotated(by: -.pi) case .upMirrored: finalTransform = finalTransform .translatedBy(x: naturalSize.height - cropX, y: 0 - cropY) .rotated(by: .pi / 2) case .downMirrored: finalTransform = finalTransform .translatedBy(x: 0 - cropX, y: naturalSize.width - cropY) .rotated(by: -.pi / 2) case .leftMirrored: finalTransform = finalTransform .translatedBy(x: naturalSize.width - cropX, y: naturalSize.height - cropY) .rotated(by: -.pi / 2) case .rightMirrored: finalTransform = finalTransform.translatedBy(x: 0 - cropX, y: 0 - cropY) } instruction.setTransform(finalTransform, at: .zero) // Instruction setup let mainInstruction = AVMutableVideoCompositionInstruction() mainInstruction.timeRange = CMTimeRange(start: .zero, duration: asset.duration) mainInstruction.layerInstructions = [instruction] // Final video composition setup videoComposition.instructions = [mainInstruction] videoComposition.renderSize = CGSize(width: cropWidth, height: cropHeight) videoComposition.frameDuration = CMTimeMake(value: 1, timescale: 30) // Safe fallback return (mixComposition, videoComposition) } enum Orientation { case up case down case left case right case upMirrored case downMirrored case leftMirrored case rightMirrored } func orientation(from transform: CGAffineTransform) -> Orientation { // Round small floating-point errors let a = round(transform.a * 1000) / 1000 let b = round(transform.b * 1000) / 1000 let c = round(transform.c * 1000) / 1000 let d = round(transform.d * 1000) / 1000 switch (a, b, c, d) { case (0, 1, -1, 0): return .up // 90° rotation case (0, 1, 1, 0): return .upMirrored // 90° rotation + mirror case (0, -1, 1, 0): return .down // 270° rotation case (0, -1, -1, 0): return .downMirrored // 270° rotation + mirror case (1, 0, 0, 1): return .right // 0° (identity) case (-1, 0, 0, 1): return .rightMirrored // 0° + mirror horizontally case (-1, 0, 0, -1): return .left // 180° rotation case (1, 0, 0, -1): return .leftMirrored // 180° + mirror default: return .up // fallback } } [/code] [b] вот код [/b] Я не могу изменить проблему с переворотом видео, когда ориентация на видео - это зеркало, я не могу рассчитать фактическое преобразование. Подробнее здесь: [url]https://stackoverflow.com/questions/79637630/video-frame-show-flipped[/url]