无法使用 AVFoundation 将录制的视频上传到服务器 - Swift 3

Posted

技术标签:

【中文标题】无法使用 AVFoundation 将录制的视频上传到服务器 - Swift 3【英文标题】:Not able to upload recorded video to server using AVFoundation - Swift 3 【发布时间】:2017-01-12 11:19:49 【问题描述】:

我正在使用 AVFoundation 录制视频并将其发送到服务器。每当服务器使用我录制的视频时,它都会返回内部服务器错误。但是当我上传一些不是我录制的虚拟视频时,它上传成功。在下面添加代码。

let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
        let logsPath = documentsPath.appendingPathComponent(GlobalVar.interVCode)


    if !FileManager.default.fileExists(atPath: (logsPath?.absoluteString)!)
    do 
        try FileManager.default.createDirectory(at: logsPath!, withIntermediateDirectories: true, attributes: nil)
                    catch let error as NSError 
        NSLog("Unable to create directory \(error.debugDescription)")
    
    
     let outputURL: URL = (self.applicationDocumentsDirectory().appendingPathComponent(GlobalVar.interVCode)?.appendingPathComponent("\(questionId)").appendingPathExtension("mp4"))!


 self.camera.startRecording(withOutputUrl: outputURL, didRecord: 

(camera, outputURL, error) in

    )

使用自定义相机控制器LLSimpleCamera开始录制。下面是开始录制的代码。

     - (void)startRecordingWithOutputUrl:(NSURL *)url didRecord:(void (^)(LLSimpleCamera *camera, NSURL *outputFileUrl, NSError *error))completionBlock

    // check if video is enabled
     if(!self.videoEnabled) 
        NSError *error = [NSError errorWithDomain:LLSimpleCameraErrorDomain
                                                 code:LLSimpleCameraErrorCodeVideoNotEnabled
                                         userInfo:nil];
        [self passError:error];
        return;
    

    if(self.flash == LLCameraFlashOn) 
        [self enableTorch:YES];
    

    // set video orientation
    for(AVCaptureConnection *connection in [self.movieFileOutput connections]) 
        for (AVCaptureInputPort *port in [connection inputPorts]) 
            // get only the video media types
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) 
                if ([connection isVideoOrientationSupported]) 
                    [connection setVideoOrientation:[self orientationForConnection]];
                
            
        
    

    self.didRecordCompletionBlock = completionBlock;

    [self.movieFileOutput startRecordingToOutputFileURL:url recordingDelegate:self];

非常感谢您的帮助。

【问题讨论】:

【参考方案1】:

我正在使用 UIImagePickerController 进行录制和上传到服务器

    if   UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)

    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    imagePicker.mediaTypes = [kUTTypeMovie as NSString as String]
    imagePicker.allowsEditing = false
    imagePicker.videoMaximumDuration = TimeInterval(10.0)

    self.present(imagePicker, animated: true, completion: nil)
    

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

    picker .dismiss(animated: true, completion: nil)



    let mediaType = info[UIImagePickerControllerMediaType] as! NSString

    if mediaType == kUTTypeMovie 
        guard let path = (info[UIImagePickerControllerMediaURL] as! NSURL).path else  return 
        if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path) 
 if let fileURL = info[UIImagePickerControllerMediaURL] as? NSURL 
                if let videoData = NSData(contentsOf: fileURL as URL) 
                    print(videoData.length)

                    let fileManager = FileManager.default
                    let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("xyz.mov")
                    fileManager.createFile(atPath: paths as String, contents: videoData as Data, attributes: nil)
                
            


        
    else
  

 

使用方法从文档目录中获取视频数据并上传到服务器

func uploadVideo(_ userDetails : [String : AnyObject]) 

    let imagePAth = URL(fileURLWithPath:(self.getDirectoryPath() as NSString).appendingPathComponent("xyz.mov") )

    //let videoData = UIImage(contentsOfFile: imagePAth)!

    do let data = try Data(contentsOf: imagePAth)

        print(data)
  catch 
        print(error)
    




【讨论】:

虽然转换为NSData的00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00>打印该..与虚拟视频它表示一些其它值。跨度>

以上是关于无法使用 AVFoundation 将录制的视频上传到服务器 - Swift 3的主要内容,如果未能解决你的问题,请参考以下文章

Objective-C:在 iOS 上使用 AVFoundation 录制视频时静音/取消静音

从 AVFoundation 保存录制的视频

使用具有自定义尺寸的 AVFoundation 录制视频? [复制]

Swift 3:如何在使用 AVFoundation 录制视频期间将麦克风静音/取消静音

Swift 4 - 在 mac os 上使用 AVAssetWriter 进行 avfoundation 屏幕和音频录制 - 视频冻结

使用 AVFoundation 在 iOS 上录制音频样本