OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / Parsing / Title.cs
1 /*  Title.cs $\r
2         \r
3            This file is part of the HandBrake source code.\r
4            Homepage: <http://handbrake.fr>.\r
5            It may be used under the terms of the GNU General Public License. */\r
6 \r
7 using System;\r
8 using System.Collections.Generic;\r
9 using System.Drawing;\r
10 using System.IO;\r
11 using System.Windows.Forms;\r
12 using System.Text.RegularExpressions;\r
13 \r
14 namespace Handbrake.Parsing\r
15 {\r
16     /// <summary>\r
17     /// An object that represents a single Title of a DVD\r
18     /// </summary>\r
19     public class Title\r
20     {\r
21         private List<Chapter> m_chapters;\r
22         /// <summary>\r
23         /// Collection of chapters in this Title\r
24         /// </summary>\r
25         public List<Chapter> Chapters\r
26         {\r
27             get\r
28             {\r
29                 return this.m_chapters;\r
30             }\r
31         }\r
32 \r
33         private List<AudioTrack> m_audioTracks;\r
34         /// <summary>\r
35         /// Collection of audio tracks associated with this Title\r
36         /// </summary>\r
37         public List<AudioTrack> AudioTracks\r
38         {\r
39             get\r
40             {\r
41                 return this.m_audioTracks;\r
42             }\r
43         }\r
44 \r
45         private List<Subtitle> m_subtitles;\r
46         /// <summary>\r
47         /// Collection of subtitles associated with this Title\r
48         /// </summary>\r
49         public List<Subtitle> Subtitles\r
50         {\r
51             get\r
52             {\r
53                 return this.m_subtitles;\r
54             }\r
55         }\r
56 \r
57         private int m_titleNumber;\r
58         /// <summary>\r
59         /// The track number of this Title\r
60         /// </summary>\r
61         public int TitleNumber\r
62         {\r
63             get\r
64             {\r
65                 return this.m_titleNumber;\r
66             }\r
67         }\r
68 \r
69         private TimeSpan m_duration;\r
70         /// <summary>\r
71         /// The length in time of this Title\r
72         /// </summary>\r
73         public TimeSpan Duration\r
74         {\r
75             get\r
76             {\r
77                 return this.m_duration;\r
78             }\r
79         }\r
80 \r
81         private Size m_resolution;\r
82         /// <summary>\r
83         /// The resolution (width/height) of this Title\r
84         /// </summary>\r
85         public Size Resolution\r
86         {\r
87             get\r
88             {\r
89                 return this.m_resolution;\r
90             }\r
91         }\r
92 \r
93         private float m_aspectRatio;\r
94         /// <summary>\r
95         /// The aspect ratio of this Title\r
96         /// </summary>\r
97         public float AspectRatio\r
98         {\r
99             get\r
100             {\r
101                 return this.m_aspectRatio;\r
102             }\r
103         }\r
104 \r
105         private int[] m_autoCrop;\r
106         /// <summary>\r
107         /// The automatically detected crop region for this Title.\r
108         /// This is an int array with 4 items in it as so:\r
109         /// 0: \r
110         /// 1: \r
111         /// 2: \r
112         /// 3: \r
113         /// </summary>\r
114         public int[] AutoCropDimensions\r
115         {\r
116             get\r
117             {\r
118                 return this.m_autoCrop;\r
119             }\r
120         }\r
121 \r
122         /// <summary>\r
123         /// The constructor for this object\r
124         /// </summary>\r
125         public Title()\r
126         {\r
127             this.m_audioTracks = new List<AudioTrack>();\r
128             this.m_chapters = new List<Chapter>();\r
129             this.m_subtitles = new List<Subtitle>();\r
130         }\r
131 \r
132         /// <summary>\r
133         /// Override of the ToString method to provide an easy way to use this object in the UI\r
134         /// </summary>\r
135         /// <returns>A string representing this track in the format: {title #} (00:00:00)</returns>\r
136         public override string ToString()\r
137         {\r
138             return string.Format("{0} ({1:00}:{2:00}:{3:00})", this.m_titleNumber, this.m_duration.Hours,\r
139              this.m_duration.Minutes, this.m_duration.Seconds);\r
140         }\r
141 \r
142         public static Title Parse(StringReader output)\r
143         {\r
144             Title thisTitle = new Title();\r
145 \r
146             Match m = Regex.Match(output.ReadLine(), @"^\+ title ([0-9]*):");\r
147             // Match track number for this title\r
148             if (m.Success)\r
149                 thisTitle.m_titleNumber = int.Parse(m.Groups[1].Value.Trim().ToString());\r
150 \r
151             String testData = output.ReadLine();\r
152 \r
153             // Get duration for this title\r
154 \r
155             m = Regex.Match(output.ReadLine(), @"^  \+ duration: ([0-9]{2}:[0-9]{2}:[0-9]{2})");\r
156             if (m.Success)\r
157                 thisTitle.m_duration = TimeSpan.Parse(m.Groups[1].Value);\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 = float.Parse(m.Groups[3].Value, Functions.Encode.Culture);\r
165             }\r
166 \r
167             // Get autocrop region for this title\r
168             m = Regex.Match(output.ReadLine(), @"^  \+ autocrop: ([0-9]*)/([0-9]*)/([0-9]*)/([0-9]*)");\r
169             if (m.Success)\r
170                 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
171 \r
172             thisTitle.m_chapters.AddRange(Chapter.ParseList(output));\r
173 \r
174             thisTitle.m_audioTracks.AddRange(AudioTrack.ParseList(output));\r
175 \r
176             thisTitle.m_subtitles.AddRange(Subtitle.ParseList(output));\r
177 \r
178             return thisTitle;\r
179         }\r
180 \r
181         public static Title[] ParseList(string output)\r
182         {\r
183             List<Title> titles = new List<Title>();\r
184             StringReader sr = new StringReader(output);\r
185 \r
186             while (sr.Peek() == '+' || sr.Peek() == ' ')\r
187             {\r
188                 // If the the character is a space, then chances are the line\r
189                 if (sr.Peek() == ' ') // If the character is a space, then chances are it's the combing detected line.\r
190                     sr.ReadLine(); // Skip over it\r
191                 else\r
192                     titles.Add(Title.Parse(sr));              \r
193             }\r
194 \r
195             return titles.ToArray();\r
196         }\r
197     }\r
198 }\r