OSDN Git Service

dabde2bb1a4f997c6ebb5a357a0f32023d4684ff
[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 QTOLibrary;\r
8 \r
9 namespace Handbrake\r
10 {\r
11     public partial class frmPreview : Form\r
12     {\r
13 \r
14         QueryGenerator hb_common_func = new QueryGenerator();\r
15         Functions.Encode process = new Functions.Encode();\r
16         private delegate void UpdateUIHandler();\r
17         String currently_playing = "";\r
18         readonly frmMain mainWindow;\r
19         private Process hbProc;\r
20         private Thread player;\r
21 \r
22         public frmPreview(frmMain mw)\r
23         {\r
24             InitializeComponent();\r
25             this.mainWindow = mw;\r
26             cb_preview.SelectedIndex = 0;\r
27             cb_duration.SelectedIndex = 1;\r
28         }\r
29  \r
30         #region Encode Sample\r
31         private void btn_playVLC_Click(object sender, EventArgs e)\r
32         {\r
33             btn_playQT.Enabled = false;\r
34             btn_playVLC.Enabled = false;\r
35             lbl_status.Text = "Encoding Sample for (VLC) ...";\r
36             String query = hb_common_func.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
37             ThreadPool.QueueUserWorkItem(procMonitor, query);  \r
38         }\r
39         private void btn_playQT_Click(object sender, EventArgs e)\r
40         {\r
41             btn_playQT.Enabled = false;\r
42             btn_playVLC.Enabled = false;\r
43             lbl_status.Text = "Encoding Sample for (QT) ...";\r
44             String query = hb_common_func.GeneratePreviewQuery(mainWindow, cb_duration.Text, cb_preview.Text);\r
45             ThreadPool.QueueUserWorkItem(procMonitor, query);\r
46         }\r
47         private void procMonitor(object state)\r
48         {\r
49             // Make sure we are not already encoding and if we are then display an error.\r
50             if (hbProc != null)\r
51                 MessageBox.Show("Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
52             else\r
53             {\r
54                 hbProc = process.runCli(this, (string)state);\r
55                 hbProc.WaitForExit();\r
56                 hbProc = null;\r
57                 encodeCompleted();\r
58             }\r
59         }\r
60         private void encodeCompleted()\r
61         {\r
62             try\r
63             {\r
64                 if (InvokeRequired)\r
65                 {\r
66                     BeginInvoke(new UpdateUIHandler(encodeCompleted));\r
67                     return;\r
68                 }\r
69                 btn_playQT.Enabled = true;\r
70                 btn_playVLC.Enabled = true;\r
71 \r
72                 // Decide which player to use.\r
73                 String playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";\r
74 \r
75                 lbl_status.Text = "Loading Clip ...";\r
76 \r
77                 // Get the sample filename\r
78                 if (mainWindow.text_destination.Text != "")\r
79                     currently_playing = mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").Replace(".avi", "_sample.avi").Replace(".ogm", "_sample.ogm");\r
80 \r
81                 // Play back in QT or VLC\r
82                 if (playerSelection == "QT")\r
83                     play();\r
84                 else \r
85                     playVLC();\r
86                 \r
87                 lbl_status.Text = "";\r
88             }\r
89             catch (Exception exc)\r
90             {\r
91                 MessageBox.Show("frmPreview.cs encodeCompleted " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
92             }\r
93         }\r
94         #endregion\r
95 \r
96         #region Playback\r
97 \r
98         /// <summary>\r
99         /// Play the video back in the QuickTime control\r
100         /// </summary>\r
101         private void play()\r
102         {\r
103             player = new Thread(OpenMovie) { IsBackground = true };\r
104             player.Start();\r
105         }\r
106 \r
107         /// <summary>\r
108         /// Play the video back in an external VLC player\r
109         /// </summary>\r
110         private void playVLC()\r
111         {\r
112             // Launch VLC and play video.\r
113             if (currently_playing != "")\r
114             {\r
115                 if (File.Exists(currently_playing))\r
116                 {\r
117                     if (File.Exists(Properties.Settings.Default.VLC_Path))\r
118                     {\r
119                         String args = "\"" + currently_playing + "\"";\r
120                         ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);\r
121                         Process.Start(vlc);\r
122                         lbl_status.Text = "VLC will now launch.";\r
123                     }\r
124                     else\r
125                         MessageBox.Show("Unable to detect VLC Player. \nPlease make sure VLC is installed and the directory specified in the program options is correct.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
126                 }\r
127                 else\r
128                     MessageBox.Show("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
129             }\r
130         }\r
131 \r
132         /// <summary>\r
133         /// QT control - Open the file\r
134         /// </summary>\r
135         [STAThread]\r
136         private void OpenMovie()\r
137         {\r
138             try\r
139             {\r
140                 if (InvokeRequired)\r
141                 {\r
142                     BeginInvoke(new UpdateUIHandler(OpenMovie));\r
143                     return;\r
144                 }\r
145                 QTControl.URL = currently_playing;\r
146                 QTControl.Width = QTControl.Movie.Width;\r
147                 QTControl.Height = QTControl.Movie.Height;\r
148                 // The initial control size is 64,64. If we do not reload the clip here\r
149                 // it'll scale the video from 64,64. \r
150                 // Unsure why as it correctly resizes the control to the movies actual size.\r
151                 QTControl.URL = currently_playing;\r
152                 QTControl.SetScale(0);\r
153                 QTControl.Show();\r
154 \r
155                 this.Width = QTControl.Width + 5;\r
156                 this.Height = QTControl.Height + 90;\r
157             }\r
158             catch (COMException ex)\r
159             {\r
160                 QTUtils qtu = new QTUtils();\r
161                 MessageBox.Show("Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") + "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode));\r
162             }\r
163             catch (Exception ex)\r
164             {\r
165                 MessageBox.Show("Unable to open movie:\n\n" + ex);\r
166             }\r
167         }\r
168         #endregion\r
169     }\r
170 }