OSDN Git Service

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