OSDN Git Service

remove crf flag and all uses of it
[handbrake-jp/handbrake-jp-git.git] / win / C# / Functions / Main.cs
index 075bdfa..6f8efaa 100644 (file)
@@ -25,167 +25,162 @@ namespace Handbrake.Functions
         /// <summary>\r
         /// Calculate the duration of the selected title and chapters\r
         /// </summary>\r
-        public static TimeSpan calculateDuration(string chapter_start, string chapter_end, Parsing.Title selectedTitle)\r
+        public static TimeSpan calculateDuration(int chapterStart, int chapterEnd, Parsing.Title selectedTitle)\r
         {\r
-            TimeSpan Duration = TimeSpan.FromSeconds(0.0);\r
-\r
-            // Get the durations between the 2 chapter points and add them together.\r
-            if (chapter_start != "Auto" && chapter_end != "Auto")\r
+            TimeSpan duration = TimeSpan.FromSeconds(0.0);\r
+            chapterStart++; chapterEnd++;\r
+            if (chapterStart != 0 && chapterEnd != 0 && chapterEnd <= selectedTitle.Chapters.Count)\r
             {\r
-                int start_chapter, end_chapter;\r
-                int.TryParse(chapter_start, out start_chapter);\r
-                int.TryParse(chapter_end, out end_chapter);\r
-\r
-                int position = start_chapter - 1;\r
-\r
-                if (start_chapter <= end_chapter)\r
-                {\r
-                    if (end_chapter > selectedTitle.Chapters.Count)\r
-                        end_chapter = selectedTitle.Chapters.Count;\r
-\r
-                    while (position != end_chapter)\r
-                    {\r
-                        TimeSpan dur = selectedTitle.Chapters[position].Duration;\r
-                        Duration = Duration + dur;\r
-                        position++;\r
-                    }\r
-                }\r
+                for (int i = chapterStart; i <= chapterEnd; i++)\r
+                    duration += selectedTitle.Chapters[i - 1].Duration;\r
             }\r
-            return Duration;\r
+\r
+            return duration;\r
         }\r
 \r
         /// <summary>\r
         /// Select the longest title in the DVD title dropdown menu on frmMain\r
         /// </summary>\r
-        public static Parsing.Title selectLongestTitle(ComboBox drp_dvdtitle)\r
+        public static Parsing.Title selectLongestTitle(Parsing.DVD thisDvd)\r
         {\r
-            int current_largest = 0;\r
-            Parsing.Title title2Select;\r
-\r
-            // Check if there are titles in the DVD title dropdown menu and make sure, it's not just "Automatic"\r
-            if (drp_dvdtitle.Items[0].ToString() != "Automatic")\r
-                title2Select = (Parsing.Title)drp_dvdtitle.Items[0];\r
-            else\r
-                title2Select = null;\r
+            TimeSpan longestDurationFound = TimeSpan.FromSeconds(0.0);\r
+            Parsing.Title returnTitle = null;\r
 \r
-            // So, If there are titles in the DVD Title dropdown menu, lets select the longest.\r
-            if (title2Select != null)\r
+            foreach (Parsing.Title item in thisDvd.Titles)\r
             {\r
-                foreach (Parsing.Title x in drp_dvdtitle.Items)\r
+                if (item.Duration > longestDurationFound)\r
                 {\r
-                    string title = x.ToString();\r
-                    if (title != "Automatic")\r
-                    {\r
-                        string[] y = title.Split(' ');\r
-                        string time = y[1].Replace("(", "").Replace(")", "");\r
-                        string[] z = time.Split(':');\r
-\r
-                        int hours = int.Parse(z[0]) * 60 * 60;\r
-                        int minutes = int.Parse(z[1]) * 60;\r
-                        int seconds = int.Parse(z[2]);\r
-                        int total_sec = hours + minutes + seconds;\r
-\r
-                        if (current_largest == 0)\r
-                        {\r
-                            current_largest = hours + minutes + seconds;\r
-                            title2Select = x;\r
-                        }\r
-                        else\r
-                        {\r
-                            if (total_sec > current_largest)\r
-                            {\r
-                                current_largest = total_sec;\r
-                                title2Select = x;\r
-                            }\r
-                        }\r
-                    }\r
+                    returnTitle = item;\r
+                    longestDurationFound = item.Duration;\r
                 }\r
             }\r
-            return title2Select;\r
+            return returnTitle;\r
         }\r
 \r
         /// <summary>\r
         /// Set's up the DataGridView on the Chapters tab (frmMain)\r
         /// </summary>\r
