private void exportToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Animated GIF|*.gif|Animated PNG|*.png|MPEG-4|*.mp4|AVI|*.avi|WebM|*.webm|Flash Video|*.flv|Windows Media Video|*.wmv"; if (dlg.ShowDialog() != DialogResult.OK) return; float fps = ActiveProject.FPS; float delta = 1.0f / fps; float endTime = 0.0f; string temp = Path.GetTempPath() + Path.GetRandomFileName(); Directory.CreateDirectory(temp); foreach (Layer layer in ActiveProject.Layers) { endTime = Math.Max(endTime, layer.Framesets[layer.Framesets.Count - 1].EndTime); } int n = 0; int nt = (int)Math.Ceiling(endTime / ActiveProject.AnimSpeed / delta); ProgressDialog progress = new ProgressDialog(); progress.StartPosition = FormStartPosition.CenterParent; bool frameCanceled = false; EventHandler frameCancelHandler = (_1, _2) => { frameCanceled = true; }; progress.Title = "Rendering Frames.."; progress.ProgressStyle = ProgressBarStyle.Continuous; progress.Canceled += frameCancelHandler; progress.Work = () => { for (float time = 0; time <= endTime / ActiveProject.AnimSpeed && !frameCanceled; time += delta) { progress.DetailText = "Frame " + (n + 1) + " of " + (nt + 1); progress.ProgressValue = n * 100 / nt; Form_Canvas.DrawFrame(time * ActiveProject.AnimSpeed, true, true); Image.FromHbitmap(Form_Canvas.TakeScreenshot()).Save(temp + "\\" + n + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp); Application.DoEvents(); n++; } if (frameCanceled) { Directory.Delete(temp, true); progress.Close(); return; } progress.Canceled -= frameCancelHandler; progress.Canceled += progress.Finish; progress.Title = "Encoding Video.."; progress.ProgressStyle = ProgressBarStyle.Marquee; progress.DetailText = "Waiting for ffmpeg.."; ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "ffmpeg.exe"; startInfo.Arguments = "-y -r " + fps + " -i \"" + temp + "\\%d.bmp\" \"" + dlg.FileName + "\""; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; Process processTemp = new Process(); processTemp.StartInfo = startInfo; processTemp.EnableRaisingEvents = true; processTemp.Exited += (sender2, e2) => { progress.Finish(sender2, e2); Directory.Delete(temp, true); }; processTemp.Start(); }; progress.ShowDialog(); }
public void ProjectExport() { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Animated GIF|*.gif|Animated PNG|*.png|MPEG-4|*.mp4|AVI|*.avi|WebM|*.webm|Flash Video|*.flv|Windows Media Video|*.wmv"; if (dlg.ShowDialog() != DialogResult.OK) { return; } float fps = ActiveProject.FPS; float delta = 1.0f / fps; float endTime = 0.0f; string temp = Path.GetTempPath() + Path.GetRandomFileName(); Directory.CreateDirectory(temp); foreach (Layer layer in ActiveProject.Layers) { endTime = Math.Max(endTime, layer.Framesets[layer.Framesets.Count - 1].EndTime); } int n = 0; int nt = (int)Math.Ceiling(endTime / ActiveProject.AnimSpeed / delta); ProgressDialog progress = new ProgressDialog(); progress.StartPosition = FormStartPosition.CenterParent; bool frameCanceled = false; EventHandler frameCancelHandler = (_1, _2) => { frameCanceled = true; }; progress.Title = "Rendering Frames.."; progress.ProgressStyle = ProgressBarStyle.Continuous; progress.Canceled += frameCancelHandler; progress.Work = () => { for (float time = 0; time <= endTime / ActiveProject.AnimSpeed && !frameCanceled; time += delta) { progress.DetailText = "Frame " + (n + 1) + " of " + (nt + 1); progress.ProgressValue = n * 100 / nt; Form_Canvas.DrawFrame(time * ActiveProject.AnimSpeed, true, true); Image.FromHbitmap(Form_Canvas.TakeScreenshot()).Save(temp + "\\" + n + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp); Application.DoEvents(); n++; } if (frameCanceled) { Directory.Delete(temp, true); progress.Close(); return; } progress.Canceled -= frameCancelHandler; progress.Canceled += progress.Finish; progress.Title = "Encoding Video.."; progress.ProgressStyle = ProgressBarStyle.Marquee; progress.DetailText = "Waiting for ffmpeg.."; ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "ffmpeg.exe"; startInfo.Arguments = "-y -r " + fps + " -i \"" + temp + "\\%d.bmp\" \"" /* + "c:v libx264 -preset veryslow -qp 0" */ + dlg.FileName + "\""; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; Process processTemp = new Process(); processTemp.StartInfo = startInfo; processTemp.EnableRaisingEvents = true; processTemp.OutputDataReceived += (s, x) => { Console.WriteLine(x.Data); }; processTemp.ErrorDataReceived += (s, x) => { MessageBox.Show(x.Data, "FFMPEG Error"); }; processTemp.Exited += (sender2, e2) => { progress.Finish(sender2, e2); Directory.Delete(temp, true); }; processTemp.Start(); }; progress.ShowDialog(); }