0

我正在使用 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];
}

非常感谢帮助。

4

1 回答 1

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



}
于 2017-01-12T11:56:18.823 回答