OSDN Git Service

4591b3d6dd3464b734332e6f85cc575bb8e95441
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmPreview.cs
1 using System;\r
2 using System.Windows.Forms;\r
3 using System.Threading;\r
4 using System.Diagnostics;\r
5 using System.Runtime.InteropServices;\r
6 using System.IO;\r
7 using AxQTOControlLib;\r
8 using QTOControlLib;\r
9 using QTOLibrary;\r
10 \r
11 namespace Handbrake\r
12 {\r
13     public partial class frmPreview : Form\r
14     {\r
15 \r
16         QueryGenerator hb_common_func = new QueryGenerator();\r
17         Functions.Encode process = new Functions.Encode();\r
18         private delegate void UpdateUIHandler();\r
19         String currently_playing = "";\r
20         readonly frmMain mainWindow;\r
21         private Process hbProc;\r
22         private Thread player;\r
23         private Boolean noQT;\r
24 \r
25         public frmPreview(frmMain mw)\r
26         {\r
27             try\r
28             {\r
29                 InitializeComponent();\r
30             }\r
31             catch (Exception exc)\r
32             {\r
33                 MessageBox.Show(mw, "It would appear QuickTime 7 is not installed. QuickTime preview functionality will be disabled! \n\n Debug Info:\n" + exc, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
34                 btn_playQT.Enabled = false;\r
35                 noQT = true;\r
36             }\r
37             this.mainWindow = mw;\r
38             cb_preview.SelectedIndex = 0;\r
39             cb_duration.SelectedIndex = 1;\r
40         }\r
41 \r
42         #region Encode Sample\r
43         private void btn_playVLC_Click(object sender, EventArgs e)\r
44         {\r
45             lbl_status.Visible = true;\r
46             try\r
47             {\r
48                 QTControl.URL = "";\r
49                 if (File.Exists(currently_playing))\r
50                     File.Delete(currently_playing);\r
51             }\r
52             catch (Exception)\r
53             {\r
54                 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
55             }\r
56 \r
57             btn_playQT.Enabled = false;\r
58             btn_playVLC.Enabled = false;\r
59             lbl_status.Text = "Encoding Sample for (VLC) ...";\r
60             String query = hb_common_func.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
61             ThreadPool.QueueUserWorkItem(procMonitor, query);\r
62         }\r
63         private void btn_playQT_Click(object sender, EventArgs e)\r
64         {\r
65             lbl_status.Visible = true;\r
66             try\r
67             {\r
68                 QTControl.URL = "";\r
69                 if (File.Exists(currently_playing))\r
70                     File.Delete(currently_playing);\r
71             }\r
72             catch (Exception)\r
73             {\r
74                 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
75             }\r
76 \r
77             btn_playQT.Enabled = false;\r
78             btn_playVLC.Enabled = false;\r
79             lbl_status.Text = "Encoding Sample for (QT) ...";\r
80             String query = hb_common_func.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
81             ThreadPool.QueueUserWorkItem(procMonitor, query);\r
82         }\r
83         private void procMonitor(object state)\r
84         {\r
85             // Make sure we are not already encoding and if we are then display an error.\r
86             if (hbProc != null)\r
87                 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
88             else\r
89             {\r
90                 hbProc = process.runCli(this, (string)state);\r
91                 hbProc.WaitForExit();\r
92                 hbProc = null;\r
93                 encodeCompleted();\r
94             }\r
95         }\r
96         private void encodeCompleted()\r
97         {\r
98             try\r
99             {\r
100                 if (InvokeRequired)\r
101                 {\r
102                     BeginInvoke(new UpdateUIHandler(encodeCompleted));\r
103                     return;\r
104                 }\r
105                 if (!noQT)\r
106                     btn_playQT.Enabled = true;\r
107                 btn_playVLC.Enabled = true;\r
108 \r
109                 // Decide which player to use.\r
110                 String playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";\r
111 \r
112                 lbl_status.Text = "Loading Clip ...";\r
113 \r
114                 // Get the sample filename\r
115                 if (mainWindow.text_destination.Text != "")\r
116                     currently_playing = mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v");\r
117 \r
118                 // Play back in QT or VLC\r
119                 if (playerSelection == "QT")\r
120                     play();\r
121                 else\r
122                     playVLC();\r
123 \r
124                 lbl_status.Text = "";\r
125             }\r
126             catch (Exception exc)\r
127             {\r
128                 MessageBox.Show(this, "frmPreview.cs encodeCompleted " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
129             }\r
130         }\r
131         #endregion\r
132 \r
133         #region Playback\r
134 \r
135         /// <summary>\r
136         /// Play the video back in the QuickTime control\r
137         /// </summary>\r
138         private void play()\r
139         {\r
140             player = new Thread(OpenMovie) { IsBackground = true };\r
141             player.Start();\r
142             lbl_status.Visible = false;\r
143         }\r
144 \r
145         /// <summary>\r
146         /// Play the video back in an external VLC player\r
147         /// </summary>\r
148         private void playVLC()\r
149         {\r
150             // Launch VLC and play video.\r
151             if (currently_playing != "")\r
152             {\r
153                 if (File.Exists(currently_playing))\r
154                 {\r
155                     if (File.Exists(Properties.Settings.Default.VLC_Path))\r
156                     {\r
157                         String args = "\"" + currently_playing + "\"";\r
158                         ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
159                         Process.Start(vlc);\r
160                         lbl_status.Text = "VLC will now launch.";\r
161                     }\r
162                     else\r
163                         MessageBox.Show(this, "Unable to detect VLC Player. \nPlease make sure VLC is installed and the directory specified in the program options is correct.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
164                 }\r
165                 else\r
166                     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
167             }\r
168             lbl_status.Visible = false;\r
169         }\r
170 \r
171         /// <summary>\r
172         /// QT control - Open the file\r
173         /// </summary>\r
174         [STAThread]\r
175         private void OpenMovie()\r
176         {\r
177             try\r
178             {\r
179                 if (InvokeRequired)\r
180                 {\r
181                     BeginInvoke(new UpdateUIHandler(OpenMovie));\r
182                     return;\r
183                 }\r
184                 QTControl.Sizing = QTSizingModeEnum.qtControlFitsMovie;\r
185                 QTControl.URL = currently_playing;\r
186                 QTControl.Sizing = QTSizingModeEnum.qtMovieFitsControl;\r
187                 QTControl.Show();\r
188 \r
189                 this.ClientSize = QTControl.Size;\r
190                 this.Height += 25;\r
191             }\r
192             catch (COMException ex)\r
193             {\r
194                 QTUtils qtu = new QTUtils();\r
195                 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
196             }\r
197             catch (Exception ex)\r
198             {\r
199                 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
200             }\r
201         }\r
202         #endregion\r
203     }\r
204 }\r