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
181 private void EncodeCompleted()
\r
185 if (InvokeRequired)
\r
187 BeginInvoke(new UpdateUIHandler(EncodeCompleted));
\r
191 ProgressBarStatus.Visible = false;
\r
192 lbl_encodeStatus.Visible = false;
\r
195 btn_playQT.Enabled = true;
\r
196 btn_playVLC.Enabled = true;
\r
198 this.Text = this.Text.Replace(" (Encoding)", string.Empty);
\r
200 // Get the sample filename
\r
201 if (MainWindow.text_destination.Text != string.Empty)
\r
203 MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").
\r
204 Replace(".mkv", "_sample.mkv");
\r
206 // Play back in QT or VLC
\r
212 catch (Exception exc)
\r
214 MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK,
\r
215 MessageBoxIcon.Error);
\r
224 /// Play the video back in the QuickTime control
\r
226 private void Play()
\r
228 Player = new Thread(OpenMovie) {IsBackground = true};
\r
233 /// Play the video back in an external VLC Player
\r
235 private void PlayVLC()
\r
237 // Launch VLC and Play video.
\r
238 if (CurrentlyPlaying != string.Empty)
\r
240 if (File.Exists(CurrentlyPlaying))
\r
242 // Attempt to find VLC if it doesn't exist in the default set location.
\r
245 if (8 == IntPtr.Size ||
\r
246 (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
\r
247 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
\r
249 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");
\r
251 vlcPath = vlcPath != null
\r
252 ? vlcPath + @"\VideoLAN\VLC\vlc.exe"
\r
253 : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
\r
255 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
257 if (File.Exists(vlcPath))
\r
259 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
260 Properties.Settings.Default.Save(); // Save this new path if it does
\r
264 MessageBox.Show(this,
\r
265 "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
266 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
270 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
272 string args = "\"" + CurrentlyPlaying + "\"";
\r
273 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
274 System.Diagnostics.Process.Start(vlc);
\r
278 MessageBox.Show(this,
\r
279 "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",
\r
280 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
285 /// QT control - Open the file
\r
288 private void OpenMovie()
\r
292 if (InvokeRequired)
\r
294 BeginInvoke(new UpdateUIHandler(OpenMovie));
\r
297 QTControl.URL = CurrentlyPlaying;
\r
298 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
299 QTControl.URL = CurrentlyPlaying;
\r
302 this.ClientSize = QTControl.Size;
\r
305 catch (COMException ex)
\r
307 QTUtils qtu = new QTUtils();
\r
308 MessageBox.Show(this,
\r
309 "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") +
\r
310 "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT",
\r
311 MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
313 catch (Exception ex)
\r
315 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK,
\r
316 MessageBoxIcon.Warning);
\r
322 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
\r
324 Process.EncodeStarted -= Process_EncodeStarted;
\r