OSDN Git Service

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