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.Globalization;\r
11 using System.IO;\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 static readonly CultureInfo Culture = new CultureInfo("en-US", false);\r
22         private readonly List<AudioTrack> m_audioTracks;\r
23         private readonly List<Chapter> m_chapters;\r
24         private readonly List<Subtitle> m_subtitles;\r
25         private List<String> m_angles = new List<string>();\r
26         private float m_aspectRatio;\r
27         private int[] m_autoCrop;\r
28         private TimeSpan m_duration;\r
29         private Size m_resolution;\r
30         private int m_titleNumber;\r
31         private Size m_parVal;\r
32         \r
33         /// <summary>\r
34         /// The constructor for this object\r
35         /// </summary>\r
36         public Title()\r
37         {\r
38             m_audioTracks = new List<AudioTrack>();\r
39             m_chapters = new List<Chapter>();\r
40             m_subtitles = new List<Subtitle>();\r
41         }\r
42 \r
43         /// <summary>\r
44         /// Collection of chapters in this Title\r
45         /// </summary>\r
46         public List<Chapter> Chapters\r
47         {\r
48             get { return m_chapters; }\r
49         }\r
50 \r
51         /// <summary>\r
52         /// Collection of audio tracks associated with this Title\r
53         /// </summary>\r
54         public List<AudioTrack> AudioTracks\r
55         {\r
56             get { return m_audioTracks; }\r
57         }\r
58 \r
59         /// <summary>\r
60         /// Collection of subtitles associated with this Title\r
61         /// </summary>\r
62         public List<Subtitle> Subtitles\r
63         {\r
64             get { return m_subtitles; }\r
65         }\r
66 \r
67         /// <summary>\r
68         /// The track number of this Title\r
69         /// </summary>\r
70         public int TitleNumber\r
71         {\r
72             get { return m_titleNumber; }\r
73         }\r
74 \r
75         /// <summary>\r
76         /// The length in time of this Title\r
77         /// </summary>\r
78         public TimeSpan Duration\r
79         {\r
80             get { return m_duration; }\r
81         }\r
82 \r
83         /// <summary>\r
84         /// The resolution (width/height) of this Title\r
85         /// </summary>\r
86         public Size Resolution\r
87         {\r
88             get { return m_resolution; }\r
89         }\r
90 \r
91         /// <summary>\r
92         /// The aspect ratio of this Title\r
93         /// </summary>\r
94         public float AspectRatio\r
95         {\r
96             get { return m_aspectRatio; }\r
97         }\r
98 \r
99         /// <summary>\r
100         /// Par Value\r
101         /// </summary>\r
102         public Size ParVal\r
103         {\r
104             get { return m_parVal; }\r
105         }\r
106 \r
107         /// <summary>\r
108         /// The automatically detected crop region for this Title.\r
109         /// This is an int array with 4 items in it as so:\r
110         /// 0: \r
111         /// 1: \r
112         /// 2: \r
113         /// 3: \r
114         /// </summary>\r
115         public int[] AutoCropDimensions\r
116         {\r
117             get { return m_autoCrop; }\r
118         }\r
119 \r
120         /// <summary>\r
121         /// Collection of Angles in this Title\r
122         /// </summary>\r
123         public List<string> Angles\r
124         {\r
125             get { return m_angles; }\r
126         }\r
127   \r
128         /// <summary>\r
129         /// Override of the ToString method to provide an easy way to use this object in the UI\r
130         /// </summary>\r
131         /// <returns>A string representing this track in the format: {title #} (00:00:00)</returns>\r
132         public override string ToString()\r
133         {\r
134             return string.Format("{0} ({1:00}:{2:00}:{3:00})", m_titleNumber, m_duration.Hours,\r
135                                  m_duration.Minutes, m_duration.Seconds);\r
136         }\r
137 \r
138         public static Title Parse(StringReader output)\r
139         {\r
140             var thisTitle = new Title();\r
141 \r
142             Match m = Regex.Match(output.ReadLine(), @"^\+ title ([0-9]*):");\r
143             // Match track number for this title\r
144             if (m.Success)\r
145                 thisTitle.m_titleNumber = int.Parse(m.Groups[1].Value.Trim());\r
146 \r
147             output.ReadLine();\r
148 \r
149             if (Properties.Settings.Default.dvdnav)\r
150             {\r
151                 // Get the Angles for the title.\r
152                 m = Regex.Match(output.ReadLine(), @"  \+ angle\(s\) ([0-9,])");\r
153                 if (m.Success)\r
154                 {\r
155                     String angleList = m.Value.Replace("+ angle(s) ", "").Trim();\r
156 \r
157                     if (angleList.Contains(","))\r
158                     {\r
159                         string[] angles = angleList.Split(',');\r
160                         foreach (string s in angles)\r
161                             thisTitle.m_angles.Add(s);\r
162                     }\r
163                     else\r
164                         thisTitle.m_angles.Add(m.Value.Replace("+ angle(s) ", "").Trim());\r
165                 }\r
166             }\r
167 \r
168             // Get duration for this title\r
169             m = Regex.Match(output.ReadLine(), @"^  \+ duration: ([0-9]{2}:[0-9]{2}:[0-9]{2})");\r
170             if (m.Success)\r
171                 thisTitle.m_duration = TimeSpan.Parse(m.Groups[1].Value);\r
172 \r
173             // Get resolution, aspect ratio and FPS for this title\r
174             m = Regex.Match(output.ReadLine(),\r
175                             @"^  \+ size: ([0-9]*)x([0-9]*), pixel aspect: ([0-9]*)/([0-9]*), display aspect: ([0-9]*\.[0-9]*), ([0-9]*\.[0-9]*) fps");\r
176             //size: 720x576, pixel aspect: 16/15, display aspect: 1.33, 25.000 fps\r
177 \r
178 \r
179 \r
180             if (m.Success)\r
181             {\r
182                 thisTitle.m_resolution = new Size(int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));\r
183                 thisTitle.m_parVal = new Size(int.Parse(m.Groups[3].Value), int.Parse(m.Groups[4].Value));\r
184                 thisTitle.m_aspectRatio = float.Parse(m.Groups[5].Value, Culture);\r
185             }\r
186 \r
187             // Get autocrop region for this title\r
188             m = Regex.Match(output.ReadLine(), @"^  \+ autocrop: ([0-9]*)/([0-9]*)/([0-9]*)/([0-9]*)");\r
189             if (m.Success)\r
190                 thisTitle.m_autoCrop = new int[]\r
191                                            {\r
192                                                int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value),\r
193                                                int.Parse(m.Groups[3].Value), int.Parse(m.Groups[4].Value)\r
194                                            };\r
195 \r
196             thisTitle.m_chapters.AddRange(Chapter.ParseList(output));\r
197 \r
198             thisTitle.m_audioTracks.AddRange(AudioTrack.ParseList(output));\r
199 \r
200             thisTitle.m_subtitles.AddRange(Subtitle.ParseList(output));\r
201 \r
202             return thisTitle;\r
203         }\r
204 \r
205         public static Title[] ParseList(string output)\r
206         {\r
207             var titles = new List<Title>();\r
208             var sr = new StringReader(output);\r
209 \r
210             while (sr.Peek() == '+' || sr.Peek() == ' ')\r
211             {\r
212                 // If the the character is a space, then chances are the line\r
213                 if (sr.Peek() == ' ') // If the character is a space, then chances are it's the combing detected line.\r
214                     sr.ReadLine(); // Skip over it\r
215                 else\r
216                     titles.Add(Parse(sr));\r
217             }\r
218 \r
219             return titles.ToArray();\r
220         }\r
221     }\r
222 }