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 \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 \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(StreamReader output)\r
138         {\r
139             Title thisTitle = new Title();\r
140 \r
141             /*\r
142              * This will be converted to use Regex soon, I promise ;)\r
143              * brianmario - 7/9/07\r
144              */\r
145         \r
146                 string curLine = output.ReadLine();\r
147                 thisTitle.m_titleNumber = int.Parse(curLine.Substring(curLine.Length - 2, 1));\r
148                 curLine = output.ReadLine();\r
149                 string[] splitter = curLine.Split(',');\r
150    \r
151                 splitter = splitter[2].Trim().Split(' ', '(', ')');\r
152 \r
153                 splitter = splitter[1].Split('-', '>');\r
154 \r
155                 curLine = output.ReadLine();\r
156                 splitter = curLine.Split(new string[] { "  + duration: " }, StringSplitOptions.RemoveEmptyEntries);\r
157                 thisTitle.m_duration = TimeSpan.Parse(splitter[0]);\r
158                 curLine = output.ReadLine();\r
159                 splitter = curLine.Split(new string[] { "  + size: ", "aspect: ", ", ", " fps", "x" }, StringSplitOptions.RemoveEmptyEntries);\r
160                 thisTitle.m_resolution = new Size(int.Parse(splitter[0]), int.Parse(splitter[1]));\r
161                 thisTitle.m_aspectRatio = float.Parse(splitter[2].ToString());\r
162      \r
163                 curLine = output.ReadLine();\r
164                 splitter = curLine.Split(new string[] { "  + autocrop: ", "/" }, StringSplitOptions.RemoveEmptyEntries);\r
165                 thisTitle.m_autoCrop = new int[4] { int.Parse(splitter[0]), int.Parse(splitter[1]), int.Parse(splitter[2]), int.Parse(splitter[3]) };\r
166 \r
167                 /* \r
168                  * \r
169                  * A Few Bugs that need fixed.\r
170                  * \r
171                  * \r
172                  * Problem 1\r
173                  * There is a small problem here... What happens if the DVD has no Subtitles? or Handbrake misses the Audio or Chapter track \r
174                  * data due to an error.\r
175                  * \r
176                  * hbcli will sit in a suspended state until it is forcefully closed.\r
177                  * \r
178                  * Problem 2\r
179                  * See AudioTrack.cs Line 80 for details\r
180                  * \r
181                  * Problem 3\r
182                  * Doesn't seem to support DVD's where the first track is 0 instead of 1, and only includes 1 title (TS/MPG files)\r
183                  * Simply Doesn't list any titles.\r
184                  * \r
185                  * \r
186                  */\r
187          \r
188                 thisTitle.m_chapters.AddRange(Chapter.ParseList(output));\r
189                 thisTitle.m_audioTracks.AddRange(AudioTrack.ParseList(output));\r
190                 thisTitle.m_subtitles.AddRange(Subtitle.ParseList(output));\r
191             \r
192             return thisTitle;\r
193         }\r
194 \r
195         public static Title[] ParseList(StreamReader output)\r
196         {\r
197             List<Title> titles = new List<Title>();\r
198             while ((char)output.Peek() == '+')\r
199             {\r
200                 titles.Add(Title.Parse(output));\r
201             }\r
202             return titles.ToArray();\r
203         }\r
204     }\r
205 }\r