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
31 catch (Exception exc)
\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
37 this.mainWindow = mw;
\r
38 cb_preview.SelectedIndex = 0;
\r
39 cb_duration.SelectedIndex = 1;
\r
42 #region Encode Sample
\r
43 private void btn_playVLC_Click(object sender, EventArgs e)
\r
45 lbl_status.Visible = true;
\r
49 if (File.Exists(currently_playing))
\r
50 File.Delete(currently_playing);
\r
54 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
57 btn_playQT.Enabled = false;
\r
58 btn_playVLC.Enabled = false;
\r
59 lbl_status.Text = "Encoding Sample for (VLC) ...";
\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
65 private void btn_playQT_Click(object sender, EventArgs e)
\r
67 if (mainWindow.text_destination.Text.Contains(".mkv"))
\r
69 MessageBox.Show(this,
\r
70 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",
\r
71 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
75 lbl_status.Visible = true;
\r
79 if (File.Exists(currently_playing))
\r
80 File.Delete(currently_playing);
\r
84 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
87 btn_playQT.Enabled = false;
\r
88 btn_playVLC.Enabled = false;
\r
89 lbl_status.Text = "Encoding Sample for (QT) ...";
\r
91 int.TryParse(cb_duration.Text, out duration);
\r
92 String query = hb_common_func.GenerateCLIQuery(mainWindow, duration, cb_preview.Text);
\r
94 ThreadPool.QueueUserWorkItem(procMonitor, query);
\r
97 private void procMonitor(object state)
\r
99 // Make sure we are not already encoding and if we are then display an error.
\r
100 if (process.hbProcess != null)
\r
101 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
104 process.RunCli((string)state);
\r
105 if (process.hbProcess != null)
\r
107 process.hbProcess.WaitForExit();
\r
108 process.hbProcess = null;
\r
113 private void encodeCompleted()
\r
117 if (InvokeRequired)
\r
119 BeginInvoke(new UpdateUIHandler(encodeCompleted));
\r
123 btn_playQT.Enabled = true;
\r
124 btn_playVLC.Enabled = true;
\r
126 // Decide which player to use.
\r
127 String playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";
\r
129 lbl_status.Text = "Loading Clip ...";
\r
131 // Get the sample filename
\r
132 if (mainWindow.text_destination.Text != "")
\r
133 currently_playing = mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").Replace(".mkv", "_sample.mkv"); ;
\r
135 // Play back in QT or VLC
\r
136 if (playerSelection == "QT")
\r
141 lbl_status.Text = "";
\r
143 catch (Exception exc)
\r
145 MessageBox.Show(this, "frmPreview.cs encodeCompleted " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
153 /// Play the video back in the QuickTime control
\r
155 private void play()
\r
157 player = new Thread(OpenMovie) { IsBackground = true };
\r
159 lbl_status.Visible = false;
\r
163 /// Play the video back in an external VLC player
\r
165 private void playVLC()
\r
167 // Launch VLC and play video.
\r
168 if (currently_playing != "")
\r
170 if (File.Exists(currently_playing))
\r
172 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
174 String args = "\"" + currently_playing + "\"";
\r
175 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
176 Process.Start(vlc);
\r
177 lbl_status.Text = "VLC will now launch.";
\r
180 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
183 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
185 lbl_status.Visible = false;
\r
189 /// QT control - Open the file
\r
192 private void OpenMovie()
\r
196 if (InvokeRequired)
\r
198 BeginInvoke(new UpdateUIHandler(OpenMovie));
\r
201 QTControl.URL = currently_playing;
\r
202 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
203 QTControl.URL = currently_playing;
\r
206 this.ClientSize = QTControl.Size;
\r
209 catch (COMException ex)
\r
211 QTUtils qtu = new QTUtils();
\r
212 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
214 catch (Exception ex)
\r
216 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r