4 using System.Diagnostics;
\r
6 using System.Runtime.InteropServices;
\r
7 using System.Threading;
\r
8 using System.Windows.Forms;
\r
10 using QTOControlLib;
\r
14 public partial class frmPreview : Form
\r
16 private readonly Queue Process = new Queue();
\r
18 private delegate void UpdateUIHandler();
\r
20 private string CurrentlyPlaying = string.Empty;
\r
21 private readonly frmMain MainWindow;
\r
22 private Thread Player;
\r
23 private readonly bool NoQT;
\r
25 public frmPreview(frmMain mw)
\r
29 InitializeComponent();
\r
35 this.MainWindow = mw;
\r
36 cb_preview.SelectedIndex = 0;
\r
37 cb_duration.SelectedIndex = 1;
\r
39 cb_preview.Items.Clear();
\r
40 for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)
\r
41 cb_preview.Items.Add(i.ToString());
\r
42 cb_preview.SelectedIndex = 0;
\r
45 #region Encode Sample
\r
47 private void btn_playVLC_Click(object sender, EventArgs e)
\r
49 lbl_status.Visible = true;
\r
53 QTControl.URL = string.Empty;
\r
55 if (File.Exists(CurrentlyPlaying))
\r
56 File.Delete(CurrentlyPlaying);
\r
60 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.",
\r
61 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
64 btn_playQT.Enabled = false;
\r
65 btn_playVLC.Enabled = false;
\r
66 lbl_status.Text = "Encoding Sample for (VLC) ...";
\r
68 int.TryParse(cb_duration.Text, out duration);
\r
69 string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);
\r
70 ThreadPool.QueueUserWorkItem(ProcMonitor, query);
\r
73 private void btn_playQT_Click(object sender, EventArgs e)
\r
77 MessageBox.Show(this,
\r
78 "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.",
\r
79 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
82 if (MainWindow.text_destination.Text.Contains(".mkv"))
\r
84 MessageBox.Show(this,
\r
85 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",
\r
86 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
90 lbl_status.Visible = true;
\r
93 QTControl.URL = string.Empty;
\r
94 if (File.Exists(CurrentlyPlaying))
\r
95 File.Delete(CurrentlyPlaying);
\r
99 MessageBox.Show(this,
\r
100 "Unable to delete previous preview file. You may need to restart the application.",
\r
101 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
104 btn_playQT.Enabled = false;
\r
105 btn_playVLC.Enabled = false;
\r
106 lbl_status.Text = "Encoding Sample for (QT) ...";
\r
108 int.TryParse(cb_duration.Text, out duration);
\r
109 string query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);
\r
111 ThreadPool.QueueUserWorkItem(ProcMonitor, query);
\r
115 private void ProcMonitor(object state)
\r
117 // Make sure we are not already encoding and if we are then display an error.
\r
118 if (Process.HbProcess != null)
\r
119 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK,
\r
120 MessageBoxIcon.Warning);
\r
123 Process.CreatePreviewSample((string) state);
\r
124 if (Process.HbProcess != null)
\r
126 Process.HbProcess.WaitForExit();
\r
127 Process.HbProcess = null;
\r
133 private void EncodeCompleted()
\r
137 if (InvokeRequired)
\r
139 BeginInvoke(new UpdateUIHandler(EncodeCompleted));
\r
143 btn_playQT.Enabled = true;
\r
144 btn_playVLC.Enabled = true;
\r
146 // Decide which Player to use.
\r
147 string playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";
\r
149 lbl_status.Text = "Loading Clip ...";
\r
151 // Get the sample filename
\r
152 if (MainWindow.text_destination.Text != string.Empty)
\r
154 MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").
\r
155 Replace(".mkv", "_sample.mkv");
\r
158 // Play back in QT or VLC
\r
159 if (playerSelection == "QT")
\r
164 lbl_status.Text = string.Empty;
\r
166 catch (Exception exc)
\r
168 MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK,
\r
169 MessageBoxIcon.Error);
\r
178 /// Play the video back in the QuickTime control
\r
180 private void Play()
\r
182 Player = new Thread(OpenMovie) {IsBackground = true};
\r
184 lbl_status.Visible = false;
\r
188 /// Play the video back in an external VLC Player
\r
190 private void PlayVLC()
\r
192 // Launch VLC and Play video.
\r
193 if (CurrentlyPlaying != string.Empty)
\r
195 if (File.Exists(CurrentlyPlaying))
\r
197 // Attempt to find VLC if it doesn't exist in the default set location.
\r
200 if (8 == IntPtr.Size ||
\r
201 (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
\r
202 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
\r
204 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");
\r
206 vlcPath = vlcPath != null
\r
207 ? vlcPath + @"\VideoLAN\VLC\vlc.exe"
\r
208 : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
\r
210 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
212 if (File.Exists(vlcPath))
\r
214 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
215 Properties.Settings.Default.Save(); // Save this new path if it does
\r
219 MessageBox.Show(this,
\r
220 "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
221 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
225 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
227 string args = "\"" + CurrentlyPlaying + "\"";
\r
228 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
229 System.Diagnostics.Process.Start(vlc);
\r
230 lbl_status.Text = "VLC will now launch.";
\r
234 MessageBox.Show(this,
\r
235 "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.",
\r
236 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
238 lbl_status.Visible = false;
\r
242 /// QT control - Open the file
\r
245 private void OpenMovie()
\r
249 if (InvokeRequired)
\r
251 BeginInvoke(new UpdateUIHandler(OpenMovie));
\r
254 QTControl.URL = CurrentlyPlaying;
\r
255 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
256 QTControl.URL = CurrentlyPlaying;
\r
259 this.ClientSize = QTControl.Size;
\r
262 catch (COMException ex)
\r
264 QTUtils qtu = new QTUtils();
\r
265 MessageBox.Show(this,
\r
266 "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") +
\r
267 "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT",
\r
268 MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
270 catch (Exception ex)
\r
272 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK,
\r
273 MessageBoxIcon.Warning);
\r