如何将video文件转换为多种数据在迅速?

我已经试过这段代码,但我不能得到video的path和文件转换为多种forms的数据是有任何其他方法来转换文件。代码工作正常的图像上传,但对于video,我不知道出错

var path = NSBundle.mainBundle().pathForResource("sample", ofType: "mp4") var data1 = NSData(contentsOfFile:path!) var videodata:NSData=NSData(data:data1!) var len = videodata.length println() var postlength:NSString = NSString(format:"%d",len) var request = NSMutableURLRequest(URL: NSURL(string:"(liveurl)/mobile/post/video_post")!) request.HTTPMethod = "POST" var boundary = NSString(format:"---------------------------14737809831466499882746641449") request.setValue(postlength, forHTTPHeaderField: "Content-Length") var contentType = NSString(format:"multipart/form-data; boundary=%@",boundary) request.addValue(contentType, forHTTPHeaderField: "Content-Type") var postData = NSMutableData.alloc() println(request) postData.appendData(NSString(format:"\r\n--%@\r\n",boundary).dataUsingEncoding(NSUTF8StringEncoding)!) postData.appendData(NSString(format:"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"video.mp4\"\r\n").dataUsingEncoding(NSUTF8StringEncoding)!) postData.appendData(NSString(format:"Content-Type: application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!) postData.appendData(videodata) println(videodata) postData.appendData(NSString(format:"\r\n--%@--\r\n",boundary).dataUsingEncoding(NSUTF8StringEncoding)!) println("postData") println(postData) request.HTTPBody = postData println("request") println(request) NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse!, data: NSData?, error: NSError!) -> Void in if (data != nil) { println(response) if let jsonArray: NSArray = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableLeaves, error: nil) as NSArray? { let jsonObject: NSDictionary? = jsonArray.objectAtIndex(0) as? NSDictionary println(jsonObject!) if (jsonObject != nil) { println("object: %@", jsonObject!) if (jsonObject != nil) { println("\(jsonObject)") // process jsonResult } else { println("no response \(error)") // couldn't load JSON, look at error } } } } } 

 var path = NSBundle.mainBundle().pathForResource("sample", ofType: "mp4") var data1 = NSData() var videodata: NSData? = NSData.dataWithContentsOfMappedFile(path!) as? NSData var request = NSMutableURLRequest(URL: NSURL(string:"the request url")!) var session = NSURLSession.sharedSession() request.HTTPMethod = "POST" //the encoding begins var boundary = NSString(format: "---------------------------14737809831466499882746641449") var contentType = NSString(format: "multipart/form-data; boundary=%@",boundary) request.addValue(contentType, forHTTPHeaderField: "Content-Type") var body = NSMutableData.alloc() // Title body.appendData(NSString(format: "\r\n--%@\r\n",boundary).dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(NSString(format:"Content-Disposition: form-data; name=\"title\"\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData("Hello World".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!) body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData("Content-Disposition: form-data; name=\"fileToUpload\"; filename=\"sample.mp4\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(NSString(format: "Content-Type: application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(videodata!) body.appendData(NSString(format: "\r\n--%@\r\n", boundary).dataUsingEncoding(NSUTF8StringEncoding)!) request.HTTPBody = body NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse!, data: NSData?, error: NSError!) -> Void in if (data != nil) { if let jsonArray: NSArray = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSArray? { println("json array\(jsonArray)") } else { } } } }