summaryrefslogtreecommitdiffstats
path: root/win/C#/Parsing/Title.cs
blob: 16c032edfabc3093a6c7b5355a5ec9d7e779d89e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Handbrake.Parsing
{
    /// <summary>
    /// An object that represents a single Title of a DVD
    /// </summary>
    public class Title
    {
        private List<Chapter> m_chapters;
        /// <summary>
        /// Collection of chapters in this Title
        /// </summary>
        public List<Chapter> Chapters
        {
            get
            {
                return this.m_chapters;
            }
        }

        private List<AudioTrack> m_audioTracks;
        /// <summary>
        /// Collection of audio tracks associated with this Title
        /// </summary>
        public List<AudioTrack> AudioTracks
        {
            get
            {
                return this.m_audioTracks;
            }
        }

        private List<Subtitle> m_subtitles;
        /// <summary>
        /// Collection of subtitles associated with this Title
        /// </summary>
        public List<Subtitle> Subtitles
        {
            get
            {
                return this.m_subtitles;
            }
        }

        private int m_titleNumber;
        /// <summary>
        /// The track number of this Title
        /// </summary>
        public int TitleNumber
        {
            get
            {
                return this.m_titleNumber;
            }
        }

        private TimeSpan m_duration;
        /// <summary>
        /// The length in time of this Title
        /// </summary>
        public TimeSpan Duration
        {
            get
            {
                return this.m_duration;
            }
        }

        private Size m_resolution;
        /// <summary>
        /// The resolution (width/height) of this Title
        /// </summary>
        public Size Resolution
        {
            get
            {
                return this.m_resolution;
            }
        }

        private string m_aspectRatio;
        /// <summary>
        /// The aspect ratio of this Title
        /// </summary>
        public string AspectRatio
        {
            get
            {
                return this.m_aspectRatio;
            }
        }

        private int[] m_autoCrop;
        /// <summary>
        /// The automatically detected crop region for this Title.
        /// This is an int array with 4 items in it as so:
        /// 0: 
        /// 1: 
        /// 2: 
        /// 3: 
        /// </summary>
        public int[] AutoCropDimensions
        {
            get
            {
                return this.m_autoCrop;
            }
        }

        /// <summary>
        /// The constructor for this object
        /// </summary>
        public Title()
        {
            this.m_audioTracks = new List<AudioTrack>();
            this.m_chapters = new List<Chapter>();
            this.m_subtitles = new List<Subtitle>();
        }

        /// <summary>
        /// Override of the ToString method to provide an easy way to use this object in the UI
        /// </summary>
        /// <returns>A string representing this track in the format: {title #} (00:00:00)</returns>
        public override string ToString()
        {
                   return string.Format("{0} ({1:00}:{2:00}:{3:00})", this.m_titleNumber, this.m_duration.Hours,
                    this.m_duration.Minutes, this.m_duration.Seconds);
        }


        /*
         * 
         * There is some pretty extensive exception handling in here. A number of people have been seeing random problems.
         * It just helps pinpointing which line of code is causing the issue.
         * Can be cut down at a later date.
         * 
         */
        public static Title Parse(StringReader output)
        {
            Title thisTitle = new Title();
            try
            {
                Match m = Regex.Match(output.ReadLine(), @"^\+ title ([0-9]*):");
                try
                {
                    // Match track number for this title
                    if (m.Success)
                    {
                        //MessageBox.Show(m.Groups[1].Value);
                        thisTitle.m_titleNumber = int.Parse(m.Groups[1].Value.Trim().ToString()); 
                        //.Trim().ToString() Not sure why this is needed but some systems seem to get a rogue 
                    }
                                    }
                catch (Exception exc)
                {
                    if (Properties.Settings.Default.GuiDebug == "Checked")
                    {
                        MessageBox.Show("Title.cs - Track Number " + exc.ToString());
                    }
                }

                output.ReadLine();

                // Get duration for this title
                try
                {
                    m = Regex.Match(output.ReadLine(), @"^  \+ duration: ([0-9]{2}:[0-9]{2}:[0-9]{2})");
                    if (m.Success)
                    {
                        thisTitle.m_duration = TimeSpan.Parse(m.Groups[1].Value);
                    }
                }
                catch (Exception exc)
                {
                    if (Properties.Settings.Default.GuiDebug == "Checked")
                    {
                        MessageBox.Show("Title.cs - Duration " + exc.ToString());
                    }
                }

                try
                {
                    // Get resolution, aspect ratio and FPS for this title
                    m = Regex.Match(output.ReadLine(), @"^  \+ size: ([0-9]*)x([0-9]*), aspect: ([0-9]*\.[0-9]*), ([0-9]*\.[0-9]*) fps");
                    if (m.Success)
                    {
                        thisTitle.m_resolution = new Size(int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value));
                        thisTitle.m_aspectRatio = m.Groups[3].ToString(); // Converted to a String from float. Caused issue on french systems
                        // French system floats are 1,78 not 1.78 and the CLI always outputs a . 
                    }
                }
                catch (Exception exc)
                {
                    if (Properties.Settings.Default.GuiDebug == "Checked")
                    {
                        MessageBox.Show("Title.cs - Resolution and Aspect " + exc.ToString());
                    }
                }

                try
                {
                    // Get autocrop region for this title
                    m = Regex.Match(output.ReadLine(), @"^  \+ autocrop: ([0-9]*)/([0-9]*)/([0-9]*)/([0-9]*)");
                    if (m.Success)
                    {
                        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) };
                    }
                }
                catch (Exception exc)
                {
                    if (Properties.Settings.Default.GuiDebug == "Checked")
                    {
                        MessageBox.Show("Title.cs - Auto Crop " + exc.ToString());
                    }
                }


                try
                {
                    thisTitle.m_chapters.AddRange(Chapter.ParseList(output));
                }
                catch (Exception exc)
                {
                    if (Properties.Settings.Default.GuiDebug == "Checked")
                    {
                        MessageBox.Show("Title.cs - Chapters EXC " + exc.ToString());
                    }
                }

                try
                {
                    thisTitle.m_audioTracks.AddRange(AudioTrack.ParseList(output));
                }
                catch (Exception exc)
                {
                    if (Properties.Settings.Default.GuiDebug == "Checked")
                    {
                        MessageBox.Show("Title.cs - Audio EXC " + exc.ToString());
                    }
                }

                try
                {
                    thisTitle.m_subtitles.AddRange(Subtitle.ParseList(output));
                }
                catch (Exception exc)
                {
                    if (Properties.Settings.Default.GuiDebug == "Checked")
                    {
                        MessageBox.Show("Title.cs - Subtitles EXC " + exc.ToString());
                    }
                }
            }
            catch (Exception exc)
            {
                if (Properties.Settings.Default.GuiDebug == "Checked")
                {
                    MessageBox.Show("Title.cs - Parse " + exc.ToString());
                }
            }
     
        
            return thisTitle;
        }

        public static Title[] ParseList(string output)
        {
            List<Title> titles = new List<Title>();
            try
            {
                StringReader sr = new StringReader(output);
                while ((char)sr.Peek() == '+')
                {
                    titles.Add(Title.Parse(sr));
                }
            }
            catch (Exception exc)
            {
                if (Properties.Settings.Default.GuiDebug == "Checked")
                {
                    MessageBox.Show("Title.cs - ParseList " + exc.ToString());
                }
            }
            return titles.ToArray();
        }
    }
}