7

Tôi đang làm việc trên một nhiệm vụ mà tôi phải cắt video đã ghi từ điểm bắt đầu cụ thể đến điểm cuối cụ thể như được người dùng nhập hoặc chọn. Tôi phải làm như thế nào. Vì trước đây tôi đã sử dụng UIVideoEditorController nhưng tôi không muốn sử dụng chế độ xem mặc định và tôi muốn cắt trực tiếp video.cách cắt video nhanh chóng trong một thời điểm cụ thể

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

     } 
    }) 

Tôi đang cố gắng đạt được mục tiêu của mình bằng cách sử dụng nhưng không thành công.

nhắn

Lỗi:

không bắt buộc (Lỗi miền = NSURLErrorDomain Mã = -1100 "The URL được yêu cầu không được tìm thấy trên máy chủ này." UserInfo = {NSErrorFailingURLStringKey = file: /// var/di động/Vùng chứa/Dữ liệu/Ứng dụng/E68D3BFD-6923-4EA6-9FB3-C020CE4AA9D4/Tài liệu/thời điểm/jGq_9AUFa47s2ZiiPP4x.mp4, NSErrorFailingURLKey = file: /// var/mobile/Vùng chứa/Dữ liệu/Ứng dụng/E68D3BFD-6923-4EA6- 9FB3-C020CE4AA9D4/Tài liệu/thời điểm/jGq_9AUFa47s2ZiiPP4x.mp4, NSLocalizedDescription = URL được yêu cầu không được tìm thấy trên máy chủ này., NSUnderlyingError = 0x1553c220 {Err hoặc tên miền = N

Lỗi xảy ra lần thứ hai:

không bắt buộc (Lỗi miền = NSURLErrorDomain Mã = -3000 "Không thể tạo tập tin" UserInfo = {NSUnderlyingError = 0x14e00000 {Lỗi miền = NSOSStatusErrorDomain Mã = -12.124 "(null)"}, NSLocalizedDescription = không thể tạo tập tin})

+0

làm thế nào nó không thành công? Lỗi là gì? – hola

+0

tôi đang chuyển url đến tham số newURL và luôn đi vào khối lỗi không thành công ... –

+0

Có nhưng * thông báo lỗi * là gì? – Moritz

Trả lời

12

tôi tìm thấy giải pháp của tôi sử dụng phương pháp này và nó hoạt động như một nét duyên dáng ....

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

Câu trả lời tuyệt vời! @Parv Bhasker –

+0

Cảm ơn nó hoạt động, nhưng video đầu ra không có âm thanh. – user3306145

Các vấn đề liên quan