3 This file is part of the HandBrake source code.
\r
4 Homepage: <http://handbrake.fr>.
\r
5 It may be used under the terms of the GNU General Public License. */
\r
8 using System.Collections;
\r
10 using System.Windows.Forms;
\r
11 using System.Globalization;
\r
13 using System.Drawing;
\r
14 using System.Diagnostics;
\r
15 using System.Text.RegularExpressions;
\r
17 namespace Handbrake.Functions
\r
22 /// Take in a File destination and change it's file extension to a new Extension
\r
24 /// <param name="destination"></param>
\r
25 /// <param name="newExtension"></param>
\r
26 /// <returns>String of the new file path and extension</returns>
\r
27 public string setExtension(string destination, string newExtension)
\r
29 destination.Replace(".mp4", newExtension);
\r
30 destination.Replace(".m4v", newExtension);
\r
31 destination.Replace(".mkv", newExtension);
\r
32 destination.Replace(".avi", newExtension);
\r
33 destination.Replace(".ogm", newExtension);
\r
39 /// Calculate the duration of the selected title and chapters
\r
41 public TimeSpan calculateDuration(string chapter_start, string chapter_end, Parsing.Title selectedTitle)
\r
43 TimeSpan Duration = TimeSpan.FromSeconds(0.0);
\r
45 // Get the durations between the 2 chapter points and add them together.
\r
46 if (chapter_start != "Auto" && chapter_end != "Auto")
\r
48 int start_chapter, end_chapter = 0;
\r
49 int.TryParse(chapter_start, out start_chapter);
\r
50 int.TryParse(chapter_end, out end_chapter);
\r
52 int position = start_chapter - 1;
\r
54 if (start_chapter <= end_chapter)
\r
56 if (end_chapter > selectedTitle.Chapters.Count)
\r
57 end_chapter = selectedTitle.Chapters.Count;
\r
59 while (position != end_chapter)
\r
61 TimeSpan dur = selectedTitle.Chapters[position].Duration;
\r
62 Duration = Duration + dur;
\r
71 /// Calculate the non-anamorphic resoltuion of the source
\r
73 /// <param name="width"></param>
\r
74 /// <returns></returns>
\r
75 public int cacluateNonAnamorphicHeight(int width, decimal top, decimal bottom, decimal left, decimal right, Parsing.Title selectedTitle)
\r
77 float aspect = selectedTitle.AspectRatio;
\r
80 if (aspect.ToString() == "1.78")
\r
91 double a = width * selectedTitle.Resolution.Width * ah * (selectedTitle.Resolution.Height - (double)top - (double)bottom);
\r
92 double b = selectedTitle.Resolution.Height * aw * (selectedTitle.Resolution.Width - (double)left - (double)right);
\r
96 // If it's not Mod 16, make it mod 16
\r
99 double mod16 = y % 16;
\r
102 mod16 = 16 - mod16;
\r
112 //double z = ( 16 * (( y + 8 ) / 16 ) );
\r
113 int x = int.Parse(y.ToString());
\r
118 /// Select the longest title in the DVD title dropdown menu on frmMain
\r
120 public Handbrake.Parsing.Title selectLongestTitle(ComboBox drp_dvdtitle)
\r
122 int current_largest = 0;
\r
123 Handbrake.Parsing.Title title2Select;
\r
125 // Check if there are titles in the DVD title dropdown menu and make sure, it's not just "Automatic"
\r
126 if (drp_dvdtitle.Items[0].ToString() != "Automatic")
\r
127 title2Select = (Handbrake.Parsing.Title)drp_dvdtitle.Items[0];
\r
129 title2Select = null;
\r
131 // So, If there are titles in the DVD Title dropdown menu, lets select the longest.
\r
132 if (title2Select != null)
\r
134 foreach (Handbrake.Parsing.Title x in drp_dvdtitle.Items)
\r
136 string title = x.ToString();
\r
137 if (title != "Automatic")
\r
139 string[] y = title.Split(' ');
\r
140 string time = y[1].Replace("(", "").Replace(")", "");
\r
141 string[] z = time.Split(':');
\r
143 int hours = int.Parse(z[0]) * 60 * 60;
\r
144 int minutes = int.Parse(z[1]) * 60;
\r
145 int seconds = int.Parse(z[2]);
\r
146 int total_sec = hours + minutes + seconds;
\r
148 if (current_largest == 0)
\r
150 current_largest = hours + minutes + seconds;
\r
155 if (total_sec > current_largest)
\r
157 current_largest = total_sec;
\r
164 return title2Select;
\r
168 /// Set's up the DataGridView on the Chapters tab (frmMain)
\r
170 /// <param name="mainWindow"></param>
\r
171 public DataGridView chapterNaming(DataGridView data_chpt, string chapter_start, string chapter_end)
\r
179 if (chapter_end != "Auto")
\r
180 finish = int.Parse(chapter_end);
\r
182 if (chapter_start != "Auto")
\r
183 start = int.Parse(chapter_start);
\r
185 rowCount = finish - (start - 1);
\r
187 while (i < rowCount)
\r
189 DataGridViewRow row = new DataGridViewRow();
\r
191 data_chpt.Rows.Insert(i, row);
\r
192 data_chpt.Rows[i].Cells[0].Value = (i + 1);
\r
193 data_chpt.Rows[i].Cells[1].Value = "Chapter " + (i + 1);
\r
198 catch (Exception exc)
\r
200 MessageBox.Show("chapterNaming() Error has occured: \n" + exc.ToString());
\r
206 /// Function which generates the filename and path automatically based on
\r
207 /// the Source Name, DVD title and DVD Chapters
\r
209 /// <param name="mainWindow"></param>
\r
210 public string autoName(ComboBox drp_dvdtitle, string chapter_start, string chatper_end, string source, string dest, int format)
\r
213 string AutoNamePath = string.Empty;
\r
215 if (drp_dvdtitle.Text != "Automatic")
\r
217 // Todo: This code is a tad messy. Clean it up at some point.
\r
218 // Get the Source Name
\r
219 string[] sourceName = source.Split('\\');
\r
220 source = sourceName[sourceName.Length - 1].Replace(".iso", "").Replace(".mpg", "").Replace(".ts", "").Replace(".ps", "");
\r
222 // Get the Selected Title Number
\r
223 string title = drp_dvdtitle.Text;
\r
224 string[] titlesplit = title.Split(' ');
\r
225 title = titlesplit[0];
\r
227 // Get the Chapter Start and Chapter End Numbers
\r
228 string cs = chapter_start;
\r
229 string cf = chatper_end;
\r
231 // Just incase the above are set to their default Automatic values, set the varible to ""
\r
232 if (title == "Automatic")
\r
239 // If both CS and CF are populated, set the dash varible
\r
244 // Get the destination filename.
\r
245 string destination_filename = "";
\r
246 if (Properties.Settings.Default.autoNameFormat != "")
\r
248 destination_filename = Properties.Settings.Default.autoNameFormat;
\r
249 destination_filename = destination_filename.Replace("{source}", source).Replace("{title}", title).Replace("{chapters}", cs + dash + cf);
\r
252 destination_filename = source + "_T" + title + "_C" + cs + dash + cf;
\r
254 // If the text box is blank
\r
255 if (!dest.Contains("\\"))
\r
257 string filePath = "";
\r
258 if (Properties.Settings.Default.autoNamePath.Trim() != "")
\r
260 if (Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location")
\r
261 filePath = Properties.Settings.Default.autoNamePath + "\\";
\r
265 AutoNamePath = filePath + destination_filename + ".mp4";
\r
266 else if (format == 1)
\r
267 AutoNamePath = filePath + destination_filename + ".m4v";
\r
268 else if (format == 2)
\r
269 AutoNamePath = filePath + destination_filename + ".mkv";
\r
270 else if (format == 3)
\r
271 AutoNamePath = filePath + destination_filename + ".avi";
\r
272 else if (format == 4)
\r
273 AutoNamePath = filePath + destination_filename + ".ogm";
\r
275 else // If the text box already has a path and file
\r
277 string destination = AutoNamePath;
\r
278 string[] destName = dest.Split('\\');
\r
279 string[] extension = dest.Split('.');
\r
280 string ext = extension[extension.Length - 1];
\r
282 destName[destName.Length - 1] = destination_filename + "." + ext;
\r
284 string fullDest = "";
\r
285 foreach (string part in destName)
\r
287 if (fullDest != "")
\r
288 fullDest = fullDest + "\\" + part;
\r
290 fullDest = fullDest + part;
\r
295 return AutoNamePath;
\r
299 /// Checks for updates and returns true if an update is available.
\r
301 /// <param name="debug">Turns on debug mode. Don't use on program startup</param>
\r
302 /// <returns>Boolean True = Update available</returns>
\r
303 public Boolean updateCheck(Boolean debug)
\r
307 Functions.AppcastReader rssRead = new Functions.AppcastReader();
\r
308 rssRead.getInfo(); // Initializes the class.
\r
309 string build = rssRead.build();
\r
311 int latest = int.Parse(build);
\r
312 int current = Properties.Settings.Default.hb_build;
\r
313 int skip = Properties.Settings.Default.skipversion;
\r
315 if (latest == skip)
\r
319 Boolean update = (latest > current);
\r
323 catch (Exception exc)
\r
326 MessageBox.Show("Unable to check for updates, Please try again later. \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
332 /// Get's HandBrakes version data from the CLI.
\r
334 /// <returns>Arraylist of Version Data. 0 = hb_version 1 = hb_build</returns>
\r
335 public ArrayList getCliVersionData()
\r
337 ArrayList cliVersionData = new ArrayList();
\r
338 // 0 = SVN Build / Version
\r
341 Process cliProcess = new Process();
\r
342 ProcessStartInfo handBrakeCLI = new ProcessStartInfo("HandBrakeCLI.exe", " -u");
\r
343 handBrakeCLI.UseShellExecute = false;
\r
344 handBrakeCLI.RedirectStandardError = true;
\r
345 handBrakeCLI.RedirectStandardOutput = true;
\r
346 handBrakeCLI.CreateNoWindow = true;
\r
347 cliProcess.StartInfo = handBrakeCLI;
\r
348 cliProcess.Start();
\r
350 // Retrieve standard output and report back to parent thread until the process is complete
\r
352 TextReader stdOutput = cliProcess.StandardError;
\r
354 while (!cliProcess.HasExited)
\r
356 line = stdOutput.ReadLine();
\r
357 Match m = Regex.Match(line, @"HandBrake [0-9\.]*svn[0-9]*[M]* \([0-9]*\)");
\r
358 if (m.Success != false)
\r
360 string data = line.Replace("(", "").Replace(")", "").Replace("HandBrake ", "");
\r
361 string[] arr = data.Split(' ');
\r
362 cliVersionData.Add(arr[0]);
\r
363 cliVersionData.Add(arr[1]);
\r
364 return cliVersionData;
\r
371 /// Check if the queue recovery file contains records.
\r
372 /// If it does, it means the last queue did not complete before HandBrake closed.
\r
373 /// So, return a boolean if true.
\r
375 public Boolean check_queue_recovery()
\r
379 string tempPath = Path.Combine(Path.GetTempPath(), "hb_queue_recovery.dat");
\r
380 using (StreamReader reader = new StreamReader(tempPath))
\r
382 string queue_item = reader.ReadLine();
\r
383 if (queue_item == null)
\r
389 else // There exists an item in the recovery queue file, so try and recovr it.
\r
399 // Keep quiet about the error.
\r