Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
solarisis opened this issue Mar 8, 2018 · 1 comment

Comments

@solarisis
Copy link

solarisis commented Mar 8, 2018

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
        }

    })
}
@rifat072
Copy link

No need to assign timeRange

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants