OSDN Git Service

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