2 using System.Windows.Forms;
\r
3 using System.Threading;
\r
4 using System.Diagnostics;
\r
5 using System.Runtime.InteropServices;
\r
7 using Handbrake.EncodeQueue;
\r
8 using Handbrake.Functions;
\r
14 public partial class frmPreview : Form
\r
16 readonly Queue Process = new Queue();
\r
17 private delegate void UpdateUIHandler();
\r
18 String CurrentlyPlaying = "";
\r
19 readonly frmMain MainWindow;
\r
20 private Thread Player;
\r
21 private readonly Boolean NoQT;
\r
23 public frmPreview(frmMain mw)
\r
27 InitializeComponent();
\r
33 this.MainWindow = mw;
\r
34 cb_preview.SelectedIndex = 0;
\r
35 cb_duration.SelectedIndex = 1;
\r
37 cb_preview.Items.Clear();
\r
38 for (int i = 1; i <= Properties.Settings.Default.previewScanCount; i++)
\r
39 cb_preview.Items.Add(i.ToString());
\r
40 cb_preview.SelectedIndex = 0;
\r
43 #region Encode Sample
\r
44 private void btn_playVLC_Click(object sender, EventArgs e)
\r
46 lbl_status.Visible = true;
\r
52 if (File.Exists(CurrentlyPlaying))
\r
53 File.Delete(CurrentlyPlaying);
\r
57 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
60 btn_playQT.Enabled = false;
\r
61 btn_playVLC.Enabled = false;
\r
62 lbl_status.Text = "Encoding Sample for (VLC) ...";
\r
64 int.TryParse(cb_duration.Text, out duration);
\r
65 String query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);
\r
66 ThreadPool.QueueUserWorkItem(ProcMonitor, query);
\r
68 private void btn_playQT_Click(object sender, EventArgs e)
\r
72 MessageBox.Show(this, "It would appear QuickTime 7 is not installed or not accessible. Please (re)install QuickTime.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
75 if (MainWindow.text_destination.Text.Contains(".mkv"))
\r
77 MessageBox.Show(this,
\r
78 "The QuickTime Control does not support MKV files, It is recommended you use VLC option instead.",
\r
79 "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
83 lbl_status.Visible = true;
\r
87 if (File.Exists(CurrentlyPlaying))
\r
88 File.Delete(CurrentlyPlaying);
\r
92 MessageBox.Show(this, "Unable to delete previous preview file. You may need to restart the application.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
95 btn_playQT.Enabled = false;
\r
96 btn_playVLC.Enabled = false;
\r
97 lbl_status.Text = "Encoding Sample for (QT) ...";
\r
99 int.TryParse(cb_duration.Text, out duration);
\r
100 String query = QueryGenerator.GenerateCliQuery(MainWindow, 3, duration, cb_preview.Text);
\r
102 ThreadPool.QueueUserWorkItem(ProcMonitor, query);
\r
105 private void ProcMonitor(object state)
\r
107 // Make sure we are not already encoding and if we are then display an error.
\r
108 if (Process.HbProcess != null)
\r
109 MessageBox.Show(this, "Handbrake is already encoding a video!", "Status", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
112 Process.CreatePreviewSample((string)state);
\r
113 if (Process.HbProcess != null)
\r
115 Process.HbProcess.WaitForExit();
\r
116 Process.HbProcess = null;
\r
121 private void EncodeCompleted()
\r
125 if (InvokeRequired)
\r
127 BeginInvoke(new UpdateUIHandler(EncodeCompleted));
\r
131 btn_playQT.Enabled = true;
\r
132 btn_playVLC.Enabled = true;
\r
134 // Decide which Player to use.
\r
135 String playerSelection = lbl_status.Text.Contains("QT") ? "QT" : "VLC";
\r
137 lbl_status.Text = "Loading Clip ...";
\r
139 // Get the sample filename
\r
140 if (MainWindow.text_destination.Text != "")
\r
141 CurrentlyPlaying = MainWindow.text_destination.Text.Replace(".mp4", "_sample.mp4").Replace(".m4v", "_sample.m4v").Replace(".mkv", "_sample.mkv"); ;
\r
143 // Play back in QT or VLC
\r
144 if (playerSelection == "QT")
\r
149 lbl_status.Text = "";
\r
151 catch (Exception exc)
\r
153 MessageBox.Show(this, "frmPreview.cs EncodeCompleted " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
161 /// Play the video back in the QuickTime control
\r
163 private void Play()
\r
165 Player = new Thread(OpenMovie) { IsBackground = true };
\r
167 lbl_status.Visible = false;
\r
171 /// Play the video back in an external VLC Player
\r
173 private void PlayVLC()
\r
175 // Launch VLC and Play video.
\r
176 if (CurrentlyPlaying != "")
\r
178 if (File.Exists(CurrentlyPlaying))
\r
180 // Attempt to find VLC if it doesn't exist in the default set location.
\r
183 if (8 == IntPtr.Size || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
\r
184 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
\r
186 vlcPath = Environment.GetEnvironmentVariable("ProgramFiles");
\r
188 vlcPath = vlcPath != null ? vlcPath + @"\VideoLAN\VLC\vlc.exe" : @"C:\Program Files (x86)\VideoLAN\VLC\vlc.exe";
\r
190 if (!File.Exists(Properties.Settings.Default.VLC_Path))
\r
192 if (File.Exists(vlcPath))
\r
194 Properties.Settings.Default.VLC_Path = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
\r
195 Properties.Settings.Default.Save(); // Save this new path if it does
\r
199 MessageBox.Show(this,
\r
200 "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
201 "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
205 if (File.Exists(Properties.Settings.Default.VLC_Path))
\r
207 String args = "\"" + CurrentlyPlaying + "\"";
\r
208 ProcessStartInfo vlc = new ProcessStartInfo(Properties.Settings.Default.VLC_Path, args);
\r
209 System.Diagnostics.Process.Start(vlc);
\r
210 lbl_status.Text = "VLC will now launch.";
\r
215 MessageBox.Show(this, "Unable to find the preview file. Either the file was deleted or the encode failed. Check the activity log for details.", "VLC", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
217 lbl_status.Visible = false;
\r
221 /// QT control - Open the file
\r
224 private void OpenMovie()
\r
228 if (InvokeRequired)
\r
230 BeginInvoke(new UpdateUIHandler(OpenMovie));
\r
233 QTControl.URL = CurrentlyPlaying;
\r
234 QTControl.SetSizing(QTSizingModeEnum.qtControlFitsMovie, true);
\r
235 QTControl.URL = CurrentlyPlaying;
\r
238 this.ClientSize = QTControl.Size;
\r
241 catch (COMException ex)
\r
243 QTUtils qtu = new QTUtils();
\r
244 MessageBox.Show(this, "Unable to open movie:\n\nError Code: " + ex.ErrorCode.ToString("X") + "\nQT Error code : " + qtu.QTErrorFromErrorCode(ex.ErrorCode), "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
246 catch (Exception ex)
\r
248 MessageBox.Show(this, "Unable to open movie:\n\n" + ex, "QT", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r