OSDN Git Service

LinGui: enable libdvdnav by default
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmPreview.cs
index 029ac85..59e6c25 100644 (file)
@@ -3,6 +3,10 @@ using System.Windows.Forms;
 using System.Threading;\r
 using System.Diagnostics;\r
 using System.Runtime.InteropServices;\r
+using System.IO;\r
+using Handbrake.EncodeQueue;\r
+using Handbrake.Functions;\r
+using QTOControlLib;\r
 using QTOLibrary;\r
 \r
 namespace Handbrake\r
@@ -11,79 +15,98 @@ namespace Handbrake
     {\r
 \r
         QueryGenerator hb_common_func = new QueryGenerator();\r
-        Functions.Encode process = new Functions.Encode();\r
+        EncodeAndQueueHandler process = new EncodeAndQueueHandler();\r
         private delegate void UpdateUIHandler();\r
         String currently_playing = "";\r
-        frmMain mainWindow;\r
-        private Process hbProc;\r
+        readonly frmMain mainWindow;\r
         private Thread player;\r
+        private Boolean noQT;\r
 \r
         public frmPreview(frmMain mw)\r
         {\r
-            InitializeComponent();\r
+            try\r
+            {\r
+                InitializeComponent();\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                MessageBox.Show(mw, "It would appear QuickTime 7 is not installed or not accessible. QuickTime preview functionality will be disabled! \n\n Debug Info:\n" + exc, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                btn_playQT.Enabled = false;\r
+                noQT = true;\r
+            }\r
             this.mainWindow = mw;\r
             cb_preview.SelectedIndex = 0;\r
             cb_duration.SelectedIndex = 1;\r
         }\r
 \r
-        private void play()\r
-        {\r
-            player = new Thread(OpenMovie) {IsBackground = true};\r
-            player.Start();\r
-        }\r
-\r
-        [STAThread]\r
-        private void OpenMovie()\r
+        #region Encode Sample\r
+        private void btn_playVLC_Click(object sender, EventArgs e)\r
         {\r
+            lbl_status.Visible = true;\r
             try\r
             {\r
-                if (InvokeRequired)\r
-                {\r
-                    BeginInvoke(new UpdateUIHandler(OpenMovie));\r
-                    return;\r
-                }\r
-                QTControl.URL = currently_playing;\r
-                QTControl.Width = QTControl.Movie.Width;\r
-                QTControl.Height = QTControl.Movie.Height;\r
-                // The initial control size is 64,64. If we do not reload the clip here\r
-                // it'll scale the video from 64,64. \r
-                // Unsure why as it correctly resizes the control to the movies actual size.\r
-                QTControl.URL = currently_playing; \r
-                QTControl.SetScale(0);\r
-                QTControl.Show();\r
-\r
-                this.Width = QTControl.Width + 5;\r
-                this.Height = QTControl.Height + 90;\r
-            }\r
-            catch (COMException ex)\r
-            {\r
-                QTUtils qtu = new QTUtils();\r
-                MessageBox.Show("Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") + "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode));\r
+                QTControl.URL = "";\r
+                if (File.Exists(currently_playing))\r
+                    File.Delete(currently_playing);\r
             }\r
-            catch (Exception ex)\r
+            catch (Exception)\r
             {\r
-                MessageBox.Show("Unable to open movie:\n\n" + ex);\r
+                MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
             }\r
+\r
+            btn_playQT.Enabled = false;\r
+            btn_playVLC.Enabled = false;\r
+            lbl_status.Text = "Encoding Sample for (VLC) ...";\r
+            int duration;\r
+            int.TryParse(cb_duration.Text, out duration);\r
+            String query = hb_common_func.GenerateCLIQuery(mainWindow, duration, cb_preview.Text);\r
+            ThreadPool.QueueUserWorkItem(procMonitor, query);\r
         }\r
\r
-        #region Encode Sample\r
-        private void btn_encode_Click(object sender, EventArgs e)\r
+        private void btn_playQT_Click(object sender, EventArgs e)\r
         {\r
-            btn_encode.Enabled = false;\r
-            lbl_encode.Text = "Encoding Sample ...";\r
-            String query = hb_common_func.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
-            ThreadPool.QueueUserWorkItem(procMonitor, query);\r
+            if (mainWindow.text_destination.Text.Contains(".mkv"))\r
+            {\r
+                MessageBox.Show(this,\r
+                                "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",\r
+                                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            }\r
+            else\r
+            {\r
+                lbl_status.Visible = true;\r
+                try\r
+                {\r
+                    QTControl.URL = "";\r
+                    if (File.Exists(currently_playing))\r
+                        File.Delete(currently_playing);\r
+                }\r
+                catch (Exception)\r
+                {\r
+                    MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                }\r
+\r
+                btn_playQT.Enabled = false;\r
+                btn_playVLC.Enabled = false;\r
+                lbl_status.Text = "Encoding Sample for (QT) ...";\r
+                int duration;\r
+                int.TryParse(cb_duration.Text, out duration);\r
+                String query = hb_common_func.GenerateCLIQuery(mainWindow, duration, cb_preview.Text);\r
+\r
+                ThreadPool.QueueUserWorkItem(procMonitor, query);\r
+            }\r
         }\r
         private void procMonitor(object state)\r
         {\r
             // Make sure we are not already encoding and if we are then display an error.\r
-            if (hbProc != null)\r
-                MessageBox.Show("Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            if (process.hbProcess != null)\r
+                MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
             else\r
             {\r
-                hbProc = process.runCli(this, (string)state);\r
-                hbProc.WaitForExit();\r
-                hbProc = null;\r
+                process.RunCli((string)state);\r
+                if (process.hbProcess != null)\r
+                {\r
+                    process.hbProcess.WaitForExit();\r
+                    process.hbProcess = null;\r
+                }\r
                 encodeCompleted();\r
             }\r
         }\r
@@ -96,21 +119,103 @@ namespace Handbrake
                     BeginInvoke(new UpdateUIHandler(encodeCompleted));\r
                     return;\r
                 }\r
-                btn_encode.Enabled = true;\r
-                lbl_encode.Text = "Loading Clip ...";\r
+                if (!noQT)\r
+                    btn_playQT.Enabled = true;\r
+                btn_playVLC.Enabled = true;\r
+\r
+                // Decide which player to use.\r
+                String playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";\r
 \r
+                lbl_status.Text = "Loading Clip ...";\r
+\r
+                // Get the sample filename\r
                 if (mainWindow.text_destination.Text != "")\r
-                    currently_playing = mainWindow.text_destination.Text.Replace(".m", "_sample.m").Replace(".avi", "_sample.avi").Replace(".ogm", "_sample.ogm");\r
+                    currently_playing = mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").Replace(".mkv", "_sample.mkv"); ;\r
+\r
+                // Play back in QT or VLC\r
+                if (playerSelection == "QT")\r
+                    play();\r
+                else\r
+                    playVLC();\r
 \r
-                play();\r
-                lbl_encode.Text = "";\r
+                lbl_status.Text = "";\r
             }\r
             catch (Exception exc)\r
             {\r
-                MessageBox.Show("frmPreview.cs encodeCompleted " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                MessageBox.Show(this, "frmPreview.cs encodeCompleted " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
             }\r
         }\r
         #endregion\r
 \r
+        #region Playback\r
+\r
+        /// <summary>\r
+        /// Play the video back in the QuickTime control\r
+        /// </summary>\r
+        private void play()\r
+        {\r
+            player = new Thread(OpenMovie) { IsBackground = true };\r
+            player.Start();\r
+            lbl_status.Visible = false;\r
+        }\r
+\r
+        /// <summary>\r
+        /// Play the video back in an external VLC player\r
+        /// </summary>\r
+        private void playVLC()\r
+        {\r
+            // Launch VLC and play video.\r
+            if (currently_playing != "")\r
+            {\r
+                if (File.Exists(currently_playing))\r
+                {\r
+                    if (File.Exists(Properties.Settings.Default.VLC_Path))\r
+                    {\r
+                        String args = "\"" + currently_playing + "\"";\r
+                        ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
+                        Process.Start(vlc);\r
+                        lbl_status.Text = "VLC will now launch.";\r
+                    }\r
+                    else\r
+                        MessageBox.Show(this, "Unable to detect VLC Player. \nPlease make sure VLC is installed and the directory specified in HandBrake's options is correct. (See: \"Tools Menu > Options > Picture Tab\") ", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                }\r
+                else\r
+                    MessageBox.Show(this, "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            }\r
+            lbl_status.Visible = false;\r
+        }\r
+\r
+        /// <summary>\r
+        /// QT control - Open the file\r
+        /// </summary>\r
+        [STAThread]\r
+        private void OpenMovie()\r
+        {\r
+            try\r
+            {\r
+                if (InvokeRequired)\r
+                {\r
+                    BeginInvoke(new UpdateUIHandler(OpenMovie));\r
+                    return;\r
+                }\r
+                QTControl.URL = currently_playing;\r
+                QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);\r
+                QTControl.URL = currently_playing;\r
+                QTControl.Show();\r
+\r
+                this.ClientSize = QTControl.Size;\r
+                this.Height += 25;\r
+            }\r
+            catch (COMException ex)\r
+            {\r
+                QTUtils qtu = new QTUtils();\r
+                MessageBox.Show(this, "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") + "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            }\r
+            catch (Exception ex)\r
+            {\r
+                MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+            }\r
+        }\r
+        #endregion\r
     }\r
-}
\ No newline at end of file
+}\r