summaryrefslogtreecommitdiffstats
path: root/macosx/HBStateFormatter.m
blob: 876b10d41493dcd255a1975480b3a1154b8f419e (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
/* HBStateFormatter.m

 This file is part of the HandBrake source code.
 Homepage: <http://handbrake.fr/>.
 It may be used under the terms of the GNU General Public License. */

#import "HBStateFormatter.h"
#import "HBLocalizationUtilities.h"
#include "hb.h"

@implementation HBStateFormatter

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        _twoLines = YES;
        _showPassNumber = YES;
    }
    return self;
}

- (NSString *)stateToString:(hb_state_t)s
{
    NSMutableString *string = [NSMutableString string];

    switch (s.state)
    {
#define p s.param.working

        case HB_STATE_SEARCHING:
        {
            NSString *desc = [NSString localizedStringWithFormat:HBKitLocalizedString(@"Searching for start point:  %.2f %%", @"HBStateFormatter -> search pass display name"),
             100.0 * p.progress];
            [string appendString:desc];

            if (p.seconds > -1)
            {
                NSString *eta = [NSString stringWithFormat:@"%02d:%02d:%02d", p.hours, p.minutes, p.seconds];
                [string appendFormat:HBKitLocalizedString(@" (ETA %@)", @"HBStateFormatter -> search time format"), eta];
            }

            break;
        }

        case HB_STATE_WORKING:
        {
            if (_title)
            {
                [string appendFormat:HBKitLocalizedString(@"Encoding %@ ", @"HBStateFormatter -> work pass display name"), _title];
                if (_twoLines)
                {
                    [string appendString:@"\n"];
                }
            }

            if (_showPassNumber)
            {
                if (p.pass_count > -1)
                {
                    if (p.pass_id == HB_PASS_SUBTITLE)
                    {
                        NSString *desc = [NSString localizedStringWithFormat:HBKitLocalizedString(@"Pass %d %@ of %d, %.2f %%", @"HBStateFormatter -> work pass number format"),
                                          p.pass,
                                          HBKitLocalizedString(@"(subtitle scan)", @"HBStateFormatter -> work pass type format"),
                                          p.pass_count, 100.0 * p.progress];
                        [string appendString:desc];
                    }
                    else
                    {
                        NSString *desc = [NSString localizedStringWithFormat:HBKitLocalizedString(@"Pass %d of %d, %.2f %%", @"HBStateFormatter -> work pass number format"),
                                          p.pass, p.pass_count, 100.0 * p.progress];
                        [string appendString:desc];
                    }
                }
                else
                {
                    [string appendString:HBKitLocalizedString(@"Pass 1", @"HBStateFormatter -> work first pass number format")];
                }
            }

            if (p.seconds > -1)
            {
                NSString *eta = [NSString stringWithFormat:@"%02d:%02d:%02d", p.hours, p.minutes, p.seconds];

                if (p.rate_cur > 0.0)
                {
                     NSString *desc = [NSString localizedStringWithFormat:HBKitLocalizedString(@" (%.2f fps, avg %.2f fps, ETA %@)", @"HBStateFormatter -> work time format"),
                                       p.rate_cur, p.rate_avg, eta];
                    [string appendString:desc];

                }
                else
                {
                    NSString *desc = [NSString localizedStringWithFormat:HBKitLocalizedString(@" (ETA %@)", @"HBStateFormatter -> work time format"),
                                      eta];
                    [string appendString:desc];
                }
            }

            break;
        }
#undef p

        case HB_STATE_MUXING:
        {
            [string appendString:HBKitLocalizedString(@"Muxing…", @"HBStateFormatter -> pass display name")];
            break;
        }

        case HB_STATE_PAUSED:
        {
            [string appendString:HBKitLocalizedString(@"Paused", @"HBStateFormatter -> pass display name")];
            break;
        }

        case HB_STATE_SCANNING:
        {
#define p s.param.scanning
            if (p.preview_cur)
            {
                [string appendFormat:
                 HBKitLocalizedString(@"Scanning title %d of %d, preview %d…", @"HBStateFormatter -> scan pass format"),
                 p.title_cur, p.title_count,
                 p.preview_cur];
            }
            else
            {
                [string appendFormat:
                 HBKitLocalizedString(@"Scanning title %d of %d…", @"HBStateFormatter -> scan pass format"),
                 p.title_cur, p.title_count];
            }
#undef p
            break;
        }

        default:
            break;
    }

    return string;
}

- (float)stateToPercentComplete:(hb_state_t)s
{
    float progress = 0;

    switch (s.state)
    {
        case HB_STATE_SEARCHING:
        case HB_STATE_WORKING:
        case HB_STATE_PAUSED:
#define p s.param.working
            if (p.pass_count > 0)
            {
                progress = (p.progress + p.pass - 1) / p.pass_count;
            }
#undef p

            break;

        case HB_STATE_SCANNING:
#define p s.param.scanning
            progress = p.progress;
#undef p
            break;

        case HB_STATE_MUXING:
            progress = 1;
            break;

        default:
            break;
    }

    if (progress < 0)
    {
        progress = 0;
    }
    else if (progress > 1)
    {
        progress = 1;
    }

    return progress;
}

@end