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
15 using QTOControlLib;
\r
20 public partial class frmPreview : Form
\r
22 private string CurrentlyPlaying = string.Empty;
\r
23 private readonly frmMain MainWindow;
\r
24 private Thread Player;
\r
25 private readonly bool NoQT;
\r
26 private readonly Queue Process = new Queue();
\r
27 private delegate void UpdateUIHandler();
\r
28 private bool playWithVLC;
\r
30 public frmPreview(frmMain mw)
\r
34 InitializeComponent();
\r
40 this.MainWindow = mw;
\r
41 cb_preview.SelectedIndex = 0;
\r
42 cb_duration.SelectedIndex = 1;
\r
44 cb_preview.Items.Clear();
\r
45 for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)
\r
46 cb_preview.Items.Add(i.ToString());
\r
47 cb_preview.SelectedIndex = 0;
\r
49 Process.EncodeStarted += new EventHandler(Process_EncodeStarted);
\r
51 private void Process_EncodeStarted(object sender, EventArgs e)
\r
53 Thread encodeMon = new Thread(EncodeMonitorThread);
\r
57 #region Encode Sample
\r
59 private void btn_playVLC_Click(object sender, EventArgs e)
\r
61 ProgressBarStatus.Visible = true;
\r
62 ProgressBarStatus.Value = 0;
\r
63 lbl_encodeStatus.Visible = true;
\r
69 QTControl.URL = string.Empty;
\r
71 if (File.Exists(CurrentlyPlaying))
\r
72 File.Delete(CurrentlyPlaying);
\r
76 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",
\r
77 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
80 btn_playQT.Enabled = false;
\r
81 btn_playVLC.Enabled = false;
\r
82 this.Text += " (Encoding)";
\r
84 int.TryParse(cb_duration.Text, out duration);
\r
85 string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);
\r
86 ThreadPool.QueueUserWorkItem(ProcMonitor, query);
\r
89 private void btn_playQT_Click(object sender, EventArgs e)
\r
91 playWithVLC = false;
\r
94 MessageBox.Show(this,
\r
95 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",
\r
96 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
99 if (MainWindow.text_destination.Text.Contains(".mkv"))
\r
101 MessageBox.Show(this,
\r
102 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",
\r
103 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
107 ProgressBarStatus.Visible = true;
\r
108 ProgressBarStatus.Value = 0;
\r
109 lbl_encodeStatus.Visible = true;
\r
112 QTControl.URL = string.Empty;
\r
113 if (File.Exists(CurrentlyPlaying))
\r
114 File.Delete(CurrentlyPlaying);
\r
118 MessageBox.Show(this,
\r
119 "Unable to delete previous preview file. You may need to restart the application.",
\r
120 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
123 btn_playQT.Enabled = false;
\r
124 btn_playVLC.Enabled = false;
\r
125 this.Text += " (Encoding)";
\r
127 int.TryParse(cb_duration.Text, out duration);
\r
128 string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);
\r
130 ThreadPool.QueueUserWorkItem(ProcMonitor, query);
\r
134 private void ProcMonitor(object state)
\r
136 // Make sure we are not already encoding and if we are then display an error.
\r
137 if (Process.HbProcess != null)
\r
138 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK,
\r
139 MessageBoxIcon.Warning);
\r
142 Process.CreatePreviewSample((string) state);
\r
144 if (Process.HbProcess != null)
\r
146 Process.HbProcess.WaitForExit();
\r
147 Process.HbProcess = null;
\r
153 private void EncodeMonitorThread()
\r
157 Parser encode = new Parser(Process.HbProcess.StandardOutput.BaseStream);
\r
158 encode.OnEncodeProgress += EncodeOnEncodeProgress;
\r
159 while (!encode.EndOfStream)
\r
160 encode.ReadEncodeStatus();
\r
162 catch (Exception exc)
\r
164 MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
168 private void EncodeOnEncodeProgress(object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, TimeSpan TimeRemaining)
\r
170 if (this.InvokeRequired)
\r
173 new EncodeProgressEventHandler(EncodeOnEncodeProgress),
\r
174 new[] { Sender, CurrentTask, TaskCount, PercentComplete, CurrentFps, AverageFps, TimeRemaining });
\r
177 lbl_encodeStatus.Text = PercentComplete + "%";
\r
178 ProgressBarStatus.Value = (int)Math.Round(PercentComplete);
\r
182 private void EncodeCompleted()
\r
186 if (InvokeRequired)
\r
188 BeginInvoke(new UpdateUIHandler(EncodeCompleted));
\r
192 ProgressBarStatus.Visible = false;
\r
193 lbl_encodeStatus.Visible = false;
\r
196 btn_playQT.Enabled = true;
\r
197 btn_playVLC.Enabled = true;
\r
199 this.Text = this.Text.Replace(" (Encoding)", string.Empty);
\r
201 // Get the sample filename
\r
202 if (MainWindow.text_destination.Text != string.Empty)
\r
204 MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").
\r
205 Replace(".mkv", "_sample.mkv");
\r
207 // Play back in QT or VLC
\r
213 catch (Exception exc)
\r
215 MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK,
\r
216 MessageBoxIcon.Error);
\r
225 /// Play the video back in the QuickTime control
\r
227 private void Play()
\r
229 Player = new Thread(OpenMovie) {IsBackground = true};
\r
234 /// Play the video back in an external VLC Player
\r
236 private void PlayVLC()
\r
238 // Launch VLC and Play video.
\r
239 if (CurrentlyPlaying != string.Empty)
\r
241 if (File.Exists(CurrentlyPlaying))
\r
243 // Attempt to find VLC if it doesn't exist in the default set location.
\r
246 if (8 == IntPtr.Size ||
\r
247 (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
\r
248 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
\r
250 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");
\r
252 vlcPath = vlcPath != null
\r
253 ? vlcPath + @"\VideoLAN\VLC\vlc.exe"
\r
254 : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
\r
256 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
258 if (File.Exists(vlcPath))
\r
260 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
261 Properties.Settings.Default.Save(); // Save this new path if it does
\r
265 MessageBox.Show(this,
\r
266 "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
267 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
271 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
273 string args = "\"" + CurrentlyPlaying + "\"";
\r
274 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
275 System.Diagnostics.Process.Start(vlc);
\r
279 MessageBox.Show(this,
\r
280 "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",
\r
281 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
286 /// QT control - Open the file
\r
289 private void OpenMovie()
\r
293 if (InvokeRequired)
\r
295 BeginInvoke(new UpdateUIHandler(OpenMovie));
\r
298 QTControl.URL = CurrentlyPlaying;
\r
299 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
300 QTControl.URL = CurrentlyPlaying;
\r
303 this.ClientSize = QTControl.Size;
\r
306 catch (COMException ex)
\r
308 QTUtils qtu = new QTUtils();
\r
309 MessageBox.Show(this,
\r
310 "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") +
\r
311 "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT",
\r
312 MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
314 catch (Exception ex)
\r
316 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK,
\r
317 MessageBoxIcon.Warning);
\r
323 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
\r
325 Process.EncodeStarted -= Process_EncodeStarted;
\r