OSDN Git Service

added new parsing code to parse cli output into objects
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmReadDVD.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.ComponentModel;\r
4 using System.Data;\r
5 using System.Drawing;\r
6 using System.Text;\r
7 using System.Windows.Forms;\r
8 \r
9 namespace Handbrake\r
10 {\r
11     public partial class frmReadDVD : Form\r
12     {\r
13 \r
14         string inputFile;\r
15 \r
16         public frmReadDVD(string inputFile)\r
17         {\r
18             InitializeComponent();\r
19             this.inputFile = inputFile;\r
20         }\r
21 \r
22         public void scan(string filename)\r
23         {\r
24             string query = "-i " + '"' + filename + '"' + " -t0";\r
25             System.Diagnostics.Process hbProc = new System.Diagnostics.Process();\r
26             hbProc.StartInfo.FileName = "hbcli.exe";\r
27             hbProc.StartInfo.RedirectStandardOutput = true;\r
28             hbProc.StartInfo.RedirectStandardError = true;\r
29             hbProc.StartInfo.Arguments = query;\r
30             hbProc.StartInfo.UseShellExecute = false;\r
31             hbProc.Start();\r
32             System.IO.StreamReader errorReader = new System.IO.StreamReader(new System.IO.BufferedStream(hbProc.StandardError.BaseStream));\r
33             hbProc.WaitForExit();\r
34             hbProc.Close();\r
35 \r
36             //Parsing.DVD thisDvd = Parsing.DVD.Parse(errorReader);\r
37 \r
38             String DvdData = errorReader.ReadToEnd();\r
39             DvdData = DvdData + "-- end --";\r
40 \r
41             String[] DvdDataArr = DvdData.Split('\n');\r
42             int DvdDataSize = DvdDataArr.Length -1;\r
43             String line = "";\r
44             int counter = 0;\r
45 \r
46             //\r
47             // Some varbiles used for parseing HandBrakes output\r
48             //\r
49 \r
50             // DVD info stroage varibles\r
51             string titleData  = "";\r
52             string duationData  = "";\r
53             string sizeData  = "";\r
54             string cropdata  = "";\r
55             string chatperData  = "";\r
56             string audioData  = "";\r
57             string subtitleData  = "";\r
58 \r
59             string fullTitleData  = "";\r
60 \r
61             // Position Pointers\r
62             bool chapterPointer  = false;\r
63             bool audioPointer = false;\r
64             bool subtitlePointer = false;\r
65 \r
66             // Error handling varibles\r
67             bool titleError = false;\r
68             bool readError = false;\r
69             \r
70             while (counter <= DvdDataSize)\r
71             {\r
72                 line = DvdDataArr[counter];\r
73                 counter++;\r
74                  \r
75                 // Get all the 1 liner data and set chaper potiner to true when done\r
76                 if (line.Contains("exited.")){\r
77                     subtitlePointer = false;\r
78                     fullTitleData = titleData.Trim() + " ~ " + duationData.Trim() + " ~ " + sizeData.Trim() + " ~ " + cropdata.Trim() + " ~ " + chatperData.Trim() + " ~ " + audioData.Trim() + " ~ " + subtitleData.Trim();\r
79                     add(fullTitleData, titleData, duationData);\r
80                     counter++;\r
81                 }else if (line.Contains("+ title")){\r
82                     if (titleData != "") {\r
83                         subtitlePointer = true;\r
84                         fullTitleData = titleData.Trim() + " ~ " + duationData.Trim() + " ~ " + sizeData.Trim() + " ~ " + cropdata.Trim() + " ~ " + chatperData.Trim() + " ~ " + audioData.Trim() + " ~ " + subtitleData.Trim();\r
85                         add(fullTitleData, titleData, duationData);\r
86                         counter = counter + 1;\r
87                     }\r
88                     titleData = line;\r
89                 }else if (line.Contains("+ duration")) {\r
90                     duationData = line;\r
91                 }else if (line.Contains("+ size")) {\r
92                     sizeData = line;\r
93                 }else if (line.Contains("+ autocrop")) {\r
94                     cropdata = line;\r
95                 }else if (line.Contains("+ chapters")) {\r
96                     chatperData = line;\r
97                     chapterPointer = true;\r
98                 }\r
99 \r
100                 // Get all the chapter information in 1 varible\r
101                 if (chapterPointer == true)\r
102                 {\r
103                     if (!line.Contains("+ audio"))\r
104                     {\r
105                         chapterPointer = false;\r
106                         audioPointer = true;\r
107                         audioData = line;\r
108                    }\r
109                     else \r
110                     {\r
111                         if (!chatperData.Equals(line))\r
112                         {\r
113                             chatperData = chatperData + " & " + line.Trim();\r
114                         }\r
115                     }\r
116                 }\r
117 \r
118                  // Get all the audio channel information in 1 varible\r
119                 if (audioPointer == true)\r
120                 { \r
121                     if (line.Contains("+ subtitle"))\r
122                     {\r
123                         audioPointer = false;\r
124                         subtitlePointer = true;\r
125                         subtitleData = line;\r
126                     } \r
127                     else \r
128                     {\r
129                         if (!audioData.Equals(line))\r
130                         {\r
131                             audioData = audioData + " & " + line.Trim();\r
132                         }\r
133                     }\r
134                  }\r
135 \r
136                  //Get all the subtitle data into 1 varible\r
137                  if (subtitlePointer == true)\r
138                  {\r
139                             if (line.Contains("+ subtitle")) \r
140                             {\r
141                                 subtitleData = line;\r
142                             } else \r
143                             {\r
144                                 if (!subtitleData.Equals(line))\r
145                                 {\r
146                                     subtitleData = subtitleData + " & " + line.Trim();\r
147                                 }\r
148                             }\r
149                   }\r
150 \r
151                   // Handle some of Handbrakes Error outputs if they occur.\r
152                   if (line.Contains("No title"))\r
153                   {\r
154                     titleError = true;\r
155                   }\r
156 \r
157                   if (line.Contains("***")) \r
158                   {\r
159                     readError = true;\r
160                   }\r
161 \r
162                  // Display error messages for errors detected above.\r
163                  if (readError == true)\r
164                  {\r
165                      MessageBox.Show("Some DVD Title information may be missing however you may still be able to select your required title for encoding!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
166                  }\r
167 \r
168                  if (titleError == true)\r
169                  {\r
170                      MessageBox.Show("No Title(s) found. Please make sure you have selected a valid, non-copy protected source.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\r
171                  }\r
172 \r
173             }\r
174         }\r
175 \r
176         public void add(string fullTitleData, string titleData, string durationData)\r
177         {\r
178 \r
179             try\r
180             {\r
181                 string t = titleData.Trim().Substring(8).Replace(":", "");\r
182                 string d = durationData.Trim().Substring(12);\r
183 \r
184                 // Lets store the captured full title data as a string in the programs settings file.\r
185                 // This can then be used by the DVD title dropdown menu to populate other fields.\r
186 \r
187                 Properties.Settings.Default.FullDVDInfo = Properties.Settings.Default.FullDVDInfo + " \n " + fullTitleData;\r
188 \r
189                 //Now lets add the info to the main form dropdowns\r
190                 frmMain form = (frmMain)frmMain.ActiveForm;\r
191                 string title = t + " " + " " + d + " ";\r
192                 form.drp_dvdtitle.Items.Add(title);\r
193             }\r
194             catch (Exception)\r
195             {\r
196                 // Don't really need to do anything about it.\r
197             }\r
198         }\r
199 \r
200 \r
201         private void btn_ok_Click(object sender, EventArgs e)\r
202         {\r
203             scan(inputFile);\r
204         }\r
205 \r
206        \r
207     }\r
208 }