OSDN Git Service

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