public void UploadVideo(string filename, string title, string description, VideoManagerClient.UploadVideoProgress progressCallback) { Console.WriteLine(FacebookPageID); FileInfo fileInfo = new FileInfo(filename); string videoUrl = "https://graph-video.facebook.com/v2.3/" + FacebookPageID + "/videos"; var webClient = createWebClient(); UploaderState state = sendStartRequest(videoUrl, fileInfo.Length); uploadChunks(state, videoUrl, fileInfo, progressCallback); Console.WriteLine("Upload chunks complete!"); progressCallback(fileInfo.Name, 0, fileInfo.Length); bool success = postVideo(state, videoUrl, fileInfo, title, description); Console.WriteLine("Success? " + success); progressCallback(fileInfo.Name, fileInfo.Length, fileInfo.Length); System.Threading.Thread.Sleep(60000); }
private void uploadChunks(UploaderState state, string url, FileInfo fileInfo, VideoManagerClient.UploadVideoProgress progressCallback) { int chunk = 1; using (FileStream fileStream = new FileStream(fileInfo.ToString(), FileMode.Open)) { while (true) { progressCallback(fileInfo.Name, state.StartOffset, fileInfo.Length); byte[] data = new byte[state.EndOffset - state.StartOffset]; int totalRead = 0; while (totalRead < data.Length) { int bytesRead = fileStream.Read(data, totalRead, data.Length - totalRead); totalRead += bytesRead; Console.WriteLine("Total Read: " + totalRead + ", bytesRead: " + bytesRead); } MultipartFormDataContent form = new MultipartFormDataContent(); form.Add(new StringContent(FacebookToken), "access_token"); form.Add(new StringContent("transfer"), "upload_phase"); form.Add(new StringContent(state.StartOffset.ToString()), "start_offset"); form.Add(new StringContent(state.UploadSessionID.ToString()), "upload_session_id"); form.Add(new ByteArrayContent(data, 0, data.Length), "video_file_chunk", "chunk" + chunk + ".mp4"); var client = createWebClient(); JObject initialJson = postAndGetJson(client, url, form); state.StartOffset = initialJson["start_offset"].ToObject <int>(); state.EndOffset = initialJson["end_offset"].ToObject <int>(); Console.WriteLine("Uploaderstate: " + state.ToString()); chunk++; // This indicates that the upload process is complete if (state.StartOffset == state.EndOffset) { break; } } } }
private string ExecuteConversion(string commandTemplateFile, UploadMode uploadMode1, UploadMode uploadMode2, FileInfo inputFileInfo, string outputFile, string serviceName, string scriptureReference, string[] tags, DateTime serviceDate, int skipMinutes, int skipSeconds, bool visible) { string errors = ""; int totalSkipSeconds = (60 * skipMinutes) + skipSeconds; int endTime = 0; if ((int)numericEndMinutes.Value != 0 || (int)numericEndSeconds.Value != 0) { endTime = (60 * (int)numericEndMinutes.Value) + (int)numericEndSeconds.Value; } string commandTemplate = LoadCommandTemplate(commandTemplateFile).Trim(); string[] commandTemplateArr = commandTemplate.Split(' '); List <string> args = new List <string>(); string command = commandTemplateArr[0]; foreach (string arg in commandTemplateArr.ToList().Skip(1)) { switch (arg) { case "${inputFile}": args.Add(inputFileInfo.FullName); break; case "${outputFile}": args.Add(outputFile); break; case "${skipSeconds}": args.Add("" + totalSkipSeconds); if (endTime != 0) { args.Add("-to"); args.Add("" + endTime); } break; default: args.Add(arg); break; } } try { this.Invoke((MethodInvoker) delegate { setControlsEnabled(false); }); FileInfo outputFileInfo = new FileInfo(outputFile); outputFileInfo.Delete(); VideoManagerClient client = new VideoManagerClient(); string homePath = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%"); using (StreamWriter outWriter = new StreamWriter(homePath + @"\videoconversionout_" + uploadMode1 + ".log")) { // Start the child process. Process p = new Process(); // Redirect the output stream of the child process. p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.FileName = command; p.StartInfo.Arguments = ArgvToCommandLine(args); outWriter.WriteLine("Arguments: " + p.StartInfo.Arguments); p.Start(); // make sure to kill it if we are closed MainForm.FormClosing += delegate(Object sender, FormClosingEventArgs e) { p.Refresh(); if (!p.HasExited) { p.Kill(); } }; // Do not wait for the child process to exit before // reading to the end of its redirected stream. // p.WaitForExit(); // Read the output stream first and then wait. string line; string totalDurationString = null; Regex durationRegex = new Regex(".*Duration: (.*?), "); Regex frameLineRegex = new Regex(".*time=(.*?) .*"); int totalSeconds = 0; while ((line = p.StandardError.ReadLine()) != null) { if (line.Contains("Duration: ")) { string durationString = durationRegex.Match(line).Groups[1].Value; totalDurationString = durationString; outWriter.WriteLine("DURATION: " + durationString); string[] durationArray = durationString.Split(':'); //00:00:49.55 float hours = float.Parse(durationArray[0]); float minutes = float.Parse(durationArray[1]); float seconds = float.Parse(durationArray[2]); totalSeconds = (int)(seconds + (minutes * 60) + (hours * 60 * 60)); } else if (frameLineRegex.IsMatch(line)) { // 00:00:00.30 string currentPositionString = frameLineRegex.Match(line).Groups[1].Value; this.Invoke((MethodInvoker) delegate { lblProgressText.Text = currentPositionString + "/" + totalDurationString; }); string[] currentPositionArray = currentPositionString.Split(':'); float hours = float.Parse(currentPositionArray[0]); float minutes = float.Parse(currentPositionArray[1]); float seconds = float.Parse(currentPositionArray[2]); int currentSeconds = (int)(seconds + (minutes * 60) + (hours * 60 * 60)); int currentPercentage = (int)(((double)currentSeconds / (double)totalSeconds) * 100d); outWriter.WriteLine("PERCENTAGE: " + currentPercentage); this.Invoke((MethodInvoker) delegate { progressBar1.Value = currentPercentage > 100 ? 100 : currentPercentage; }); } outWriter.WriteLine("LINE: " + line); outWriter.Flush(); } while ((line = p.StandardOutput.ReadLine()) != null) { outWriter.WriteLine(line); } p.WaitForExit(); } if (uploadMode1 != UploadMode.No_Upload || uploadMode2 != UploadMode.No_Upload) { this.Invoke((MethodInvoker) delegate { lblProgressText.Text = "Beginning File Upload"; progressBar1.Value = 0; }); UploadMode currentUploadMode = uploadMode1; VideoManagerClient.UploadVideoProgress progressCallback = delegate(string filename, long currentProgress, long totalLength) { this.Invoke((MethodInvoker) delegate { lblProgressText.Text = "(" + currentUploadMode + ") Uploading " + filename + " " + currentProgress + "/" + totalLength; int percentage = (int)(((double)currentProgress / (double)totalLength) * 100d); progressBar1.Value = percentage > 100 ? 100 : percentage; }); }; string remoteFilename; try { if (uploadMode1 != UploadMode.No_Upload) { currentUploadMode = uploadMode1; client.UploadVideo(uploadMode1, out remoteFilename, outputFileInfo.FullName, serviceName, scriptureReference, tags, progressCallback); } } catch (Exception e) { errors += "Conversion for " + uploadMode1 + " failed due to: " + e.Message; } try { if (uploadMode2 != UploadMode.No_Upload) { currentUploadMode = uploadMode2; client.UploadVideo(uploadMode2, out remoteFilename, outputFileInfo.FullName, serviceName, scriptureReference, tags, progressCallback); } } catch (Exception e) { errors += "Conversion for " + uploadMode2 + " failed due to: " + e.Message; } if (uploadMode1 != UploadMode.No_Upload || uploadMode2 != UploadMode.No_Upload) { this.Invoke((MethodInvoker) delegate { lblProgressText.Text = "Upload complete!"; }); } else { this.Invoke((MethodInvoker) delegate { lblProgressText.Text = UploadMode.No_Upload + " Video Update Complete!"; }); } } } catch (Exception e) { errors += "Conversion for " + uploadMode1 + " failed due to: " + e.Message; } return(errors); }