7

Sto lavorando a un'attività in cui devo tagliare il video registrato da un particolare punto di partenza a un particolare punto finale inserito o selezionato dall'utente. Come dovrei farlo. Come ho usato prima UIVideoEditorController ma non voglio usare la vista predefinita e voglio tagliare direttamente il video.come tagliare un video in rapido per un tempo particolare

let FinalUrlTosave = NSURL(string: "\(newURL)") 
    exportSession!.outputURL=FinalUrlTosave 
    exportSession!.shouldOptimizeForNetworkUse = true 
    // exportSession.outputFileType = AVFileTypeQuickTimeMovie 
    exportSession!.outputFileType = AVFileTypeQuickTimeMovie; 
    let start:CMTime 
    let duration:CMTime 
    var st = starttime.doubleValue 
    var ed = endTime.doubleValue 
    start = CMTimeMakeWithSeconds(st, 600) 
    duration = CMTimeMakeWithSeconds(ed, 600) 
    // let timeRangeForCurrentSlice = CMTimeRangeMake(start, duration) 
    let range = CMTimeRangeMake(start, duration); 
    exportSession!.timeRange = range 

     exportSession!.exportAsynchronouslyWithCompletionHandler({ 
     switch exportSession!.status{ 
     case AVAssetExportSessionStatus.Failed: 

      print("failed \(exportSession!.error)") 
     case AVAssetExportSessionStatus.Cancelled: 
      print("cancelled \(exportSession!.error)") 
     default: 
      print("complete....complete") 
      //    self.SaveVideoToPhotoLibrary(destinationURL1!) 

     } 
    }) 

Sto cercando di raggiungere il mio obiettivo utilizzando questo ma non ci riesce.

messaggio

errore:

falliti opzionale (errore di dominio = Codice NSURLErrorDomain = -1100 "Il URL richiesto non è stato trovato su questo server." UserInfo = {NSErrorFailingURLStringKey = file: /// var/mobile/Contenitori/Dati/Applicazione/E68D3BFD-6923-4EA6-9FB3-C020CE4AA9D4/Documenti/momento/jGq_9AUFa47s2ZiiPP4x.mp4, NSErrorFailingURLKey = file: /// var/mobile/Contenitori/Dati/Applicazione/E68D3BFD-6923-4EA6- 9FB3-C020CE4AA9D4/Documents/moment/jGq_9AUFa47s2ZiiPP4x.mp4, NSLocalizedDescription = L'URL richiesto non è stato trovato su questo server ., NSUnderlyingError = 0x1553c220 {Err o Dominio = N

è verificato un errore seconda volta:

falliti opzionale (errore di dominio = Codice NSURLErrorDomain = -3000 "Impossibile creare il file" UserInfo = {NSUnderlyingError = 0x14e00000 {Errore Domain = NSOSStatusErrorDomain Codice = -12.124 "(null)"}, NSLocalizedDescription = Impossibile creare il file})

+0

come si guasta? Qual è l'errore? – hola

+0

Sto passando un url al parametro newURL e va sempre nel blocco errori fallito ... –

+0

Sì, ma * qual è il messaggio di errore *? – Moritz

risposta

12

ho trovato la mia soluzione con questo metodo e funziona come un fascino ....

func cropVideo(sourceURL1: NSURL, statTime:Float, endTime:Float) 
{ 
    let manager = NSFileManager.defaultManager() 

    guard let documentDirectory = try? manager.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true) else {return} 
    guard let mediaType = "mp4" as? String else {return} 
    guard let url = sourceURL1 as? NSURL else {return} 

    if mediaType == kUTTypeMovie as String || mediaType == "mp4" as String { 
     let asset = AVAsset(URL: url) 
     let length = Float(asset.duration.value)/Float(asset.duration.timescale) 
     print("video length: \(length) seconds") 

     let start = statTime 
     let end = endTime 

     var outputURL = documentDirectory.URLByAppendingPathComponent("output") 
     do { 
      try manager.createDirectoryAtURL(outputURL, withIntermediateDirectories: true, attributes: nil) 
      let name = Moment.newName() 
      outputURL = outputURL.URLByAppendingPathComponent("\(name).mp4") 
     }catch let error { 
      print(error) 
     } 

     //Remove existing file 
     _ = try? manager.removeItemAtURL(outputURL) 


     guard let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality) else {return} 
     exportSession.outputURL = outputURL 
     exportSession.outputFileType = AVFileTypeMPEG4 

     let startTime = CMTime(seconds: Double(start ?? 0), preferredTimescale: 1000) 
     let endTime = CMTime(seconds: Double(end ?? length), preferredTimescale: 1000) 
     let timeRange = CMTimeRange(start: startTime, end: endTime) 

     exportSession.timeRange = timeRange 
     exportSession.exportAsynchronouslyWithCompletionHandler{ 
      switch exportSession.status { 
      case .Completed: 
       print("exported at \(outputURL)") 
       self.saveVideoTimeline(outputURL) 
      case .Failed: 
       print("failed \(exportSession.error)") 

      case .Cancelled: 
       print("cancelled \(exportSession.error)") 

      default: break 
      } 
     } 
    } 
} 
+1

Ottima risposta! @Parv Bhasker –

+0

Grazie funziona, ma il video in uscita non ha audio. – user3306145