2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr/>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
9 using System.Diagnostics;
\r
11 using System.Runtime.InteropServices;
\r
12 using System.Threading;
\r
13 using System.Windows.Forms;
\r
16 using HandBrake.ApplicationServices.Services;
\r
17 using HandBrake.ApplicationServices.Services.Interfaces;
\r
19 using QTOControlLib;
\r
23 /// The Preview Window
\r
25 public partial class frmPreview : Form
\r
30 private readonly frmMain mainWindow;
\r
33 /// True if QT is not installed
\r
35 private readonly bool noQT;
\r
38 /// The encode queue
\r
40 private readonly IQueue encodeQueue = new Queue();
\r
43 /// What is currently playing
\r
45 private string currentlyPlaying = string.Empty;
\r
48 /// Play With VLC tracker
\r
50 private bool playWithVLC;
\r
53 /// A Thread for the video player
\r
55 private Thread player;
\r
58 /// Initializes a new instance of the <see cref="frmPreview"/> class.
\r
60 /// <param name="mw">
\r
63 public frmPreview(frmMain mw)
\r
67 InitializeComponent();
\r
73 this.mainWindow = mw;
\r
74 cb_preview.SelectedIndex = 0;
\r
75 cb_duration.SelectedIndex = 1;
\r
77 cb_preview.Items.Clear();
\r
78 for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)
\r
79 cb_preview.Items.Add(i.ToString());
\r
80 cb_preview.SelectedIndex = 0;
\r
82 encodeQueue.EncodeStarted += this.EncodeQueue_EncodeStarted;
\r
83 encodeQueue.EncodeEnded += this.EncodeQueue_EncodeEnded;
\r
87 /// Update UI Delegate
\r
89 /// <param name="sender">
\r
92 /// <param name="e">
\r
95 private delegate void UpdateUIHandler(object sender, EventArgs e);
\r
98 /// The Open Movie Handler
\r
100 private delegate void OpenMovieHandler();
\r
103 /// The encode has started
\r
105 /// <param name="sender">
\r
108 /// <param name="e">
\r
111 private void EncodeQueue_EncodeStarted(object sender, EventArgs e)
\r
113 encodeQueue.EncodeStatusChanged += this.EncodeQueue_EncodeStatusChanged;
\r
117 /// The Enocde has ended
\r
119 /// <param name="sender">
\r
122 /// <param name="e">
\r
125 private void EncodeQueue_EncodeEnded(object sender, EventArgs e)
\r
127 encodeQueue.EncodeStatusChanged -= this.EncodeQueue_EncodeStatusChanged;
\r
131 if (this.InvokeRequired)
\r
133 this.BeginInvoke(new UpdateUIHandler(EncodeQueue_EncodeEnded), new[] { sender, e });
\r
137 ProgressBarStatus.Visible = false;
\r
138 lbl_encodeStatus.Visible = false;
\r
141 btn_playQT.Enabled = true;
\r
142 btn_playVLC.Enabled = true;
\r
144 this.Text = this.Text.Replace(" (Encoding)", string.Empty);
\r
146 // Get the sample filename
\r
147 if (this.mainWindow.text_destination.Text != string.Empty)
\r
148 this.currentlyPlaying =
\r
149 this.mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").
\r
150 Replace(".mkv", "_sample.mkv");
\r
152 // Play back in QT or VLC
\r
158 catch (Exception exc)
\r
160 Main.ShowExceptiowWindow("An Unexpected error has occured", exc.ToString());
\r
165 /// Encode status has changed
\r
167 /// <param name="sender">
\r
170 /// <param name="e">
\r
173 private void EncodeQueue_EncodeStatusChanged(object sender, HandBrake.ApplicationServices.EncodeProgressEventArgs e)
\r
175 if (this.InvokeRequired)
\r
177 this.BeginInvoke(new Encode.EncodeProgessStatus(this.EncodeQueue_EncodeStatusChanged), new[] { sender, e });
\r
181 lbl_encodeStatus.Text = e.PercentComplete + "%";
\r
182 ProgressBarStatus.Value = (int)Math.Round(e.PercentComplete);
\r
185 #region Encode Sample
\r
190 /// <param name="sender">
\r
193 /// <param name="e">
\r
196 private void PlayVLC_Click(object sender, EventArgs e)
\r
198 ProgressBarStatus.Visible = true;
\r
199 ProgressBarStatus.Value = 0;
\r
200 lbl_encodeStatus.Visible = true;
\r
201 playWithVLC = true;
\r
206 QTControl.URL = string.Empty;
\r
208 if (File.Exists(this.currentlyPlaying))
\r
209 File.Delete(this.currentlyPlaying);
\r
213 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",
\r
214 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
217 btn_playQT.Enabled = false;
\r
218 btn_playVLC.Enabled = false;
\r
219 this.Text += " (Encoding)";
\r
221 int.TryParse(cb_duration.Text, out duration);
\r
222 string query = QueryGenerator.GenerateCliQuery(this.mainWindow, 3, duration, cb_preview.Text);
\r
223 ThreadPool.QueueUserWorkItem(this.CreatePreview, query);
\r
227 /// Encode and Play with QT
\r
229 /// <param name="sender">
\r
232 /// <param name="e">
\r
235 private void PlayQT_Click(object sender, EventArgs e)
\r
237 playWithVLC = false;
\r
240 MessageBox.Show(this,
\r
241 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",
\r
242 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
245 if (this.mainWindow.text_destination.Text.Contains(".mkv"))
\r
247 MessageBox.Show(this,
\r
248 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",
\r
249 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
253 ProgressBarStatus.Visible = true;
\r
254 ProgressBarStatus.Value = 0;
\r
255 lbl_encodeStatus.Visible = true;
\r
258 QTControl.URL = string.Empty;
\r
259 if (File.Exists(this.currentlyPlaying))
\r
260 File.Delete(this.currentlyPlaying);
\r
264 MessageBox.Show(this,
\r
265 "Unable to delete previous preview file. You may need to restart the application.",
\r
266 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
269 btn_playQT.Enabled = false;
\r
270 btn_playVLC.Enabled = false;
\r
271 this.Text += " (Encoding)";
\r
273 int.TryParse(cb_duration.Text, out duration);
\r
274 string query = QueryGenerator.GenerateCliQuery(this.mainWindow, 3, duration, cb_preview.Text);
\r
276 ThreadPool.QueueUserWorkItem(this.CreatePreview, query);
\r
281 /// Create the Preview.
\r
283 /// <param name="state">
\r
286 private void CreatePreview(object state)
\r
288 // Make sure we are not already encoding and if we are then display an error.
\r
289 if (encodeQueue.IsEncoding)
\r
293 "Handbrake is already encoding a video!",
\r
295 MessageBoxButtons.OK,
\r
296 MessageBoxIcon.Warning);
\r
301 encodeQueue.CreatePreviewSample((string)state);
\r
309 /// Play the video back in the QuickTime control
\r
311 private void Play()
\r
313 this.player = new Thread(OpenMovie) { IsBackground = true };
\r
314 this.player.Start();
\r
318 /// Play the video back in an external VLC Player
\r
320 private void PlayVLC()
\r
322 // Launch VLC and Play video.
\r
323 if (this.currentlyPlaying != string.Empty)
\r
325 if (File.Exists(this.currentlyPlaying))
\r
327 // Attempt to find VLC if it doesn't exist in the default set location.
\r
330 if (8 == IntPtr.Size ||
\r
331 (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
\r
332 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
\r
334 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");
\r
336 vlcPath = vlcPath != null
\r
337 ? vlcPath + @"\VideoLAN\VLC\vlc.exe"
\r
338 : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
\r
340 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
342 if (File.Exists(vlcPath))
\r
344 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
345 Properties.Settings.Default.Save(); // Save this new path if it does
\r
349 MessageBox.Show(this,
\r
350 "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
351 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
355 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
357 string args = "\"" + this.currentlyPlaying + "\"";
\r
358 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
359 Process.Start(vlc);
\r
363 MessageBox.Show(this,
\r
364 "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",
\r
365 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
370 /// QT control - Open the file
\r
373 private void OpenMovie()
\r
377 if (InvokeRequired)
\r
379 BeginInvoke(new OpenMovieHandler(OpenMovie));
\r
382 QTControl.URL = this.currentlyPlaying;
\r
383 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
384 QTControl.URL = this.currentlyPlaying;
\r
387 this.ClientSize = QTControl.Size;
\r
390 catch (COMException ex)
\r
392 QTUtils qtu = new QTUtils();
\r
393 Main.ShowExceptiowWindow("Unable to open movie.", ex + Environment.NewLine + qtu.QTErrorFromErrorCode(ex.ErrorCode));
\r
395 catch (Exception ex)
\r
397 Main.ShowExceptiowWindow("Unable to open movie.", ex.ToString());
\r
404 /// Remove any subscribed events then close.
\r
406 /// <param name="e">
\r
409 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
\r
411 encodeQueue.EncodeStarted -= this.EncodeQueue_EncodeStarted;
\r
412 encodeQueue.EncodeEnded -= this.EncodeQueue_EncodeEnded;
\r