/* Title.cs $ This file is part of the HandBrake source code. Homepage: . It may be used under the terms of the GNU General Public License. */ namespace Handbrake.Parsing { using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; using System.Text.RegularExpressions; /// /// An object that represents a single Title of a DVD /// public class Title { /// /// The Culture Info /// private static readonly CultureInfo Culture = new CultureInfo("en-US", false); /// /// A collection of Audio Tracks /// private readonly List audioTracks; /// /// A Collection of Chapters /// private readonly List chapters; /// /// A Collection of Subtitles /// private readonly List subtitles; /// /// A collection of angles /// private List angles = new List(); /// /// The source aspect ratio /// private float aspectRatio; /// /// The source framerate /// private float fps; /// /// Source autocrop values /// private int[] autoCrop; /// /// Source name /// private string source; /// /// The duration of the source /// private TimeSpan duration; /// /// The source resolution /// private Size resolution; /// /// The Title number /// private int titleNumber; /// /// Is A Main Title /// private bool mainTitle; /// /// The par values for this title. /// private Size parVal; /// /// Initializes a new instance of the class. /// The constructor for this object /// public Title() { audioTracks = new List(); chapters = new List(); subtitles = new List(); } /// /// Gets a Collection of chapters in this Title /// public List Chapters { get { return chapters; } } /// /// Gets a Collection of audio tracks associated with this Title /// public List AudioTracks { get { return audioTracks; } } /// /// Gets aCollection of subtitles associated with this Title /// public List Subtitles { get { return subtitles; } } /// /// Gets The track number of this Title /// public int TitleNumber { get { return this.titleNumber; } } /// /// Gets a value indicating whether this is a MainTitle. /// public bool MainTitle { get { return this.mainTitle; } } /// /// Gets the Source Name /// public string SourceName { get { return source; } } /// /// Gets the length in time of this Title /// public TimeSpan Duration { get { return duration; } } /// /// Gets the resolution (width/height) of this Title /// public Size Resolution { get { return resolution; } } /// /// Gets the aspect ratio of this Title /// public float AspectRatio { get { return aspectRatio; } } /// /// Gets Par Value /// public Size ParVal { get { return parVal; } } /// /// Gets the automatically detected crop region for this Title. /// This is an int array with 4 items in it as so: /// 0: /// 1: /// 2: /// 3: /// public int[] AutoCropDimensions { get { return autoCrop; } } /// /// Gets a Collection of Angles in this Title /// public List Angles { get { return angles; } } /// /// Gets the FPS of the source. /// public float Fps { get { return fps; } } /// /// Parse the Title Information /// /// A stingreader of output data /// A Title public static Title Parse(StringReader output) { var thisTitle = new Title(); Match m = Regex.Match(output.ReadLine(), @"^\+ title ([0-9]*):"); // Match track number for this title if (m.Success) thisTitle.titleNumber = int.Parse(m.Groups[1].Value.Trim()); // If we are scanning a groupd of files, we'll want to get the source name. string path = output.ReadLine(); m = Regex.Match(path, @" \+ Main Feature"); if (m.Success) { thisTitle.mainTitle = true; path = output.ReadLine(); } m = Regex.Match(path, @"^ \+ stream:"); if (m.Success) thisTitle.source = path.Replace("+ stream:", string.Empty).Trim(); if (!Properties.Settings.Default.noDvdNav) { // Get the Angles for the title. m = Regex.Match(output.ReadLine(), @" \+ angle\(s\) ([0-9])"); if (m.Success) { string angleList = m.Value.Replace("+ angle(s) ", string.Empty).Trim(); int angleCount; int.TryParse(angleList, out angleCount); for (int i = 1; i <= angleCount; i++) thisTitle.angles.Add(i.ToString()); } } // Get duration for this title m = Regex.Match(output.ReadLine(), @"^ \+ duration: ([0-9]{2}:[0-9]{2}:[0-9]{2})"); if (m.Success) thisTitle.duration = TimeSpan.Parse(m.Groups[1].Value); // Get resolution, aspect ratio and FPS for this title m = Regex.Match(output.ReadLine(), @"^ \+ size: ([0-9]*)x([0-9]*), pixel aspect: ([0-9]*)/([0-9]*), display aspect: ([0-9]*\.[0-9]*), ([0-9]*\.[0-9]*) fps"); if (m.Success) { thisTitle.resolution = new Size(int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value)); thisTitle.parVal = new Size(int.Parse(m.Groups[3].Value), int.Parse(m.Groups[4].Value)); thisTitle.aspectRatio = float.Parse(m.Groups[5].Value, Culture); thisTitle.fps = float.Parse(m.Groups[6].Value, Culture); } // Get autocrop region for this title m = Regex.Match(output.ReadLine(), @"^ \+ autocrop: ([0-9]*)/([0-9]*)/([0-9]*)/([0-9]*)"); if (m.Success) thisTitle.autoCrop = new[] { int.Parse(m.Groups[1].Value), int.Parse(m.Groups[2].Value), int.Parse(m.Groups[3].Value), int.Parse(m.Groups[4].Value) }; thisTitle.chapters.AddRange(Chapter.ParseList(output)); thisTitle.audioTracks.AddRange(AudioTrack.ParseList(output)); thisTitle.subtitles.AddRange(Subtitle.ParseList(output)); return thisTitle; } /// /// Return a list of parsed titles /// /// The Output /// A List of titles public static Title[] ParseList(string output) { var titles = new List(); var sr = new StringReader(output); while (sr.Peek() == '+' || sr.Peek() == ' ') { // If the the character is a space, then chances are the line if (sr.Peek() == ' ') // If the character is a space, then chances are it's the combing detected line. sr.ReadLine(); // Skip over it else titles.Add(Parse(sr)); } return titles.ToArray(); } /// <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})", titleNumber, duration.Hours, duration.Minutes, duration.Seconds); } } }