93 lines
3.0 KiB
Swift
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()
|
|
}
|
|
}
|