-        public static DataGridView chapterNaming(DataGridView data_chpt, string chapter_end)\r
+        public static DataGridView chapterNaming(DataGridView dataChpt, string chapterEnd)\r
         {\r
             int i = 0, finish = 0;\r
 \r
-            if (chapter_end != "Auto")\r
-                int.TryParse(chapter_end, out finish);\r
+            if (chapterEnd != "Auto")\r
+                int.TryParse(chapterEnd, out finish);\r
 \r
             while (i < finish)\r
             {\r
-                int n = data_chpt.Rows.Add();\r
-                data_chpt.Rows[n].Cells[0].Value = (i + 1);\r
-                data_chpt.Rows[n].Cells[1].Value = "Chapter " + (i + 1);\r
-                data_chpt.Rows[n].Cells[0].ValueType = typeof(int);\r
-                data_chpt.Rows[n].Cells[1].ValueType = typeof(string);\r
+                int n = dataChpt.Rows.Add();\r
+                dataChpt.Rows[n].Cells[0].Value = (i + 1);\r
+                dataChpt.Rows[n].Cells[1].Value = "Chapter " + (i + 1);\r
+                dataChpt.Rows[n].Cells[0].ValueType = typeof(int);\r
+                dataChpt.Rows[n].Cells[1].ValueType = typeof(string);\r
                 i++;\r
             }\r
 \r
-            return data_chpt;\r
+            return dataChpt;\r
+        }\r
+\r
+        /// <summary>\r
+        /// Import a CSV file which contains Chapter Names\r
+        /// </summary>\r
+        /// <param name="dataChpt"></param>\r
+        /// <param name="filename"></param>\r
+        /// <returns></returns>\r
+        public static DataGridView importChapterNames(DataGridView dataChpt, string filename)\r
+        {\r
+            IDictionary<int, string> chapterMap = new Dictionary<int, string>();\r
+            try\r
+            {\r
+                StreamReader sr = new StreamReader(filename);\r
+                string csv = sr.ReadLine();\r
+                while (csv != null)\r
+                {\r
+                    if (csv.Trim() != "")\r
+                    {\r
+                        string[] contents = csv.Split(',');\r
+                        int chapter;\r
+                        int.TryParse(contents[0], out chapter);\r
+                        chapterMap.Add(chapter, contents[1]);\r
+                    }\r
+                    csv = sr.ReadLine();\r
+                }\r
+            }\r
+            catch (Exception)\r
+            {\r
+                return null;\r
+            }\r
+\r
+            foreach (DataGridViewRow item in dataChpt.Rows)\r
+            {\r
+                string name;\r
+                chapterMap.TryGetValue((int)item.Cells[0].Value, out name);\r
+                item.Cells[1].Value = name ?? "Chapter " + item.Cells[0].Value;\r
+            }\r
+\r
+            return dataChpt;\r
         }\r
 \r
         /// <summary>\r
         /// Function which generates the filename and path automatically based on \r
         /// the Source Name, DVD title and DVD Chapters\r
         /// </summary>\r
