2 using System.Windows.Forms;
\r
3 using System.Threading;
\r
4 using System.Diagnostics;
\r
5 using System.Runtime.InteropServices;
\r
7 using Handbrake.EncodeQueue;
\r
8 using Handbrake.Functions;
\r
14 public partial class frmPreview : Form
\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
25 public frmPreview(frmMain mw)
\r
29 InitializeComponent();
\r
35 this.mainWindow = mw;
\r
36 cb_preview.SelectedIndex = 0;
\r
37 cb_duration.SelectedIndex = 1;
\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
44 #region Encode Sample
\r
45 private void btn_playVLC_Click(object sender, EventArgs e)
\r
47 lbl_status.Visible = true;
\r
53 if (File.Exists(currently_playing))
\r
54 File.Delete(currently_playing);
\r
58 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
61 btn_playQT.Enabled = false;
\r
62 btn_playVLC.Enabled = false;
\r
63 lbl_status.Text = "Encoding Sample for (VLC) ...";
\r
65 int.TryParse(cb_duration.Text, out duration);
\r
66 String query = hb_common_func.GenerateCLIQuery(mainWindow, duration, cb_preview.Text);
\r
67 ThreadPool.QueueUserWorkItem(procMonitor, query);
\r
69 private void btn_playQT_Click(object sender, EventArgs e)
\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
76 if (mainWindow.text_destination.Text.Contains(".mkv"))
\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
84 lbl_status.Visible = true;
\r
88 if (File.Exists(currently_playing))
\r
89 File.Delete(currently_playing);
\r
93 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
96 btn_playQT.Enabled = false;
\r
97 btn_playVLC.Enabled = false;
\r
98 lbl_status.Text = "Encoding Sample for (QT) ...";
\r
100 int.TryParse(cb_duration.Text, out duration);
\r
101 String query = hb_common_func.GenerateCLIQuery(mainWindow, duration, cb_preview.Text);
\r
103 ThreadPool.QueueUserWorkItem(procMonitor, query);
\r
106 private void procMonitor(object state)
\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
113 process.RunCli((string)state);
\r
114 if (process.hbProcess != null)
\r
116 process.hbProcess.WaitForExit();
\r
117 process.hbProcess = null;
\r
122 private void encodeCompleted()
\r
126 if (InvokeRequired)
\r
128 BeginInvoke(new UpdateUIHandler(encodeCompleted));
\r
132 btn_playQT.Enabled = true;
\r
133 btn_playVLC.Enabled = true;
\r
135 // Decide which player to use.
\r
136 String playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";
\r
138 lbl_status.Text = "Loading Clip ...";
\r
140 // Get the sample filename
\r
141 if (mainWindow.text_destination.Text != "")
\r
142 currently_playing = mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").Replace(".mkv", "_sample.mkv"); ;
\r
144 // Play back in QT or VLC
\r
145 if (playerSelection == "QT")
\r
150 lbl_status.Text = "";
\r
152 catch (Exception exc)
\r
154 MessageBox.Show(this, "frmPreview.cs encodeCompleted " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
162 /// Play the video back in the QuickTime control
\r
164 private void play()
\r
166 player = new Thread(OpenMovie) { IsBackground = true };
\r
168 lbl_status.Visible = false;
\r
172 /// Play the video back in an external VLC player
\r
174 private void playVLC()
\r
176 // Launch VLC and play video.
\r
177 if (currently_playing != "")
\r
179 if (File.Exists(currently_playing))
\r
181 // Attempt to find VLC if it doesn't exist in the default set location.
\r
182 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
184 if (File.Exists("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe"))
\r
186 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
187 Properties.Settings.Default.Save(); // Save this new path if it does
\r
191 MessageBox.Show(this,
\r
192 "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
193 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
197 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
199 String args = "\"" + currently_playing + "\"";
\r
200 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
201 Process.Start(vlc);
\r
202 lbl_status.Text = "VLC will now launch.";
\r
207 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 lbl_status.Visible = false;
\r
213 /// QT control - Open the file
\r
216 private void OpenMovie()
\r
220 if (InvokeRequired)
\r
222 BeginInvoke(new UpdateUIHandler(OpenMovie));
\r
225 QTControl.URL = currently_playing;
\r
226 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
227 QTControl.URL = currently_playing;
\r
230 this.ClientSize = QTControl.Size;
\r
233 catch (COMException ex)
\r
235 QTUtils qtu = new QTUtils();
\r
236 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 catch (Exception ex)
\r
240 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r