7

Pracuję nad zadaniem, w którym muszę przyciąć nagrany film wideo z określonego punktu początkowego do określonego punktu końcowego wprowadzonego lub wybranego przez użytkownika. Jak mam to zrobić? Wcześniej używałam UIVideoEditorController, ale nie chcę używać domyślnego widoku i chcę przyciąć wideo bezpośrednio.jak przycinać wideo w szybkim czasie przez określony czas

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!) 

     } 
    }) 

Próbuję osiągnąć mój cel za pomocą tego, ale nie powiodło się.

wiadomość

Błąd:

udało Opcjonalnie (Error Code NSURLErrorDomain Domain = = -1100 "The zapytaniu URL nie został odnaleziony na tym serwerze." UserInfo = {NSErrorFailingURLStringKey = file: /// var/mobile/Containers/Data/Application/E68D3BFD-6923-4EA6-9FB3-C020CE4AA9D4/Documents/moment/jGq_9AUFa47s2ZiiPP4x.mp4, NSErrorFailingURLKey = plik: /// var/mobile/Kontenery/Dane/Aplikacja/E68D3BFD-6923-4EA6- 9FB3-C020CE4AA9D4/Documents/moment/jGq_9AUFa47s2ZiiPP4x.mp4, NSLocalizedDescription = Żądany adres URL nie został znaleziony na tym serwerze ., NSUnderlyingError = 0x1553c220 {Err lub Domain = N

Wystąpił błąd po raz drugi:

powiodło opcjonalne (Błąd Domain = kod NSURLErrorDomain = -3000 "Nie można utworzyć plik" UserInfo = {NSUnderlyingError = 0x14e00000 {Błąd Domain = NSOSStatusErrorDomain Kod = -12124 "(null)"}, NSLocalizedDescription = nie można utworzyć pliku})

+0

jak to się nie udaje? Jaki jest błąd? – hola

+0

Przepuszczam adres URL do nowego parametruURL i zawsze przechodzę do bloku błędu zakończonego niepowodzeniem ... –

+0

Tak, ale * jaki jest komunikat o błędzie *? – Moritz

Odpowiedz

12

znalazłem moje rozwiązanie przy użyciu tej metody i działa jak czar ....

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

Świetna odpowiedź! @Parv Bhasker –

+0

Dzięki to działa, ale wyjście wideo nie ma dźwięku. – user3306145