0

am using AVFoundation to record the video and send it to the server. Whenever server with my recorded video, it returns Internal server error. But when I upload some dummy video other than my recorded, it's uploading successfully. Adding the code below.

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

    })

Using custom camera controller LLSimpleCamera to start recording.Below is the code to start recording.

     - (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];
}

Help much appreciated.

1 Answers1

1

I am using UIImagePickerController for recording and uploading to Server

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

 }

Use method to fetch Data of video from document directory and upload to server

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



}
Amit Verma
  • 1,393
  • 1
  • 8
  • 7
  • While converting to 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> printing this.. with dummy video it showing some other values. – satham_rahman Jan 12 '17 at 12:26