I'm uploading a Uploading Ad Video to Facebook using Facebook Graph API is not working. Had some Bytes missing in the call.
My issue is with video_file_chunk, As we are sending the chunked File Start and End offset are getting differently.
First iteration Start Offset is 0 End Offset is 1048576
In the response we are getting Offsets for 2nd iteration as
Start Offset is 1048578 End Offset is 2048578
There is clearly a Byte missing here. and in the last iteration the code is breaking. giving some generic exception.
"Your video upload timed out before it could be completed. This is probably because of a slow network connection or because the video you're trying to upload is too large. Please try again."
var fb = GetFB();
using (FileStream fs = new FileStream("E:\\cnp.mp4",
FileMode.Open,
FileAccess.Read))
{
dynamic parameters = new System.Dynamic.ExpandoObject();
parameters.upload_phase = "start";
parameters.file_size = fs.Length;
var fileSize = (int)fs.Length;
Console.WriteLine("file_size: {0}", fileSize);
dynamic result = fb.Post("/" + FBAccountID + "/advideos", parameters);
string upload_session_id = (string)result["upload_session_id"];
Console.WriteLine("");
Console.WriteLine("upload_session_id: {0}", (string)result["upload_session_id"]);
Console.WriteLine("Video Id: {0}", (string)result["video_id"]);
Console.WriteLine("");
Console.WriteLine("start_offset: {0}", (string)result["start_offset"]);
Console.WriteLine("end_offset: {0}", (string)result["end_offset"]);
int startOffset = int.Parse((string)result["start_offset"]);
int endOffset = int.Parse((string)result["end_offset"]);
int length = endOffset - startOffset;
int i = 1;
int totalBytesRead = 0;
BinaryReader br = new BinaryReader(fs);
while (length > 0)
{
if(endOffset == fileSize)
{
if (fileSize - totalBytesRead > endOffset - startOffset)
{
length = fileSize - totalBytesRead;
}
}
byte[] buff = new byte[length];
buff = br.ReadBytes(length);
totalBytesRead += buff.Length;
//var gi = fs.Read(buff, startOffset, endOffset - startOffset);
Console.WriteLine("buff length: {0}", buff.Length);
Console.WriteLine("Total Bytes Read: {0}", totalBytesRead);
dynamic parameters1 = new System.Dynamic.ExpandoObject();
parameters1.upload_phase = "transfer";
parameters1.upload_session_id = upload_session_id;
parameters1.start_offset = startOffset;
parameters1.video_file_chunk = new FacebookMediaObject { ContentType = "video/mp4", FileName = "cnp " + i + ".mp4" }.SetValue(buff);
i++;
dynamic result1 = fb.Post("/" + FBAccountID + "/advideos", parameters1);
Console.WriteLine("");
Console.WriteLine("start_offset: {0}", (string)result1["start_offset"]);
Console.WriteLine("end_offset: {0}", (string)result1["end_offset"]);
startOffset = int.Parse((string)result1["start_offset"]);
endOffset = int.Parse((string)result1["end_offset"]);
length = endOffset - startOffset;
}
dynamic parameters2 = new System.Dynamic.ExpandoObject();
parameters2.upload_phase = "finish";
parameters2.upload_session_id = upload_session_id;
parameters2.title = "Video title";
dynamic result2 = fb.Post("/" + FBAccountID + "/advideos", parameters2);
Console.WriteLine("success: {0}", (string)result2["success"]);
Thanks in advance for any help.