summaryrefslogtreecommitdiffstats
path: root/win/C#/HandBrake.ApplicationServices/Functions/Converters.cs
blob: c4ba59f148305c47a53748335eb0f0185ba60007 (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
namespace HandBrake.ApplicationServices.Functions
{
    using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;

    /// <summary>
    /// A class to convert various things to native C# objects
    /// </summary>
    public class Converters
    {
        /// <summary>
        /// Convert HandBrakes time remaining into a TimeSpan
        /// </summary>
        /// <param name="time">
        /// The time remaining for the encode.
        /// </param>
        /// <returns>
        /// A TimepSpan object
        /// </returns>
        public static TimeSpan EncodeToTimespan(string time)
        {
            TimeSpan converted = new TimeSpan(0, 0, 0, 0);

            Match m = Regex.Match(time.Trim(), @"^([0-9]{2}:[0-9]{2}:[0-9]{2})");
            if (m.Success)
            {
                TimeSpan.TryParse(m.Groups[0].Value, out converted);
            }

            return converted;
        }

        /// <summary>
        /// Video Framerate Converter
        /// </summary>
        private static readonly Dictionary<double, int> vrates = new Dictionary<double, int>
        {
           {5, 5400000},
           {10, 2700000},
           {12, 2250000},
           {15, 1800000},
           {23.976, 1126125},
           {24, 1125000},
           {25, 1080000},
           {29.97, 900900}
        };

        /// <summary>
        /// Convert the desired framerate to the video rate.
        /// </summary>
        /// <param name="framerate">
        /// The framerate.
        /// </param>
        /// <returns>
        /// The Video Rate.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// </exception>
        public static int FramerateToVrate(double framerate)
        {
            if (!vrates.ContainsKey(framerate))
            {
                throw new ArgumentException("Framerate not recognized.", "framerate");
            }

            return vrates[framerate];
        }
    }
}