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         public static Title Parse(StringReader output)\r
138         {\r
139             Title thisTitle = new Title();\r
140             try\r
141             {\r
142                 Match m = Regex.Match(output.ReadLine(), @"^\+ title ([0-9]*):");\r
143                 try\r
144                 {\r
145                     // Match track number for this title\r
146                     if (m.Success)\r
147                     {\r
148                         thisTitle.m_titleNumber = int.Parse(m.Groups[1].Value);\r
149                     }\r
150                     output.ReadLine();\r
151                 }\r
152                 catch (Exception exc)\r
153                 {\r
154                     MessageBox.Show("Title.cs - Track Number " + exc.ToString());\r
155                 }\r
156   \r
157 \r
158                 // Get duration for this title\r
159                 try\r
160                 {\r
161                     m = Regex.Match(output.ReadLine(), @"^  \+ duration: ([0-9]{2}:[0-9]{2}:[0-9]{2})");\r
162                     if (m.Success)\r
163                     {\r
164                         thisTitle.m_duration = TimeSpan.Parse(m.Groups[1].Value);\r
165                     }\r
166                 }\r
167                 catch (Exception exc)\r
168                 {\r
169                     MessageBox.Show("Title.cs - Duration " + exc.ToString());\r
170                 }\r
171 \r
172                 try\r
173                 {\r
174                     // Get resolution, aspect ratio and FPS for this title\r
175                     m = Regex.Match(output.ReadLine(), @"^  \+ size: ([0-9]*)x([0-9]*), aspect: ([0-9]*\.[0-9]*), ([0-9]*\.[0-9]*) fps");\r
176                     if (m.Success)\r
177                     {\r
178                         thisTitle.m_resolution = new Size(int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
179                         thisTitle.m_aspectRatio = m.Groups[3].ToString(); // Converted to a String from float. Caused issue on french systems\r
180                         // French system floats are 1,78 not 1.78 and the CLI always outputs a . \r
181                     }\r
182                 }\r
183                 catch (Exception exc)\r
184                 {\r
185                     MessageBox.Show("Title.cs - Resolution and Aspect " + exc.ToString());\r
186                 }\r
187 \r
188                 try\r
189                 {\r
190                     // Get autocrop region for this title\r
191                     m = Regex.Match(output.ReadLine(), @"^  \+ autocrop: ([0-9]*)/([0-9]*)/([0-9]*)/([0-9]*)");\r
192                     if (m.Success)\r
193                     {\r
194                         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
195                     }\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                     thisTitle.m_audioTracks.AddRange(AudioTrack.ParseList(output));\r
207                     thisTitle.m_subtitles.AddRange(Subtitle.ParseList(output));\r
208                 }\r
209                 catch (Exception exc)\r
210                 {\r
211                     MessageBox.Show("Title.cs - Chapters / Audio / Subtitles " + exc.ToString());\r
212                 }\r
213             }\r
214             catch (Exception exc)\r
215             {\r
216                 MessageBox.Show("Title.cs - Parse " + exc.ToString());\r
217             }\r
218      \r
219         \r
220             return thisTitle;\r
221         }\r
222 \r
223         public static Title[] ParseList(string output)\r
224         {\r
225             List<Title> titles = new List<Title>();\r
226             try\r
227             {\r
228                 StringReader sr = new StringReader(output);\r
229                 while ((char)sr.Peek() == '+')\r
230                 {\r
231                     titles.Add(Title.Parse(sr));\r
232                 }\r
233             }\r
234             catch (Exception exc)\r
235             {\r
236                 MessageBox.Show("Title.cs - ParseList " + exc.ToString());\r
237             }\r
238             return titles.ToArray();\r
239         }\r
240     }\r
241 }\r