-        public static string autoName(ComboBox drp_dvdtitle, string chapter_start, string chatper_end, string source, string dest, int format)\r
+        public static string autoName(frmMain mainWindow) //ComboBox drpDvdtitle, string chapter_start, string chatper_end, string source, string dest, int format, Boolean chapters)\r
         {\r
             string AutoNamePath = string.Empty;\r
-            if (drp_dvdtitle.Text != "Automatic")\r
+            if (mainWindow.drp_dvdtitle.Text != "Automatic")\r
             {\r
                 // Get the Source Name \r
-                string sourceName = Path.GetFileNameWithoutExtension(source);\r
+                string sourceName = mainWindow.SourceName;\r
 \r
                 // Get the Selected Title Number\r
-                string[] titlesplit = drp_dvdtitle.Text.Split(' ');\r
+                string[] titlesplit = mainWindow.drp_dvdtitle.Text.Split(' ');\r
                 string dvdTitle = titlesplit[0].Replace("Automatic", "");\r
 \r
                 // Get the Chapter Start and Chapter End Numbers\r
-                string chapterStart = chapter_start.Replace("Auto", "");\r
-                string chapterFinish = chatper_end.Replace("Auto", "");\r
+                string chapterStart = mainWindow.drop_chapterStart.Text.Replace("Auto", "");\r
+                string chapterFinish = mainWindow.drop_chapterFinish.Text.Replace("Auto", "");\r
                 string combinedChapterTag = chapterStart;\r
                 if (chapterFinish != chapterStart && chapterFinish != "")\r
                     combinedChapterTag = chapterStart + "-" + chapterFinish;\r
 \r
                 // Get the destination filename.\r
-                string destination_filename;\r
+                string destinationFilename;\r
                 if (Properties.Settings.Default.autoNameFormat != "")\r
                 {\r
-                    destination_filename = Properties.Settings.Default.autoNameFormat;\r
-                    destination_filename = destination_filename.Replace("{source}", sourceName).Replace("{title}", dvdTitle).Replace("{chapters}", combinedChapterTag);\r
+                    destinationFilename = Properties.Settings.Default.autoNameFormat;\r
+                    destinationFilename = destinationFilename.Replace("{source}", sourceName).Replace("{title}", dvdTitle).Replace("{chapters}", combinedChapterTag);\r
                 }\r
                 else\r
-                    destination_filename = sourceName + "_T" + dvdTitle + "_C" + combinedChapterTag;\r
+                    destinationFilename = sourceName + "_T" + dvdTitle + "_C" + combinedChapterTag;\r
 \r
                 // Add the appropriate file extension\r
-                if (format == 0)\r
+                if (mainWindow.drop_format.SelectedIndex == 0)\r
                 {\r
-                    if (Properties.Settings.Default.useM4v)\r
-                        destination_filename += ".m4v";\r
+                    if (Properties.Settings.Default.useM4v || mainWindow.Check_ChapterMarkers.Checked || mainWindow.AudioSettings.RequiresM4V() || mainWindow.Subtitles.RequiresM4V())\r
+                        destinationFilename += ".m4v";\r
                     else\r
-                        destination_filename += ".mp4";\r
+                        destinationFilename += ".mp4";\r
                 }\r
-                else if (format == 1)\r
-                    destination_filename += ".mkv";\r
+                else if (mainWindow.drop_format.SelectedIndex == 1)\r
+                    destinationFilename += ".mkv";\r
 \r
                 // Now work out the path where the file will be stored.\r
                 // First case: If the destination box doesn't already contain a path, make one.\r
-                if (!dest.Contains(Path.DirectorySeparatorChar.ToString()))\r
+                if (!mainWindow.text_destination.Text.Contains(Path.DirectorySeparatorChar.ToString()))\r
                 {\r
                     // If there is an auto name path, use it...\r
                     if (Properties.Settings.Default.autoNamePath.Trim() != "" && Properties.Settings.Default.autoNamePath.Trim() != "Click 'Browse' to set the default location")\r
-                        AutoNamePath = Path.Combine(Properties.Settings.Default.autoNamePath, destination_filename);\r
+                        AutoNamePath = Path.Combine(Properties.Settings.Default.autoNamePath, destinationFilename);\r
                     else // ...otherwise, output to the source directory\r
                         AutoNamePath = null;\r
                 }\r
                 else // Otherwise, use the path that is already there.\r
                 {\r
                     // Use the path and change the file extension to match the previous destination\r
-                    AutoNamePath = Path.Combine(Path.GetDirectoryName(dest), destination_filename);\r
-                    AutoNamePath = Path.ChangeExtension(AutoNamePath, Path.GetExtension(dest));\r
+                    AutoNamePath = Path.Combine(Path.GetDirectoryName(mainWindow.text_destination.Text), destinationFilename);\r
+\r
+                    if (Path.HasExtension(mainWindow.text_destination.Text))\r
+                        AutoNamePath = Path.ChangeExtension(AutoNamePath, Path.GetExtension(mainWindow.text_destination.Text));\r
                 }\r
             }\r
 \r
@@ -202,6 +197,15 @@ namespace Handbrake.Functions
 \r
             // 0 = SVN Build / Version\r
             // 1 = Build Date\r
+\r
+            DateTime lastModified = File.GetLastWriteTime("HandBrakeCLI.exe");\r
+\r
+\r
+            if (Properties.Settings.Default.cliLastModified == lastModified && Properties.Settings.Default.hb_build != 0)\r
+                return;\r
+\r
+            Properties.Settings.Default.cliLastModified = lastModified;\r
+            \r
             Process cliProcess = new Process();\r
             ProcessStartInfo handBrakeCLI = new ProcessStartInfo("HandBrakeCLI.exe", " -u")\r
                                                 {\r
@@ -222,6 +226,7 @@ namespace Handbrake.Functions
                 {\r
                     line = stdOutput.ReadLine() ?? "";\r
                     Match m = Regex.Match(line, @"HandBrake ([0-9.]*)(svn[0-9M]*) \([0-9]*\)");\r
+                    Match platform = Regex.Match(line, @"- ([A-Za-z0-9\s ]*) -");\r
 \r
                     if (m.Success)\r
                     {\r
@@ -231,6 +236,9 @@ namespace Handbrake.Functions
                         Properties.Settings.Default.hb_build = int.Parse(arr[1]);\r
                         Properties.Settings.Default.hb_version = arr[0];\r
                     }\r
+                    if (platform.Success)\r
+                        Properties.Settings.Default.hb_platform = platform.Value.Replace("-", "").Trim();\r
+\r
                     if (cliProcess.TotalProcessorTime.Seconds > 10) // Don't wait longer than 10 seconds.\r
                     {\r
                         Process cli = Process.GetProcessById(cliProcess.Id);\r
@@ -238,6 +246,7 @@ namespace Handbrake.Functions
                             cli.Kill();\r
                     }\r
                 }\r
+                Properties.Settings.Default.Save();\r
             }\r
             catch (Exception e)\r
             {\r
@@ -250,7 +259,7 @@ namespace Handbrake.Functions
         /// If it does, it means the last queue did not complete before HandBrake closed.\r
         /// So, return a boolean if true. \r
         /// </summary>\r
-        public static Boolean check_queue_recovery()\r
+        public static Boolean checkQueueRecovery()\r
         {\r
             try\r
             {\r
@@ -286,34 +295,36 @@ namespace Handbrake.Functions
             // avoiding any previous instances of HandBrakeCLI.exe in before.\r
             // Kill the current process.\r
 \r
-            Process[] hbProcesses = Process.GetProcessesByName("HandBrakeCLI");\r
+            DateTime startTime = DateTime.Now;\r
+            TimeSpan duration;\r
 \r
-            // Another hack. We maybe need to wait a few seconds for HandBrakeCLI to launch\r
-            if (hbProcesses.Length == 0)\r
+            Process[] hbProcesses = Process.GetProcessesByName("HandBrakeCLI");\r
+            while (hbProcesses.Length == 0)\r
             {\r
-                Thread.Sleep(2000);\r
                 hbProcesses = Process.GetProcessesByName("HandBrakeCLI");\r
+                duration = DateTime.Now - startTime;\r
+                if (duration.Seconds > 5 && hbProcesses.Length == 0) // Make sure we don't wait forever if the process doesn't start\r
+                    return -1;\r
             }\r
 \r
             Process hbProcess = null;\r
-            if (hbProcesses.Length > 0)\r
-                foreach (Process process in hbProcesses)\r
+            foreach (Process process in hbProcesses)\r
+            {\r
+                Boolean found = false;\r
+                // Check if the current CLI instance was running before we started the current one\r
+                foreach (Process bprocess in before)\r
                 {\r
-                    Boolean found = false;\r
-                    // Check if the current CLI instance was running before we started the current one\r
-                    foreach (Process bprocess in before)\r
-                    {\r
-                        if (process.Id == bprocess.Id)\r
-                            found = true;\r
-                    }\r
+                    if (process.Id == bprocess.Id)\r
+                        found = true;\r
+                }\r
 \r
-                    // If it wasn't running before, we found the process we want.\r
-                    if (!found)\r
-                    {\r
-                        hbProcess = process;\r
-                        break;\r
-                    }\r
+                // If it wasn't running before, we found the process we want.\r
+                if (!found)\r
+                {\r
+                    hbProcess = process;\r
+                    break;\r
                 }\r
+            }\r
             if (hbProcess != null)\r
                 return hbProcess.Id;\r
 \r
@@ -333,9 +344,7 @@ namespace Handbrake.Functions
                 foreach (FileInfo file in logFiles)\r
                 {\r
                     if (!file.Name.Contains("last_scan_log") && !file.Name.Contains("last_encode_log") && !file.Name.Contains("tmp_appReadable_log.txt"))\r
-                    {\r
                         File.Delete(file.FullName);\r
-                    }\r
                 }\r
             }\r
         }\r