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.m0k.org/>.\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             try\r
146             {\r
147                 Match m = Regex.Match(output.ReadLine(), @"^\+ title ([0-9]*):");\r
148                 // Match track number for this title\r
149                 if (m.Success)\r
150                     thisTitle.m_titleNumber = int.Parse(m.Groups[1].Value.Trim().ToString());\r
151 \r
152                 output.ReadLine();\r
153 \r
154                 // Get duration for this title\r
155 \r
156                 m = Regex.Match(output.ReadLine(), @"^  \+ duration: ([0-9]{2}:[0-9]{2}:[0-9]{2})");\r
157                 if (m.Success)\r
158                     thisTitle.m_duration = TimeSpan.Parse(m.Groups[1].Value);\r
159 \r
160                 // Get resolution, aspect ratio and FPS for this title\r
161                 m = Regex.Match(output.ReadLine(), @"^  \+ size: ([0-9]*)x([0-9]*), aspect: ([0-9]*\.[0-9]*), ([0-9]*\.[0-9]*) fps");\r
162                 if (m.Success)\r
163                 {\r
164                     thisTitle.m_resolution = new Size(int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
165                     thisTitle.m_aspectRatio = float.Parse(m.Groups[3].Value, Functions.CLI.Culture);\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                     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
172 \r
173                 thisTitle.m_chapters.AddRange(Chapter.ParseList(output));\r
174 \r
175                 thisTitle.m_audioTracks.AddRange(AudioTrack.ParseList(output));\r
176 \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             return thisTitle;\r
185         }\r
186 \r
187         public static Title[] ParseList(string output)\r
188         {\r
189             List<Title> titles = new List<Title>();\r
190             try\r
191             {\r
192                 StringReader sr = new StringReader(output);\r
193                 while ((char)sr.Peek() == '+')\r
194                 {\r
195                     titles.Add(Title.Parse(sr));\r
196                 }\r
197             }\r
198             catch (Exception exc)\r
199             {\r
200                 MessageBox.Show("Title.cs - ParseList " + exc.ToString());\r
201             }\r
202             return titles.ToArray();\r
203         }\r
204     }\r
205 }\r