OSDN Git Service

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