2015-11-13 36 views
1

第一種:看起來有兩種方式可以通過GraphRequest將視頻上傳到Facebook。一個使用「file_url」參數。您提供一個URL和Facebook從外部服務器下載視頻文件。這很像這樣:用iOS上傳到Facebook時編碼視頻的正確方法FBSDK v4(multipart/form-data)

let params = [ 
    "title": "...", 
    "description": "...", 
    "file_url": "http://example.com/videofile.mp4" 
] 

let rq = FBSDKGraphRequest(graphPath: "me/videos", parameters: params, HTTPMethod: "POST") 

rq.startWithCompletionHandler { (conn, result, error) -> Void in 
    // handle error etc.. 
} 

Facebook從我的服務器加載文件,視頻出現在我的時間線上。

另一種方法是使用「source」參數直接上傳視頻。這應該有點像這樣:

let videoURL = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("testvideo", ofType: "mp4")!) 
let rawData = NSData(contentsOfURL: videoURL)! 

let params = [ 
    "title": "...", 
    "description": "...", 
    "source": rawData 
] 

但是這個失敗,出現此錯誤消息:

The video you're trying to upload is in a format that isn't supported. Please try again with a video in a supported format. 

考慮看看它看起來像視頻數據在「多/形式將被編碼documentation -數據」。該文件甚至提供了一個鏈接到w3

但是如何做到這一點對我來說仍然是個謎。我試過很多製作FORMDATA串在它的二進制圖像數據的組合等的:

var s = "" 
s += "Content-Type: multipart/form-data; boundary=XXXXXXXXXXXXXXXXXXXX\r\n\r\n" 
s += "--XXXXXXXXXXXXXXXXXXXX\r\n" 
s += "Content-Type: application/octet-stream\r\n\r\n" 

let p = "--XXXXXXXXXXXXXXXXXXXX--" 

let data = NSMutableData() 
data.appendData(s.asUTF8Data()) 
data.appendData(rawData) 
data.appendData(p.asUTF8Data()) 

let params = [ 
    "title": "...", 
    "description": "...", 
    "source": data 
] 

和其他組合與

s += "Content-Transfer-Encoding: binary\r\n" 
s += "Content-Disposition: file; filename=\"video.mp4\"\r\n" 

但沒有成功。我總是得到相同的錯誤信息:

The video you're trying to upload is in a format that isn't supported. Please try again with a video in a supported format. 

那麼如何包裝視頻文件的形式數據?

以防萬一: 不幸的是,我無法使用FBSDK v4中已經提供的視頻共享功能。

+0

嘿,你有沒有解決這個問題?我也很難找到解決方案。 – hobosf

回答

0

我發現瞭如何做到這一點使用FB圖形API,在這個項目的例子: https://github.com/brynbellomy/SEBeamMeUpScotty

相關的參數是:

NSData *videoData = [NSData dataWithContentsOfURL: self.videoURL];    NSMutableDictionary *params = @{@"video.mp4": videoData, 
           @"contentType": @"video/quicktime", 
           @"title":  @"Money Money Video Maker", 
           @"description": @"", }.mutableCopy; 
0

陷入三一天後,終於我能夠得到解決這個錯誤'您試圖上傳的視頻格式不支持。請以支持的格式重試視頻。 '從本地文件上傳時。請記住,規則是即使FB文檔中這樣說,請勿在請求參數中包含「source」或「file_url」。 下面是我的工作代碼:

func uploadVideoToWall() { 
    var videoData: NSData 
    guard let localVideoPath = Bundle.main.path(forResource: "overthinking", ofType: "mp4") else { 
     print("local video not found") 
     return 
    } 
    print("local video path: \(localVideoPath)") 
    let localPathURL = URL(fileURLWithPath: localVideoPath, isDirectory: false) 
    print("absolute string: \(localPathURL.absoluteString)") 

    do { 
     videoData = try NSData(contentsOf: localPathURL) 
     var strDesc : String 
     strDesc = " testing upload from test app 2" 
     //Do no include "source" or "file_url" in the request parameters even though the FB documentation says so. 
     let videoObject: [String : Any] = ["contentType": "multipart/form-data", "title": "Testing yoooo", "description": strDesc, localPathURL.absoluteString: videoData] 
     //self.view!.isUserInteractionEnabled = false 

     let uploadVideoRequest = FBSDKGraphRequest(graphPath: "me/videos", parameters: videoObject, httpMethod: "POST") 
     let connection = FBSDKGraphRequestConnection() 
     connection.delegate = self 
     connection.add(uploadVideoRequest, completionHandler: { (connection, result, error) in 
      if error != nil { 
       print("Error: \(String(describing: error))") 

      } 
      else { 
       print("Success") 
      } 
     }) 
     connection.start() 

    } catch { 
     print(error) 
    } 

}