在 AVFoundation 中镜像视频后,下载的视频未镜像
Downloaded video not mirrored after mirroring video in AVFoundation
一段时间以来,我一直在尝试镜像我的视频,但我似乎无法修复它。我也尝试了以下 。
if let connection = movieFileOutput.connection(with: .video) {
if connection.isVideoMirroringSupported {
connection.isVideoMirrored = self.videoDeviceInput?.device.position == .front
}
}
if let connection = videoDataOutput.connection(with: .video) {
if connection.isVideoMirroringSupported {
connection.isVideoMirrored = self.videoDeviceInput?.device.position == .front
}
}
视频在应用程序中得到了镜像,但是当将视频下载到我的照片应用程序时,它又没有镜像了。我该如何解决这个问题?
我的整体代码看起来如何:
automaticallyConfiguresApplicationAudioSession
设置为 false
.
- 通过获取设备输入并将其添加到
AVCaptureSession
来配置相机
- 设置电影输出,如设置比特率和 iPad 配置方向。这里我还要设置
.isVideoMirrored
属性.
- 正在设置会话预设。
添加音频会话设置:
guard let audioDevice = AVCaptureDevice.default(
.builtInMicrophone,
for: .audio,
position: .unspecified
) else { return }
guard let audioInput = try? AVCaptureDeviceInput(device: audioDevice),
captureSession.canAddInput(audioInput) else { return }
guard captureSession.inputs.contains(audioInput) == false else { return }
captureSession.addInput(audioInput)
我用这个变通方法解决了这个问题,看看它是否有用:
extension CameraViewController: AVCaptureFileOutputRecordingDelegate {
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
if error != nil {
print("Error recording movie: \(error!.localizedDescription)")
} else {
processMovie()
}
}
func processMovie() {
let asset = AVAsset(url: CameraViewController.movieURL)
let composition = AVMutableComposition()
let assetVideoTrack = asset.tracks(withMediaType: .video).last!
let compositionVideoTrack = composition.addMutableTrack(withMediaType: AVMediaType.video,
preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
try? compositionVideoTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: asset.duration),
of: assetVideoTrack,
at: CMTime.zero)
if self.currentCameraPosition == .rear {
compositionVideoTrack?.preferredTransform = assetVideoTrack.preferredTransform
}
if self.currentCameraPosition == .front {
compositionVideoTrack?.preferredTransform = CGAffineTransform(scaleX: -1.0, y: 1.0).rotated(by: CGFloat(Double.pi/2))
}
if let exporter = AVAssetExportSession(asset: composition,
presetName: AVAssetExportPresetHighestQuality) {
exporter.outputURL = CameraViewController.exportMovieURL
exporter.outputFileType = AVFileType.mov
exporter.shouldOptimizeForNetworkUse = true
exporter.exportAsynchronously() {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "ShowVideo", sender: nil)
}
}
}
}
}
一段时间以来,我一直在尝试镜像我的视频,但我似乎无法修复它。我也尝试了以下
if let connection = movieFileOutput.connection(with: .video) {
if connection.isVideoMirroringSupported {
connection.isVideoMirrored = self.videoDeviceInput?.device.position == .front
}
}
if let connection = videoDataOutput.connection(with: .video) {
if connection.isVideoMirroringSupported {
connection.isVideoMirrored = self.videoDeviceInput?.device.position == .front
}
}
视频在应用程序中得到了镜像,但是当将视频下载到我的照片应用程序时,它又没有镜像了。我该如何解决这个问题?
我的整体代码看起来如何:
automaticallyConfiguresApplicationAudioSession
设置为false
.- 通过获取设备输入并将其添加到
AVCaptureSession
来配置相机
- 设置电影输出,如设置比特率和 iPad 配置方向。这里我还要设置
.isVideoMirrored
属性. - 正在设置会话预设。
添加音频会话设置:
guard let audioDevice = AVCaptureDevice.default(
.builtInMicrophone,
for: .audio,
position: .unspecified
) else { return }
guard let audioInput = try? AVCaptureDeviceInput(device: audioDevice),
captureSession.canAddInput(audioInput) else { return }
guard captureSession.inputs.contains(audioInput) == false else { return }
captureSession.addInput(audioInput)
我用这个变通方法解决了这个问题,看看它是否有用:
extension CameraViewController: AVCaptureFileOutputRecordingDelegate {
func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
if error != nil {
print("Error recording movie: \(error!.localizedDescription)")
} else {
processMovie()
}
}
func processMovie() {
let asset = AVAsset(url: CameraViewController.movieURL)
let composition = AVMutableComposition()
let assetVideoTrack = asset.tracks(withMediaType: .video).last!
let compositionVideoTrack = composition.addMutableTrack(withMediaType: AVMediaType.video,
preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
try? compositionVideoTrack?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: asset.duration),
of: assetVideoTrack,
at: CMTime.zero)
if self.currentCameraPosition == .rear {
compositionVideoTrack?.preferredTransform = assetVideoTrack.preferredTransform
}
if self.currentCameraPosition == .front {
compositionVideoTrack?.preferredTransform = CGAffineTransform(scaleX: -1.0, y: 1.0).rotated(by: CGFloat(Double.pi/2))
}
if let exporter = AVAssetExportSession(asset: composition,
presetName: AVAssetExportPresetHighestQuality) {
exporter.outputURL = CameraViewController.exportMovieURL
exporter.outputFileType = AVFileType.mov
exporter.shouldOptimizeForNetworkUse = true
exporter.exportAsynchronously() {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "ShowVideo", sender: nil)
}
}
}
}
}