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
27 #region Private Variables
\r
31 private readonly frmMain mainWindow;
\r
34 /// True if QT is not installed
\r
36 private readonly bool noQT;
\r
39 /// The encode queue
\r
41 private readonly IQueue encodeQueue = new Queue();
\r
44 /// What is currently playing
\r
46 private string currentlyPlaying = string.Empty;
\r
49 /// Play With VLC tracker
\r
51 private bool playWithVLC;
\r
54 /// A Thread for the video player
\r
56 private Thread player;
\r
59 /// Initializes a new instance of the <see cref="frmPreview"/> class.
\r
61 /// <param name="mw">
\r
64 public frmPreview(frmMain mw)
\r
68 InitializeComponent();
\r
74 this.mainWindow = mw;
\r
75 cb_preview.SelectedIndex = 0;
\r
76 cb_duration.SelectedIndex = 1;
\r
78 cb_preview.Items.Clear();
\r
79 for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)
\r
80 cb_preview.Items.Add(i.ToString());
\r
81 cb_preview.SelectedIndex = 0;
\r
83 encodeQueue.EncodeStarted += this.EncodeQueue_EncodeStarted;
\r
84 encodeQueue.EncodeEnded += this.EncodeQueue_EncodeEnded;
\r
88 /// Update UI Delegate
\r
90 /// <param name="sender">
\r
93 /// <param name="e">
\r
96 private delegate void UpdateUIHandler(object sender, EventArgs e);
\r
99 /// The Open Movie Handler
\r
101 private delegate void OpenMovieHandler();
\r
106 /// The encode has started
\r
108 /// <param name="sender">
\r
111 /// <param name="e">
\r
114 private void EncodeQueue_EncodeStarted(object sender, EventArgs e)
\r
116 encodeQueue.EncodeStatusChanged += this.EncodeQueue_EncodeStatusChanged;
\r
120 /// The Enocde has ended
\r
122 /// <param name="sender">
\r
125 /// <param name="e">
\r
128 private void EncodeQueue_EncodeEnded(object sender, EventArgs e)
\r
130 encodeQueue.EncodeStatusChanged -= this.EncodeQueue_EncodeStatusChanged;
\r
134 if (this.InvokeRequired)
\r
136 this.BeginInvoke(new UpdateUIHandler(EncodeQueue_EncodeEnded), new[] { sender, e });
\r
140 ProgressBarStatus.Visible = false;
\r
141 lbl_encodeStatus.Visible = false;
\r
144 btn_playQT.Enabled = true;
\r
145 btn_playVLC.Enabled = true;
\r
147 this.Text = this.Text.Replace(" (Encoding)", string.Empty);
\r
149 // Get the sample filename
\r
150 if (this.mainWindow.text_destination.Text != string.Empty)
\r
151 this.currentlyPlaying =
\r
152 this.mainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").
\r
153 Replace(".mkv", "_sample.mkv");
\r
155 // Play back in QT or VLC
\r
161 catch (Exception exc)
\r
163 Main.ShowExceptiowWindow("An Unexpected error has occured", exc.ToString());
\r
168 /// Encode status has changed
\r
170 /// <param name="sender">
\r
173 /// <param name="e">
\r
176 private void EncodeQueue_EncodeStatusChanged(object sender, HandBrake.ApplicationServices.EncodeProgressEventArgs e)
\r
178 if (this.InvokeRequired)
\r
180 this.BeginInvoke(new Encode.EncodeProgessStatus(this.EncodeQueue_EncodeStatusChanged), new[] { sender, e });
\r
184 lbl_encodeStatus.Text = e.PercentComplete + "%";
\r
185 ProgressBarStatus.Value = (int)Math.Round(e.PercentComplete);
\r
188 #region Encode Sample
\r
193 /// <param name="sender">
\r
196 /// <param name="e">
\r
199 private void PlayVLC_Click(object sender, EventArgs e)
\r
201 ProgressBarStatus.Visible = true;
\r
202 ProgressBarStatus.Value = 0;
\r
203 lbl_encodeStatus.Visible = true;
\r
204 playWithVLC = true;
\r
209 QTControl.URL = string.Empty;
\r
211 if (File.Exists(this.currentlyPlaying))
\r
212 File.Delete(this.currentlyPlaying);
\r
216 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",
\r
217 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
220 btn_playQT.Enabled = false;
\r
221 btn_playVLC.Enabled = false;
\r
222 this.Text += " (Encoding)";
\r
224 int.TryParse(cb_duration.Text, out duration);
\r
225 string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);
\r
226 ThreadPool.QueueUserWorkItem(this.CreatePreview, query);
\r
230 /// Encode and Play with QT
\r
232 /// <param name="sender">
\r
235 /// <param name="e">
\r
238 private void PlayQT_Click(object sender, EventArgs e)
\r
240 playWithVLC = false;
\r
243 MessageBox.Show(this,
\r
244 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",
\r
245 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
248 if (this.mainWindow.text_destination.Text.Contains(".mkv"))
\r
250 MessageBox.Show(this,
\r
251 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",
\r
252 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
256 ProgressBarStatus.Visible = true;
\r
257 ProgressBarStatus.Value = 0;
\r
258 lbl_encodeStatus.Visible = true;
\r
261 QTControl.URL = string.Empty;
\r
262 if (File.Exists(this.currentlyPlaying))
\r
263 File.Delete(this.currentlyPlaying);
\r
267 MessageBox.Show(this,
\r
268 "Unable to delete previous preview file. You may need to restart the application.",
\r
269 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
272 btn_playQT.Enabled = false;
\r
273 btn_playVLC.Enabled = false;
\r
274 this.Text += " (Encoding)";
\r
276 int.TryParse(cb_duration.Text, out duration);
\r
277 string query = QueryGenerator.GeneratePreviewQuery(this.mainWindow, duration, cb_preview.Text);
\r
279 ThreadPool.QueueUserWorkItem(this.CreatePreview, query);
\r
284 /// Create the Preview.
\r
286 /// <param name="state">
\r
289 private void CreatePreview(object state)
\r
291 // Make sure we are not already encoding and if we are then display an error.
\r
292 if (encodeQueue.IsEncoding)
\r
296 "Handbrake is already encoding a video!",
\r
298 MessageBoxButtons.OK,
\r
299 MessageBoxIcon.Warning);
\r
304 encodeQueue.CreatePreviewSample((string)state);
\r
312 /// Play the video back in the QuickTime control
\r
314 private void Play()
\r
316 this.player = new Thread(OpenMovie) { IsBackground = true };
\r
317 this.player.Start();
\r
321 /// Play the video back in an external VLC Player
\r
323 private void PlayVLC()
\r
325 // Launch VLC and Play video.
\r
326 if (this.currentlyPlaying != string.Empty)
\r
328 if (File.Exists(this.currentlyPlaying))
\r
330 // Attempt to find VLC if it doesn't exist in the default set location.
\r
333 if (8 == IntPtr.Size ||
\r
334 (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
\r
335 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
\r
337 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");
\r
339 vlcPath = vlcPath != null
\r
340 ? vlcPath + @"\VideoLAN\VLC\vlc.exe"
\r
341 : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
\r
343 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
345 if (File.Exists(vlcPath))
\r
347 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
348 Properties.Settings.Default.Save(); // Save this new path if it does
\r
352 MessageBox.Show(this,
\r
353 "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
354 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
358 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
360 string args = "\"" + this.currentlyPlaying + "\"";
\r
361 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
362 Process.Start(vlc);
\r
366 MessageBox.Show(this,
\r
367 "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",
\r
368 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
373 /// QT control - Open the file
\r
376 private void OpenMovie()
\r
380 if (InvokeRequired)
\r
382 BeginInvoke(new OpenMovieHandler(OpenMovie));
\r
385 QTControl.URL = this.currentlyPlaying;
\r
386 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
387 QTControl.URL = this.currentlyPlaying;
\r
390 this.ClientSize = QTControl.Size;
\r
393 catch (COMException ex)
\r
395 QTUtils qtu = new QTUtils();
\r
396 Main.ShowExceptiowWindow("Unable to open movie.", ex + Environment.NewLine + qtu.QTErrorFromErrorCode(ex.ErrorCode));
\r
398 catch (Exception ex)
\r
400 Main.ShowExceptiowWindow("Unable to open movie.", ex.ToString());
\r
407 /// Remove any subscribed events then close.
\r
409 /// <param name="e">
\r
412 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
\r
414 encodeQueue.EncodeStarted -= this.EncodeQueue_EncodeStarted;
\r
415 encodeQueue.EncodeEnded -= this.EncodeQueue_EncodeEnded;
\r