iosswifturlvideoavasset

Play video backwards


What I want is basically reverse an AVAsset and output video file and I found this link - https://github.com/tempire/ReverseAVAsset/blob/master/AVAsset.swift

This is what I did:

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()
         }

    }


}

But its not working and this is what I end up with:

enter image description here

I think I'm doing something wrong with outputURL, please help if you know what to do


Solution

  • Problem was with output URL, this is the correct way to do

    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()
            }
    
        }
    
    
    }