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