我想要的基本上是反向AVAsset和输出视频文件,我发现这个链接 - https://github.com/tempire/ReverseAVAsset/blob/master/AVAsset.swift
这就是我做的:
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let path = Bundle.main.path(forResource: "video", ofType: "mov")
let outputUrl = URL(fileURLWithPath: "\(NSUUID().uuidString)video.mov")
let originalAsset = AVAsset(url: URL(fileURLWithPath: path!))
let reversedAsset = originalAsset.reversedAsset(outputUrl)
playerView = AVPlayer(playerItem: AVPlayerItem(asset: reversedAsset!))
playerViewController.player = playerView
present(playerViewController, animated: true) {
self.playerViewController.player?.play()
}
}
}
但它不起作用,这就是我最终的结果:
我想我对outputURL做错了,如果你知道该怎么做,请帮忙
问题在于输出URL,这是正确的方法
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var playerViewController = AVPlayerViewController()
var playerView = AVPlayer()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let path = Bundle.main.path(forResource: "video", ofType: "mov")
//Correct way
var outputURL: URL?
do {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
outputURL = documentsURL.appendingPathComponent("\(NSUUID().uuidString)newvideo.mp4")
}
let originalAsset = AVAsset(url: URL(fileURLWithPath: path!))
let reversedAsset = originalAsset.reversedAsset(outputUrl)
playerView = AVPlayer(playerItem: AVPlayerItem(asset: reversedAsset!))
playerViewController.player = playerView
present(playerViewController, animated: true) {
self.playerViewController.player?.play()
}
}
}