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