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