2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
6 namespace HandBrake.ApplicationServices.Parsing
\r
8 using System.Collections.Generic;
\r
10 using System.Text.RegularExpressions;
\r
12 using HandBrake.ApplicationServices.Model;
\r
13 using HandBrake.Framework.Helpers;
\r
16 /// An object that represents a subtitle associated with a Title, in a DVD
\r
18 public class Subtitle
\r
21 /// Gets or sets the track number of this Subtitle
\r
23 public int TrackNumber { get; set; }
\r
26 /// Gets or sets the The language (if detected) of this Subtitle
\r
28 public string Language { get; set; }
\r
31 /// Gets or sets the Langauage Code
\r
33 public string LanguageCode { get; set; }
\r
36 /// Gets or sets the Subtitle Type
\r
38 public SubtitleType SubtitleType { get; set; }
\r
41 /// Gets Subtitle Type
\r
43 public string TypeString
\r
47 return EnumHelper.GetDescription(this.SubtitleType);
\r
52 /// Parse the input strings related to subtitles
\r
54 /// <param name="output">
\r
58 /// A Subitle object
\r
60 public static Subtitle Parse(StringReader output)
\r
62 string curLine = output.ReadLine();
\r
64 // + 1, English (iso639-2: eng) (Text)(SSA)
\r
65 // + 1, English (iso639-2: eng) (Text)(UTF-8)
\r
66 Match m = Regex.Match(curLine, @"^ \+ ([0-9]*), ([A-Za-z, ]*) \((.*)\) \(([a-zA-Z]*)\)\(([a-zA-Z0-9\-]*)\)");
\r
68 if (m.Success && !curLine.Contains("HandBrake has exited."))
\r
70 var thisSubtitle = new Subtitle
\r
72 TrackNumber = int.Parse(m.Groups[1].Value.Trim()),
\r
73 Language = m.Groups[2].Value,
\r
74 LanguageCode = m.Groups[3].Value,
\r
77 switch (m.Groups[5].Value)
\r
80 thisSubtitle.SubtitleType = SubtitleType.VobSub;
\r
83 thisSubtitle.SubtitleType = SubtitleType.SRT;
\r
86 thisSubtitle.SubtitleType = SubtitleType.CC;
\r
89 thisSubtitle.SubtitleType = SubtitleType.UTF8Sub;
\r
92 thisSubtitle.SubtitleType = SubtitleType.TX3G;
\r
95 thisSubtitle.SubtitleType = SubtitleType.SSA;
\r
98 thisSubtitle.SubtitleType = SubtitleType.Unknown;
\r
102 return thisSubtitle;
\r
108 /// Parse a list of Subtitle tracks from an input string.
\r
110 /// <param name="output">
\r
114 /// An array of Subtitle objects
\r
116 public static IEnumerable<Subtitle> ParseList(StringReader output)
\r
118 var subtitles = new List<Subtitle>();
\r
119 while ((char)output.Peek() != '+')
\r
121 Subtitle thisSubtitle = Parse(output);
\r
123 if (thisSubtitle != null)
\r
124 subtitles.Add(thisSubtitle);
\r
128 return subtitles.ToArray();
\r
132 /// Override of the ToString method to make this object easier to use in the UI
\r
134 /// <returns>A string formatted as: {track #} {language}</returns>
\r
135 public override string ToString()
\r
137 return string.Format("{0} {1} ({2})", TrackNumber, Language, TypeString);
\r