OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmPreview.cs
1 namespace Handbrake\r
2 {\r
3     using System;\r
4     using System.Diagnostics;\r
5     using System.IO;\r
6     using System.Runtime.InteropServices;\r
7     using System.Threading;\r
8     using System.Windows.Forms;\r
9     using Functions;\r
10     using QTOControlLib;\r
11     using QTOLibrary;\r
12     using Services;\r
13 \r
14     public partial class frmPreview : Form\r
15     {\r
16         private readonly Queue Process = new Queue();\r
17 \r
18         private delegate void UpdateUIHandler();\r
19 \r
20         private string CurrentlyPlaying = string.Empty;\r
21         private readonly frmMain MainWindow;\r
22         private Thread Player;\r
23         private readonly bool 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)\r
32             {\r
33                 NoQT = true;\r
34             }\r
35             this.MainWindow = mw;\r
36             cb_preview.SelectedIndex = 0;\r
37             cb_duration.SelectedIndex = 1;\r
38 \r
39             cb_preview.Items.Clear();\r
40             for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)\r
41                 cb_preview.Items.Add(i.ToString());\r
42             cb_preview.SelectedIndex = 0;\r
43         }\r
44 \r
45         #region Encode Sample\r
46 \r
47         private void btn_playVLC_Click(object sender, EventArgs e)\r
48         {\r
49             lbl_status.Visible = true;\r
50             try\r
51             {\r
52                 if (!NoQT)\r
53                     QTControl.URL = string.Empty;\r
54 \r
55                 if (File.Exists(CurrentlyPlaying))\r
56                     File.Delete(CurrentlyPlaying);\r
57             }\r
58             catch (Exception)\r
59             {\r
60                 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", \r
61                                 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
62             }\r
63 \r
64             btn_playQT.Enabled = false;\r
65             btn_playVLC.Enabled = false;\r
66             lbl_status.Text = "Encoding Sample for (VLC) ...";\r
67             int duration;\r
68             int.TryParse(cb_duration.Text, out duration);\r
69             string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);\r
70             ThreadPool.QueueUserWorkItem(ProcMonitor, query);\r
71         }\r
72 \r
73         private void btn_playQT_Click(object sender, EventArgs e)\r
74         {\r
75             if (NoQT)\r
76             {\r
77                 MessageBox.Show(this, \r
78                                 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.", \r
79                                 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
80                 return;\r
81             }\r
82             if (MainWindow.text_destination.Text.Contains(".mkv"))\r
83             {\r
84                 MessageBox.Show(this, \r
85                                 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.", \r
86                                 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
87             }\r
88             else\r
89             {\r
90                 lbl_status.Visible = true;\r
91                 try\r
92                 {\r
93                     QTControl.URL = string.Empty;\r
94                     if (File.Exists(CurrentlyPlaying))\r
95                         File.Delete(CurrentlyPlaying);\r
96                 }\r
97                 catch (Exception)\r
98                 {\r
99                     MessageBox.Show(this, \r
100                                     "Unable to delete previous preview file. You may need to restart the application.", \r
101                                     "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
102                 }\r
103 \r
104                 btn_playQT.Enabled = false;\r
105                 btn_playVLC.Enabled = false;\r
106                 lbl_status.Text = "Encoding Sample for (QT) ...";\r
107                 int duration;\r
108                 int.TryParse(cb_duration.Text, out duration);\r
109                 string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);\r
110 \r
111                 ThreadPool.QueueUserWorkItem(ProcMonitor, query);\r
112             }\r
113         }\r
114 \r
115         private void ProcMonitor(object state)\r
116         {\r
117             // Make sure we are not already encoding and if we are then display an error.\r
118             if (Process.HbProcess != null)\r
119                 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, \r
120                                 MessageBoxIcon.Warning);\r
121             else\r
122             {\r
123                 Process.CreatePreviewSample((string) state);\r
124                 if (Process.HbProcess != null)\r
125                 {\r
126                     Process.HbProcess.WaitForExit();\r
127                     Process.HbProcess = null;\r
128                 }\r
129                 EncodeCompleted();\r
130             }\r
131         }\r
132 \r
133         private void EncodeCompleted()\r
134         {\r
135             try\r
136             {\r
137                 if (InvokeRequired)\r
138                 {\r
139                     BeginInvoke(new UpdateUIHandler(EncodeCompleted));\r
140                     return;\r
141                 }\r
142                 if (!NoQT)\r
143                     btn_playQT.Enabled = true;\r
144                 btn_playVLC.Enabled = true;\r
145 \r
146                 // Decide which Player to use.\r
147                 string playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";\r
148 \r
149                 lbl_status.Text = "Loading Clip ...";\r
150 \r
151                 // Get the sample filename\r
152                 if (MainWindow.text_destination.Text != string.Empty)\r
153                     CurrentlyPlaying =\r
154                         MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").\r
155                             Replace(".mkv", "_sample.mkv");\r
156                 ;\r
157 \r
158                 // Play back in QT or VLC\r
159                 if (playerSelection == "QT")\r
160                     Play();\r
161                 else\r
162                     PlayVLC();\r
163 \r
164                 lbl_status.Text = string.Empty;\r
165             }\r
166             catch (Exception exc)\r
167             {\r
168                 MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK, \r
169                                 MessageBoxIcon.Error);\r
170             }\r
171         }\r
172 \r
173         #endregion\r
174 \r
175         #region Playback\r
176 \r
177         /// <summary>\r
178         /// Play the video back in the QuickTime control\r
179         /// </summary>\r
180         private void Play()\r
181         {\r
182             Player = new Thread(OpenMovie) {IsBackground = true};\r
183             Player.Start();\r
184             lbl_status.Visible = false;\r
185         }\r
186 \r
187         /// <summary>\r
188         /// Play the video back in an external VLC Player\r
189         /// </summary>\r
190         private void PlayVLC()\r
191         {\r
192             // Launch VLC and Play video.\r
193             if (CurrentlyPlaying != string.Empty)\r
194             {\r
195                 if (File.Exists(CurrentlyPlaying))\r
196                 {\r
197                     // Attempt to find VLC if it doesn't exist in the default set location.\r
198                     string vlcPath;\r
199 \r
200                     if (8 == IntPtr.Size ||\r
201                         (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))\r
202                         vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");\r
203                     else\r
204                         vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");\r
205 \r
206                     vlcPath = vlcPath != null\r
207                                   ? vlcPath + @"\VideoLAN\VLC\vlc.exe"\r
208                                   : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";\r
209 \r
210                     if (!File.Exists(Properties.Settings.Default.VLC_Path))\r
211                     {\r
212                         if (File.Exists(vlcPath))\r
213                         {\r
214                             Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";\r
215                             Properties.Settings.Default.Save(); // Save this new path if it does\r
216                         }\r
217                         else\r
218                         {\r
219                             MessageBox.Show(this, \r
220                                             "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\") ", \r
221                                             "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
222                         }\r
223                     }\r
224 \r
225                     if (File.Exists(Properties.Settings.Default.VLC_Path))\r
226                     {\r
227                         string args = "\"" + CurrentlyPlaying + "\"";\r
228                         ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
229                         System.Diagnostics.Process.Start(vlc);\r
230                         lbl_status.Text = "VLC will now launch.";\r
231                     }\r
232                 }\r
233                 else\r
234                     MessageBox.Show(this, \r
235                                     "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", \r
236                                     "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
237             }\r
238             lbl_status.Visible = false;\r
239         }\r
240 \r
241         /// <summary>\r
242         /// QT control - Open the file\r
243         /// </summary>\r
244         [STAThread]\r
245         private void OpenMovie()\r
246         {\r
247             try\r
248             {\r
249                 if (InvokeRequired)\r
250                 {\r
251                     BeginInvoke(new UpdateUIHandler(OpenMovie));\r
252                     return;\r
253                 }\r
254                 QTControl.URL = CurrentlyPlaying;\r
255                 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);\r
256                 QTControl.URL = CurrentlyPlaying;\r
257                 QTControl.Show();\r
258 \r
259                 this.ClientSize = QTControl.Size;\r
260                 this.Height += 25;\r
261             }\r
262             catch (COMException ex)\r
263             {\r
264                 QTUtils qtu = new QTUtils();\r
265                 MessageBox.Show(this, \r
266                                 "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") +\r
267                                 "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT", \r
268                                 MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
269             }\r
270             catch (Exception ex)\r
271             {\r
272                 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, \r
273                                 MessageBoxIcon.Warning);\r
274             }\r
275         }\r
276 \r
277         #endregion\r
278     }\r
279 }