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 track number for this title\r
143                 Match m = Regex.Match(output.ReadLine(), @"^\+ title ([0-9]*):");\r
144                 if (m.Success)\r
145                 {\r
146                     thisTitle.m_titleNumber = int.Parse(m.Groups[1].Value);\r
147                 }\r
148                 output.ReadLine();\r
149 \r
150                 // Get duration for this title\r
151 \r
152                 m = Regex.Match(output.ReadLine(), @"^  \+ duration: ([0-9]{2}:[0-9]{2}:[0-9]{2})");\r
153                 if (m.Success)\r
154                 {\r
155                     thisTitle.m_duration = TimeSpan.Parse(m.Groups[1].Value);\r
156                 }\r
157 \r
158 \r
159                 // Get resolution, aspect ratio and FPS for this title\r
160                 m = Regex.Match(output.ReadLine(), @"^  \+ size: ([0-9]*)x([0-9]*), aspect: ([0-9]*\.[0-9]*), ([0-9]*\.[0-9]*) fps");\r
161                 if (m.Success)\r
162                 {\r
163                     thisTitle.m_resolution = new Size(int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
164                     thisTitle.m_aspectRatio = m.Groups[3].ToString(); // Converted to a String from float. Caused issue on french systems\r
165                                                                       // French system floats are 1,78 not 1.78 and the CLI always outputs a . \r
166                 }\r
167 \r
168                 // Get autocrop region for this title\r
169                 m = Regex.Match(output.ReadLine(), @"^  \+ autocrop: ([0-9]*)/([0-9]*)/([0-9]*)/([0-9]*)");\r
170                 if (m.Success)\r
171                 {\r
172                     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
173                 }\r
174 \r
175                 thisTitle.m_chapters.AddRange(Chapter.ParseList(output));\r
176                 thisTitle.m_audioTracks.AddRange(AudioTrack.ParseList(output));\r
177                 thisTitle.m_subtitles.AddRange(Subtitle.ParseList(output));\r
178             }\r
179             catch (Exception exc)\r
180             {\r
181                 MessageBox.Show("Title.cs - Parse " + exc.ToString());\r
182             }\r
183      \r
184         \r
185             return thisTitle;\r
186         }\r
187 \r
188         public static Title[] ParseList(string output)\r
189         {\r
190             List<Title> titles = new List<Title>();\r
191             try\r
192             {\r
193                 StringReader sr = new StringReader(output);\r
194                 while ((char)sr.Peek() == '+')\r
195                 {\r
196                     titles.Add(Title.Parse(sr));\r
197                 }\r
198             }\r
199             catch (Exception exc)\r
200             {\r
201                 MessageBox.Show("Title.cs - ParseList " + exc.ToString());\r
202             }\r
203             return titles.ToArray();\r
204         }\r
205     }\r
206 }\r