SDAVAssetExportSession icon indicating copy to clipboard operation
SDAVAssetExportSession copied to clipboard

I get error as -[AVAssetWriter startSessionAtSourceTime:] invalid parameter not satisfying: CMTIME_IS_NUMERIC(startTime) when using sdavexportsession

Open solarisis opened this issue 6 years ago • 1 comments

func sdvExport(_ videoURL: URL) { let avAsset1 = AVURLAsset(url: videoURL, options: nil) print("AVURLAsset1 ",avAsset1) let avAsset = AVURLAsset(url: ((videoURL as NSURL) as URL), options: nil) print("AVURLAsset2 ",avAsset) let startDate = Foundation.Date()

    //Create Export session
   // exportSessionSDA = SDAVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough)
    exportSessionSDA = SDAVAssetExportSession(asset: avAsset)

    // exportSession = AVAssetExportSession(asset: composition, presetName: mp4Quality)
    //Creating temp path to save the converted video


    let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let myDocumentPath = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("temp.mp4").absoluteString
    let url = URL(fileURLWithPath: myDocumentPath)

    let documentsDirectory2 = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL

    let filePath = documentsDirectory2.appendingPathComponent("rendered-Video.mp4")
    deleteFile(filePath)

    //Check if the file already exists then remove the previous file
    if FileManager.default.fileExists(atPath: myDocumentPath) {
        do {
            try FileManager.default.removeItem(atPath: myDocumentPath)
        }
        catch let error {
            print(error)
        }
    }



    exportSessionSDA!.outputURL = filePath
    exportSessionSDA!.outputFileType = AVFileTypeMPEG4
    exportSessionSDA!.shouldOptimizeForNetworkUse = true

    exportSessionSDA.videoSettings = [AVVideoCodecKey: AVVideoCodecH264, AVVideoWidthKey: 800, AVVideoHeightKey: 600, AVVideoCompressionPropertiesKey: [AVVideoAverageBitRateKey: 6000000, AVVideoProfileLevelKey: AVVideoProfileLevelH264High40]]
    exportSessionSDA.audioSettings = [AVFormatIDKey: kAudioFormatMPEG4AAC, AVNumberOfChannelsKey: 2, AVSampleRateKey: 44100, AVEncoderBitRateKey: 128000]

    let start = CMTimeMakeWithSeconds(0.0, 0)
    let range = CMTimeRangeMake(start, avAsset.duration)
    exportSessionSDA.timeRange = range

    exportSessionSDA!.exportAsynchronously(completionHandler: {() -> Void in
        switch self.exportSessionSDA!.status {
        case .failed:
            print("%@",self.exportSessionSDA?.error)
        case .cancelled:
            print("Export canceled")
        case .completed:
            //Video conversion finished
            let endDate = Foundation.Date()

            let time = endDate.timeIntervalSince(startDate)
            print(time)
            print("Successful!")
            guard let data = NSData(contentsOf: (self.singleTonFile?.fileUrl)!) else {
                return
            }

            print("File size before compression: \(Double(data.length / 1048576)) mb")
            print("elf.exportSession.outputURL= ",self.exportSessionSDA.outputURL)
            guard let compressedData = NSData(contentsOf: self.exportSessionSDA.outputURL!) else {
                return
            }

            print("File size after compression: \(Double(compressedData.length / 1048576)) mb")

            let when = DispatchTime.now() + 8

            DispatchQueue.main.asyncAfter(deadline: when)
            {
                print("video_duration", self.singleTon.video_duration)
                self.videoUrl() // calling upload
            }
            // self.mediaPath = self.exportSession.outputURL?.path as NSString!
            //  self.singleTon.mediaUrl = URL(self.exportSession.outputURL?.path)
            // print("self.singleTon.mediaUrl ",self.singleTon.mediaUrl)
            //self.mediaPath = String(self.exportSession.outputURL!)
        // self.mediaPath = self.mediaPath.substringFromIndex(7)
        default:
            break
        }

    })
}

solarisis avatar Mar 08 '18 05:03 solarisis

No need to assign timeRange

rifat072 avatar Jan 13 '21 06:01 rifat072