1 using Handbrake.Parsing;
\r
6 using System.Diagnostics;
\r
8 using System.Runtime.InteropServices;
\r
9 using System.Threading;
\r
10 using System.Windows.Forms;
\r
12 using QTOControlLib;
\r
16 public partial class frmPreview : Form
\r
18 private readonly Queue Process = new Queue();
\r
20 private delegate void UpdateUIHandler();
\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
27 public frmPreview(frmMain mw)
\r
31 InitializeComponent();
\r
37 this.MainWindow = mw;
\r
38 cb_preview.SelectedIndex = 0;
\r
39 cb_duration.SelectedIndex = 1;
\r
41 cb_preview.Items.Clear();
\r
42 for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)
\r
43 cb_preview.Items.Add(i.ToString());
\r
44 cb_preview.SelectedIndex = 0;
\r
46 Process.EncodeStarted += new EventHandler(Process_EncodeStarted);
\r
49 private void Process_EncodeStarted(object sender, EventArgs e)
\r
51 Thread encodeMon = new Thread(EncodeMonitorThread);
\r
55 #region Encode Sample
\r
57 private void btn_playVLC_Click(object sender, EventArgs e)
\r
59 lbl_status.Visible = true;
\r
60 ProgressBarStatus.Visible = true;
\r
61 ProgressBarStatus.Value = 0;
\r
62 lbl_encodeStatus.Visible = true;
\r
67 QTControl.URL = string.Empty;
\r
69 if (File.Exists(CurrentlyPlaying))
\r
70 File.Delete(CurrentlyPlaying);
\r
74 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",
\r
75 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
78 btn_playQT.Enabled = false;
\r
79 btn_playVLC.Enabled = false;
\r
80 lbl_status.Text = "Encoding Sample for (VLC) ...";
\r
82 int.TryParse(cb_duration.Text, out duration);
\r
83 string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);
\r
84 ThreadPool.QueueUserWorkItem(ProcMonitor, query);
\r
87 private void btn_playQT_Click(object sender, EventArgs e)
\r
91 MessageBox.Show(this,
\r
92 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",
\r
93 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
96 if (MainWindow.text_destination.Text.Contains(".mkv"))
\r
98 MessageBox.Show(this,
\r
99 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",
\r
100 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
104 lbl_status.Visible = true;
\r
105 ProgressBarStatus.Visible = true;
\r
106 ProgressBarStatus.Value = 0;
\r
107 lbl_encodeStatus.Visible = true;
\r
110 QTControl.URL = string.Empty;
\r
111 if (File.Exists(CurrentlyPlaying))
\r
112 File.Delete(CurrentlyPlaying);
\r
116 MessageBox.Show(this,
\r
117 "Unable to delete previous preview file. You may need to restart the application.",
\r
118 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
121 btn_playQT.Enabled = false;
\r
122 btn_playVLC.Enabled = false;
\r
123 lbl_status.Text = "Encoding Sample for (QT) ...";
\r
125 int.TryParse(cb_duration.Text, out duration);
\r
126 string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);
\r
128 ThreadPool.QueueUserWorkItem(ProcMonitor, query);
\r
132 private void ProcMonitor(object state)
\r
134 // Make sure we are not already encoding and if we are then display an error.
\r
135 if (Process.HbProcess != null)
\r
136 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK,
\r
137 MessageBoxIcon.Warning);
\r
140 Process.CreatePreviewSample((string) state);
\r
142 if (Process.HbProcess != null)
\r
144 Process.HbProcess.WaitForExit();
\r
145 Process.HbProcess = null;
\r
151 private void EncodeMonitorThread()
\r
155 Parser encode = new Parser(Process.HbProcess.StandardOutput.BaseStream);
\r
156 encode.OnEncodeProgress += EncodeOnEncodeProgress;
\r
157 while (!encode.EndOfStream)
\r
158 encode.ReadEncodeStatus();
\r
160 catch (Exception exc)
\r
162 MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
166 private void EncodeOnEncodeProgress(object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, TimeSpan TimeRemaining)
\r
168 if (this.InvokeRequired)
\r
171 new EncodeProgressEventHandler(EncodeOnEncodeProgress),
\r
172 new[] { Sender, CurrentTask, TaskCount, PercentComplete, CurrentFps, AverageFps, TimeRemaining });
\r
175 lbl_encodeStatus.Text = PercentComplete + "%";
\r
176 ProgressBarStatus.Value = (int)Math.Round(PercentComplete);
\r
180 private void EncodeCompleted()
\r
184 if (InvokeRequired)
\r
186 BeginInvoke(new UpdateUIHandler(EncodeCompleted));
\r
190 ProgressBarStatus.Visible = false;
\r
191 lbl_encodeStatus.Visible = false;
\r
194 btn_playQT.Enabled = true;
\r
195 btn_playVLC.Enabled = true;
\r
197 // Decide which Player to use.
\r
198 string playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";
\r
200 lbl_status.Text = "Loading Clip ...";
\r
202 // Get the sample filename
\r
203 if (MainWindow.text_destination.Text != string.Empty)
\r
205 MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").
\r
206 Replace(".mkv", "_sample.mkv");
\r
209 // Play back in QT or VLC
\r
210 if (playerSelection == "QT")
\r
215 lbl_status.Text = string.Empty;
\r
217 catch (Exception exc)
\r
219 MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK,
\r
220 MessageBoxIcon.Error);
\r
229 /// Play the video back in the QuickTime control
\r
231 private void Play()
\r
233 Player = new Thread(OpenMovie) {IsBackground = true};
\r
235 lbl_status.Visible = false;
\r
239 /// Play the video back in an external VLC Player
\r
241 private void PlayVLC()
\r
243 // Launch VLC and Play video.
\r
244 if (CurrentlyPlaying != string.Empty)
\r
246 if (File.Exists(CurrentlyPlaying))
\r
248 // Attempt to find VLC if it doesn't exist in the default set location.
\r
251 if (8 == IntPtr.Size ||
\r
252 (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
\r
253 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
\r
255 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");
\r
257 vlcPath = vlcPath != null
\r
258 ? vlcPath + @"\VideoLAN\VLC\vlc.exe"
\r
259 : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
\r
261 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
263 if (File.Exists(vlcPath))
\r
265 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
266 Properties.Settings.Default.Save(); // Save this new path if it does
\r
270 MessageBox.Show(this,
\r
271 "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
272 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
276 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
278 string args = "\"" + CurrentlyPlaying + "\"";
\r
279 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
280 System.Diagnostics.Process.Start(vlc);
\r
281 lbl_status.Text = "VLC will now launch.";
\r
285 MessageBox.Show(this,
\r
286 "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",
\r
287 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
289 lbl_status.Visible = false;
\r
293 /// QT control - Open the file
\r
296 private void OpenMovie()
\r
300 if (InvokeRequired)
\r
302 BeginInvoke(new UpdateUIHandler(OpenMovie));
\r
305 QTControl.URL = CurrentlyPlaying;
\r
306 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
307 QTControl.URL = CurrentlyPlaying;
\r
310 this.ClientSize = QTControl.Size;
\r
313 catch (COMException ex)
\r
315 QTUtils qtu = new QTUtils();
\r
316 MessageBox.Show(this,
\r
317 "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") +
\r
318 "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT",
\r
319 MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
321 catch (Exception ex)
\r
323 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK,
\r
324 MessageBoxIcon.Warning);
\r
330 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
\r
332 Process.EncodeStarted -= Process_EncodeStarted;
\r