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.Parsing;
\r
17 using HandBrake.ApplicationServices.Services;
\r
19 using QTOControlLib;
\r
22 public partial class frmPreview : Form
\r
24 private string CurrentlyPlaying = string.Empty;
\r
25 private readonly frmMain MainWindow;
\r
26 private Thread Player;
\r
27 private readonly bool NoQT;
\r
28 private readonly Queue Process = new Queue();
\r
29 private delegate void UpdateUIHandler();
\r
30 private bool playWithVLC;
\r
32 public frmPreview(frmMain mw)
\r
36 InitializeComponent();
\r
42 this.MainWindow = mw;
\r
43 cb_preview.SelectedIndex = 0;
\r
44 cb_duration.SelectedIndex = 1;
\r
46 cb_preview.Items.Clear();
\r
47 for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)
\r
48 cb_preview.Items.Add(i.ToString());
\r
49 cb_preview.SelectedIndex = 0;
\r
51 Process.EncodeStarted += new EventHandler(Process_EncodeStarted);
\r
53 private void Process_EncodeStarted(object sender, EventArgs e)
\r
55 Thread encodeMon = new Thread(EncodeMonitorThread);
\r
59 #region Encode Sample
\r
61 private void btn_playVLC_Click(object sender, EventArgs e)
\r
63 ProgressBarStatus.Visible = true;
\r
64 ProgressBarStatus.Value = 0;
\r
65 lbl_encodeStatus.Visible = true;
\r
71 QTControl.URL = string.Empty;
\r
73 if (File.Exists(CurrentlyPlaying))
\r
74 File.Delete(CurrentlyPlaying);
\r
78 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",
\r
79 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
82 btn_playQT.Enabled = false;
\r
83 btn_playVLC.Enabled = false;
\r
84 this.Text += " (Encoding)";
\r
86 int.TryParse(cb_duration.Text, out duration);
\r
87 string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);
\r
88 ThreadPool.QueueUserWorkItem(ProcMonitor, query);
\r
91 private void btn_playQT_Click(object sender, EventArgs e)
\r
93 playWithVLC = false;
\r
96 MessageBox.Show(this,
\r
97 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",
\r
98 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
101 if (MainWindow.text_destination.Text.Contains(".mkv"))
\r
103 MessageBox.Show(this,
\r
104 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",
\r
105 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
109 ProgressBarStatus.Visible = true;
\r
110 ProgressBarStatus.Value = 0;
\r
111 lbl_encodeStatus.Visible = true;
\r
114 QTControl.URL = string.Empty;
\r
115 if (File.Exists(CurrentlyPlaying))
\r
116 File.Delete(CurrentlyPlaying);
\r
120 MessageBox.Show(this,
\r
121 "Unable to delete previous preview file. You may need to restart the application.",
\r
122 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
125 btn_playQT.Enabled = false;
\r
126 btn_playVLC.Enabled = false;
\r
127 this.Text += " (Encoding)";
\r
129 int.TryParse(cb_duration.Text, out duration);
\r
130 string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);
\r
132 ThreadPool.QueueUserWorkItem(ProcMonitor, query);
\r
136 private void ProcMonitor(object state)
\r
138 // Make sure we are not already encoding and if we are then display an error.
\r
139 if (Process.HbProcess != null)
\r
140 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK,
\r
141 MessageBoxIcon.Warning);
\r
144 Process.CreatePreviewSample((string) state);
\r
146 if (Process.HbProcess != null)
\r
148 Process.HbProcess.WaitForExit();
\r
149 Process.HbProcess = null;
\r
155 private void EncodeMonitorThread()
\r
159 Parser encode = new Parser(Process.HbProcess.StandardOutput.BaseStream);
\r
160 encode.OnEncodeProgress += EncodeOnEncodeProgress;
\r
161 while (!encode.EndOfStream)
\r
162 encode.ReadEncodeStatus();
\r
164 catch (Exception exc)
\r
166 MessageBox.Show(exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
170 private void EncodeOnEncodeProgress(object Sender, int CurrentTask, int TaskCount, float PercentComplete, float CurrentFps, float AverageFps, TimeSpan TimeRemaining)
\r
172 if (this.InvokeRequired)
\r
175 new EncodeProgressEventHandler(EncodeOnEncodeProgress),
\r
176 new[] { Sender, CurrentTask, TaskCount, PercentComplete, CurrentFps, AverageFps, TimeRemaining });
\r
179 lbl_encodeStatus.Text = PercentComplete + "%";
\r
180 ProgressBarStatus.Value = (int)Math.Round(PercentComplete);
\r
183 private void EncodeCompleted()
\r
187 if (InvokeRequired)
\r
189 BeginInvoke(new UpdateUIHandler(EncodeCompleted));
\r
193 ProgressBarStatus.Visible = false;
\r
194 lbl_encodeStatus.Visible = false;
\r
197 btn_playQT.Enabled = true;
\r
198 btn_playVLC.Enabled = true;
\r
200 this.Text = this.Text.Replace(" (Encoding)", string.Empty);
\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
208 // Play back in QT or VLC
\r
214 catch (Exception exc)
\r
216 MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK,
\r
217 MessageBoxIcon.Error);
\r
226 /// Play the video back in the QuickTime control
\r
228 private void Play()
\r
230 Player = new Thread(OpenMovie) {IsBackground = true};
\r
235 /// Play the video back in an external VLC Player
\r
237 private void PlayVLC()
\r
239 // Launch VLC and Play video.
\r
240 if (CurrentlyPlaying != string.Empty)
\r
242 if (File.Exists(CurrentlyPlaying))
\r
244 // Attempt to find VLC if it doesn't exist in the default set location.
\r
247 if (8 == IntPtr.Size ||
\r
248 (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
\r
249 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
\r
251 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");
\r
253 vlcPath = vlcPath != null
\r
254 ? vlcPath + @"\VideoLAN\VLC\vlc.exe"
\r
255 : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
\r
257 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
259 if (File.Exists(vlcPath))
\r
261 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
262 Properties.Settings.Default.Save(); // Save this new path if it does
\r
266 MessageBox.Show(this,
\r
267 "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
268 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
272 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
274 string args = "\"" + CurrentlyPlaying + "\"";
\r
275 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
276 System.Diagnostics.Process.Start(vlc);
\r
280 MessageBox.Show(this,
\r
281 "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",
\r
282 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
287 /// QT control - Open the file
\r
290 private void OpenMovie()
\r
294 if (InvokeRequired)
\r
296 BeginInvoke(new UpdateUIHandler(OpenMovie));
\r
299 QTControl.URL = CurrentlyPlaying;
\r
300 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
301 QTControl.URL = CurrentlyPlaying;
\r
304 this.ClientSize = QTControl.Size;
\r
307 catch (COMException ex)
\r
309 QTUtils qtu = new QTUtils();
\r
310 MessageBox.Show(this,
\r
311 "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") +
\r
312 "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT",
\r
313 MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
315 catch (Exception ex)
\r
317 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK,
\r
318 MessageBoxIcon.Warning);
\r
324 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
\r
326 Process.EncodeStarted -= Process_EncodeStarted;
\r