OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Parsing / Title.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Text;\r
4 using System.Drawing;\r
5 using System.IO;\r
6 using System.Windows.Forms;\r
7 using System.Text.RegularExpressions;\r
8 \r
9 namespace Handbrake.Parsing\r
10 {\r
11     /// <summary>\r
12     /// An object that represents a single Title of a DVD\r
13     /// </summary>\r
14     public class Title\r
15     {\r
16         private List<Chapter> m_chapters;\r
17         /// <summary>\r
18         /// Collection of chapters in this Title\r
19         /// </summary>\r
20         public List<Chapter> Chapters\r
21         {\r
22             get\r
23             {\r
24                 return this.m_chapters;\r
25             }\r
26         }\r
27 \r
28         private List<AudioTrack> m_audioTracks;\r
29         /// <summary>\r
30         /// Collection of audio tracks associated with this Title\r
31         /// </summary>\r
32         public List<AudioTrack> AudioTracks\r
33         {\r
34             get\r
35             {\r
36                 return this.m_audioTracks;\r
37             }\r
38         }\r
39 \r
40         private List<Subtitle> m_subtitles;\r
41         /// <summary>\r
42         /// Collection of subtitles associated with this Title\r
43         /// </summary>\r
44         public List<Subtitle> Subtitles\r
45         {\r
46             get\r
47             {\r
48                 return this.m_subtitles;\r
49             }\r
50         }\r
51 \r
52         private int m_titleNumber;\r
53         /// <summary>\r
54         /// The track number of this Title\r
55         /// </summary>\r
56         public int TitleNumber\r
57         {\r
58             get\r
59             {\r
60                 return this.m_titleNumber;\r
61             }\r
62         }\r
63 \r
64         private TimeSpan m_duration;\r
65         /// <summary>\r
66         /// The length in time of this Title\r
67         /// </summary>\r
68         public TimeSpan Duration\r
69         {\r
70             get\r
71             {\r
72                 return this.m_duration;\r
73             }\r
74         }\r
75 \r
76         private Size m_resolution;\r
77         /// <summary>\r
78         /// The resolution (width/height) of this Title\r
79         /// </summary>\r
80         public Size Resolution\r
81         {\r
82             get\r
83             {\r
84                 return this.m_resolution;\r
85             }\r
86         }\r
87 \r
88         private string m_aspectRatio;\r
89         /// <summary>\r
90         /// The aspect ratio of this Title\r
91         /// </summary>\r
92         public string AspectRatio\r
93         {\r
94             get\r
95             {\r
96                 return this.m_aspectRatio;\r
97             }\r
98         }\r
99 \r
100         private int[] m_autoCrop;\r
101         /// <summary>\r
102         /// The automatically detected crop region for this Title.\r
103         /// This is an int array with 4 items in it as so:\r
104         /// 0: \r
105         /// 1: \r
106         /// 2: \r
107         /// 3: \r
108         /// </summary>\r
109         public int[] AutoCropDimensions\r
110         {\r
111             get\r
112             {\r
113                 return this.m_autoCrop;\r
114             }\r
115         }\r
116 \r
117         /// <summary>\r
118         /// The constructor for this object\r
119         /// </summary>\r
120         public Title()\r
121         {\r
122             this.m_audioTracks = new List<AudioTrack>();\r
123             this.m_chapters = new List<Chapter>();\r
124             this.m_subtitles = new List<Subtitle>();\r
125         }\r
126 \r
127         /// <summary>\r
128         /// Override of the ToString method to provide an easy way to use this object in the UI\r
129         /// </summary>\r
130         /// <returns>A string representing this track in the format: {title #} (00:00:00)</returns>\r
131         public override string ToString()\r
132         {\r
133                    return string.Format("{0} ({1:00}:{2:00}:{3:00})", this.m_titleNumber, this.m_duration.Hours,\r
134                     this.m_duration.Minutes, this.m_duration.Seconds);\r
135         }\r
136 \r
137 \r
138         /*\r
139          * \r
140          * There is some pretty extensive exception handling in here. A number of people have been seeing random problems.\r
141          * It just helps pinpointing which line of code is causing the issue.\r
142          * Can be cut down at a later date.\r
143          * \r
144          */\r
145         public static Title Parse(StringReader output)\r
146         {\r
147             Title thisTitle = new Title();\r
148             try\r
149             {\r
150                 Match m = Regex.Match(output.ReadLine(), @"^\+ title ([0-9]*):");\r
151                 try\r
152                 {\r
153                     // Match track number for this title\r
154                     if (m.Success)\r
155                     {\r
156                         //MessageBox.Show(m.Groups[1].Value);\r
157                         thisTitle.m_titleNumber = int.Parse(m.Groups[1].Value.Trim().ToString()); \r
158                         //.Trim().ToString() Not sure why this is needed but some systems seem to get a rogue \r
159                     }\r
160                                     }\r
161                 catch (Exception exc)\r
162                 {\r
163                     if (Properties.Settings.Default.GuiDebug == "Checked")\r
164                     {\r
165                         MessageBox.Show("Title.cs - Track Number " + exc.ToString());\r
166                     }\r
167                 }\r
168 \r
169                 output.ReadLine();\r
170 \r
171                 // Get duration for this title\r
172                 try\r
173                 {\r
174                     m = Regex.Match(output.ReadLine(), @"^  \+ duration: ([0-9]{2}:[0-9]{2}:[0-9]{2})");\r
175                     if (m.Success)\r
176                     {\r
177                         thisTitle.m_duration = TimeSpan.Parse(m.Groups[1].Value);\r
178                     }\r
179                 }\r
180                 catch (Exception exc)\r
181                 {\r
182                     if (Properties.Settings.Default.GuiDebug == "Checked")\r
183                     {\r
184                         MessageBox.Show("Title.cs - Duration " + exc.ToString());\r
185                     }\r
186                 }\r
187 \r
188                 try\r
189                 {\r
190                     // Get resolution, aspect ratio and FPS for this title\r
191                     m = Regex.Match(output.ReadLine(), @"^  \+ size: ([0-9]*)x([0-9]*), aspect: ([0-9]*\.[0-9]*), ([0-9]*\.[0-9]*) fps");\r
192                     if (m.Success)\r
193                     {\r
194                         thisTitle.m_resolution = new Size(int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
195                         thisTitle.m_aspectRatio = m.Groups[3].ToString(); // Converted to a String from float. Caused issue on french systems\r
196                         // French system floats are 1,78 not 1.78 and the CLI always outputs a . \r
197                     }\r
198                 }\r
199                 catch (Exception exc)\r
200                 {\r
201                     if (Properties.Settings.Default.GuiDebug == "Checked")\r
202                     {\r
203                         MessageBox.Show("Title.cs - Resolution and Aspect " + exc.ToString());\r
204                     }\r
205                 }\r
206 \r
207                 try\r
208                 {\r
209                     // Get autocrop region for this title\r
210                     m = Regex.Match(output.ReadLine(), @"^  \+ autocrop: ([0-9]*)/([0-9]*)/([0-9]*)/([0-9]*)");\r
211                     if (m.Success)\r
212                     {\r
213                         thisTitle.m_autoCrop = new int[4] { int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value), int.Parse(m.Groups[3].Value), int.Parse(m.Groups[4].Value) };\r
214                     }\r
215                 }\r
216                 catch (Exception exc)\r
217                 {\r
218                     if (Properties.Settings.Default.GuiDebug == "Checked")\r
219                     {\r
220                         MessageBox.Show("Title.cs - Auto Crop " + exc.ToString());\r
221                     }\r
222                 }\r
223 \r
224 \r
225                 try\r
226                 {\r
227                     thisTitle.m_chapters.AddRange(Chapter.ParseList(output));\r
228                 }\r
229                 catch (Exception exc)\r
230                 {\r
231                     if (Properties.Settings.Default.GuiDebug == "Checked")\r
232                     {\r
233                         MessageBox.Show("Title.cs - Chapters EXC " + exc.ToString());\r
234                     }\r
235                 }\r
236 \r
237                 try\r
238                 {\r
239                     thisTitle.m_audioTracks.AddRange(AudioTrack.ParseList(output));\r
240                 }\r
241                 catch (Exception exc)\r
242                 {\r
243                     if (Properties.Settings.Default.GuiDebug == "Checked")\r
244                     {\r
245                         MessageBox.Show("Title.cs - Audio EXC " + exc.ToString());\r
246                     }\r
247                 }\r
248 \r
249                 try\r
250                 {\r
251                     thisTitle.m_subtitles.AddRange(Subtitle.ParseList(output));\r
252                 }\r
253                 catch (Exception exc)\r
254                 {\r
255                     if (Properties.Settings.Default.GuiDebug == "Checked")\r
256                     {\r
257                         MessageBox.Show("Title.cs - Subtitles EXC " + exc.ToString());\r
258                     }\r
259                 }\r
260             }\r
261             catch (Exception exc)\r
262             {\r
263                 if (Properties.Settings.Default.GuiDebug == "Checked")\r
264                 {\r
265                     MessageBox.Show("Title.cs - Parse " + exc.ToString());\r
266                 }\r
267             }\r
268      \r
269         \r
270             return thisTitle;\r
271         }\r
272 \r
273         public static Title[] ParseList(string output)\r
274         {\r
275             List<Title> titles = new List<Title>();\r
276             try\r
277             {\r
278                 StringReader sr = new StringReader(output);\r
279                 while ((char)sr.Peek() == '+')\r
280                 {\r
281                     titles.Add(Title.Parse(sr));\r
282                 }\r
283             }\r
284             catch (Exception exc)\r
285             {\r
286                 if (Properties.Settings.Default.GuiDebug == "Checked")\r
287                 {\r
288                     MessageBox.Show("Title.cs - ParseList " + exc.ToString());\r
289                 }\r
290             }\r
291             return titles.ToArray();\r
292         }\r
293     }\r
294 }\r