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