VPCamera3/tdvideo/tdvideo/PlayContoller10.swift
2024-03-05 11:44:34 +08:00

93 lines
3.0 KiB
Swift

//
// PlayContoller10.swift
// tdvideo
//
// Created by mac on 2024/2/22.
//
import UIKit
import AVFoundation
import AVKit
//
class PlayContoller10: UIViewController {
// URL
var leftEyeVideoURL: URL?
var rightEyeVideoURL: URL?
var outputVideoURL: URL?
//
var player: AVPlayer?
var playerLayer: AVPlayerLayer?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .brown
let path1 = Bundle.main.path(forResource: "aa", ofType: "MOV")
let path2 = Bundle.main.path(forResource: "bb", ofType: "MOV")
leftEyeVideoURL = URL(fileURLWithPath: path1!)
rightEyeVideoURL = URL(fileURLWithPath: path2!)
outputVideoURL = URL.documentsDirectory.appending(path: "output1111.MOV")
setupUI()
}
private func setupUI() {
let generateButton = UIButton(type: .system)
generateButton.setTitleColor(UIColor.white, for: .normal)
generateButton.setTitle("生成空间视频并保存到相册", for: .normal)
generateButton.addTarget(self, action: #selector(generateSpatialVideo), for: .touchUpInside)
generateButton.frame = CGRect(x: 50, y: 200, width: view.frame.width - 100, height: 50)
view.addSubview(generateButton)
//
let playerView = UIView(frame: CGRect(x: 0, y: 300, width: view.frame.width, height: 300))
view.addSubview(playerView)
playerView.backgroundColor = UIColor.black
//
player = AVPlayer()
playerLayer = AVPlayerLayer(player: player)
playerLayer?.frame = playerView.bounds
playerLayer?.backgroundColor = UIColor.black.cgColor
playerView.layer.addSublayer(playerLayer!)
}
@objc private func generateSpatialVideo() {
guard let leftEyeVideoURL = leftEyeVideoURL,
let rightEyeVideoURL = rightEyeVideoURL,
let outputVideoURL = outputVideoURL else {
print("Invalid video URLs")
return
}
let spatialVideoWriter = SpatialVideoWriter()
Task {
spatialVideoWriter.writeSpatialVideo(leftEyeVideoURL: leftEyeVideoURL, rightEyeVideoURL: rightEyeVideoURL, outputVideoURL: outputVideoURL) { success, error in
if success {
print("空间视频生成成功")
self.playSpatialVideo()
} else if let error = error {
print("生成空间视频失败:\(error.localizedDescription)")
}
}
}
}
private func playSpatialVideo() {
guard let outputVideoURL = outputVideoURL else {
print("Output video URL is nil")
return
}
// AVPlayerItem
let playerItem = AVPlayerItem(url: outputVideoURL)
player?.replaceCurrentItem(with: playerItem)
//
player?.play()
}
}