向后播放视频
Play video backwards
我想要的基本上是反转 AVAsset 和输出视频文件,我发现了这个 link -
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()
}
}
}
我想要的基本上是反转 AVAsset 和输出视频文件,我发现了这个 link - 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()
}
}
}