Exemplo n.º 1
0
        private void buttonPreview_Click(object sender, EventArgs e)
        {
            try
            {
                buttonPreview.Enabled          = false;
                labelPreviewPleaseWait.Visible = true;
                Cursor = Cursors.WaitCursor;

                // generate blank video
                var tempVideoFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".mkv");
                var process           = VideoPreviewGenerator.GenerateVideoFile(
                    tempVideoFileName,
                    2,
                    (int)numericUpDownWidth.Value,
                    (int)numericUpDownHeight.Value,
                    Color.Black,
                    true,
                    25,
                    null);
                process.Start();
                while (!process.HasExited)
                {
                    System.Threading.Thread.Sleep(100);
                    Application.DoEvents();
                }

                // make temp assa file with font
                var assaTempFileName = GetAssaFileName(tempVideoFileName);
                var sub = new Subtitle();
                sub.Header = _assaSubtitle.Header;
                sub.Paragraphs.Add(new Paragraph(GetPreviewParagraph()));

                if (!_isAssa)
                {
                    SetStyleForNonAssa(sub);
                }
                FixRightToLeft(sub);
                FileUtil.WriteAllText(assaTempFileName, new AdvancedSubStationAlpha().ToText(sub, string.Empty), new TextEncoding(Encoding.UTF8, "UTF8"));

                // hardcode subtitle
                var outputVideoFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".mp4");
                process = GetFfmpegProcess(tempVideoFileName, outputVideoFileName, assaTempFileName);
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                while (!process.HasExited)
                {
                    System.Threading.Thread.Sleep(100);
                    Application.DoEvents();
                }

                string bmpFileName;
                try
                {
                    bmpFileName = VideoPreviewGenerator.GetScreenShot(outputVideoFileName, "00:00:01");
                    using (var bmp = new Bitmap(bmpFileName))
                    {
                        using (var form = new ExportPngXmlPreview(bmp))
                        {
                            form.AllowNext                 = false;
                            form.AllowPrevious             = false;
                            labelPreviewPleaseWait.Visible = false;
                            form.ShowDialog(this);
                        }
                    }
                }
                catch
                {
                    if (comboBoxVideoEncoding.Text.EndsWith("_amf"))
                    {
                        MessageBox.Show("Unable to generate video with AMD hardware acceleration!");
                    }
                    else if (comboBoxVideoEncoding.Text.EndsWith("_nvenc"))
                    {
                        MessageBox.Show("Unable to generate video with Nvidia hardware acceleration!");
                    }
                    else
                    {
                        MessageBox.Show("Unable to generate video!");
                    }

                    Cursor = Cursors.Default;
                    buttonPreview.Enabled          = true;
                    labelPreviewPleaseWait.Visible = false;
                    return;
                }


                try
                {
                    File.Delete(tempVideoFileName);
                    File.Delete(assaTempFileName);
                    File.Delete(outputVideoFileName);
                    File.Delete(bmpFileName);
                }
                catch
                {
                    // ignore
                }
            }
            finally
            {
                Cursor = Cursors.Default;
                buttonPreview.Enabled          = true;
                labelPreviewPleaseWait.Visible = false;
            }
        }
        /// <summary>
        /// The link label preview_ link clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void linkLabelPreview_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.linkLabelPreview.Enabled = false;
            this.Cursor = Cursors.WaitCursor;
            try
            {
                int width;
                int height;
                this.GetResolution(out width, out height);
                using (var bmp = new Bitmap(width, height))
                {
                    using (var g = Graphics.FromImage(bmp))
                    {
                        var p = this._subtitle.Paragraphs[this.subtitleListView1.SelectedItems[0].Index];
                        this.FillPreviewBackground(bmp, g, p);

                        var nBmp = new NikseBitmap(this.pictureBox1.Image as Bitmap);
                        nBmp.CropSidesAndBottom(100, Color.Transparent, true);
                        using (var textBmp = nBmp.GetBitmap())
                        {
                            var bp = this.MakeMakeBitmapParameter(this.subtitleListView1.SelectedItems[0].Index, width, height);
                            var alignment = GetAlignmentFromParagraph(bp, this._format, this._subtitle);
                            if (this.comboBoxHAlign.Visible && alignment == ContentAlignment.BottomCenter && this._format.GetType() != typeof(AdvancedSubStationAlpha) && this._format.GetType() != typeof(SubStationAlpha))
                            {
                                if (this.comboBoxHAlign.SelectedIndex == 0)
                                {
                                    alignment = ContentAlignment.BottomLeft;
                                }
                                else if (this.comboBoxHAlign.SelectedIndex == 2)
                                {
                                    alignment = ContentAlignment.BottomRight;
                                }
                            }

                            int x = (bmp.Width - textBmp.Width) / 2;
                            if (alignment == ContentAlignment.BottomLeft || alignment == ContentAlignment.MiddleLeft || alignment == ContentAlignment.TopLeft)
                            {
                                x = int.Parse(this.comboBoxBottomMargin.Text);
                            }
                            else if (alignment == ContentAlignment.BottomRight || alignment == ContentAlignment.MiddleRight || alignment == ContentAlignment.TopRight)
                            {
                                x = bmp.Width - textBmp.Width - int.Parse(this.comboBoxBottomMargin.Text);
                            }

                            int y = bmp.Height - textBmp.Height - int.Parse(this.comboBoxBottomMargin.Text);
                            if (alignment == ContentAlignment.BottomLeft || alignment == ContentAlignment.MiddleLeft || alignment == ContentAlignment.TopLeft)
                            {
                                x = int.Parse(this.comboBoxBottomMargin.Text);
                            }
                            else if (alignment == ContentAlignment.BottomRight || alignment == ContentAlignment.MiddleRight || alignment == ContentAlignment.TopRight)
                            {
                                x = bmp.Width - textBmp.Width - int.Parse(this.comboBoxBottomMargin.Text);
                            }

                            if (alignment == ContentAlignment.MiddleLeft || alignment == ContentAlignment.MiddleCenter || alignment == ContentAlignment.MiddleRight)
                            {
                                y = (this.groupBoxExportImage.Height - 4 - textBmp.Height) / 2;
                            }
                            else if (alignment == ContentAlignment.TopLeft || alignment == ContentAlignment.TopCenter || alignment == ContentAlignment.TopRight)
                            {
                                y = int.Parse(this.comboBoxBottomMargin.Text);
                            }

                            g.DrawImageUnscaled(textBmp, new Point(x, y));
                        }
                    }

                    using (var form = new ExportPngXmlPreview(bmp))
                    {
                        this.Cursor = Cursors.Default;
                        form.ShowDialog(this);
                    }
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
                this.linkLabelPreview.Enabled = true;
            }
        }
        private void buttonPreview_Click(object sender, EventArgs e)
        {
            try
            {
                buttonPreview.Enabled          = false;
                labelPreviewPleaseWait.Visible = true;
                Cursor = Cursors.WaitCursor;

                // generate blank video
                var tempVideoFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".mkv");
                var process           = VideoPreviewGenerator.GenerateVideoFile(
                    tempVideoFileName,
                    2,
                    (int)numericUpDownWidth.Value,
                    (int)numericUpDownHeight.Value,
                    Color.Black,
                    true,
                    25);
                process.Start();
                while (!process.HasExited)
                {
                    System.Threading.Thread.Sleep(100);
                    Application.DoEvents();
                }

                // make temp assa file with font
                var assaTempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".ass");
                var sub = new Subtitle();
                sub.Header = _assaSubtitle.Header;
                sub.Paragraphs.Add(new Paragraph(GetPreviewText(), 0, 10_000));

                if (numericUpDownFontSize.Visible) // not ASSA format
                {
                    SetStyleForNonAssa(sub);
                }
                FixRightToLeft(sub);
                File.WriteAllText(assaTempFileName, new AdvancedSubStationAlpha().ToText(sub, string.Empty));

                // hardcode subtitle
                var outputVideoFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".mp4");
                process = GetFfmpegProcess(tempVideoFileName, outputVideoFileName, assaTempFileName);
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                while (!process.HasExited)
                {
                    System.Threading.Thread.Sleep(100);
                    Application.DoEvents();
                }

                Cursor = Cursors.Default;
                var bmpFileName = VideoPreviewGenerator.GetScreenShot(outputVideoFileName, "00:00:01");
                using (var bmp = new Bitmap(bmpFileName))
                {
                    using (var form = new ExportPngXmlPreview(bmp))
                    {
                        form.AllowNext                 = false;
                        form.AllowPrevious             = false;
                        labelPreviewPleaseWait.Visible = false;
                        form.ShowDialog(this);
                    }
                }

                try
                {
                    File.Delete(tempVideoFileName);
                    File.Delete(assaTempFileName);
                    File.Delete(outputVideoFileName);
                    File.Delete(bmpFileName);
                }
                catch
                {
                    // ignore
                }
            }
            finally
            {
                Cursor = Cursors.Default;
                buttonPreview.Enabled          = true;
                labelPreviewPleaseWait.Visible = false;
            }
        }