blob: 9d10c52d674176c1d9430094f3804ad9f75cd961 (
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
|
/* HBJob+HBAdditions.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 "HBJob+HBAdditions.h"
#import "HBPreferencesKeys.h"
#import "hb.h"
static NSDateFormatter *_timeFormatter = nil;
static NSDateFormatter *_dateFormatter = nil;
static NSDateFormatter *_releaseDateFormatter = nil;
@implementation HBJob (HBAdditions)
+ (void)initialize
{
if (self == [HBJob class]) {
_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateStyle:NSDateFormatterShortStyle];
[_dateFormatter setTimeStyle:NSDateFormatterNoStyle];
_timeFormatter = [[NSDateFormatter alloc] init];
[_timeFormatter setDateStyle:NSDateFormatterNoStyle];
[_timeFormatter setTimeStyle:NSDateFormatterShortStyle];
_releaseDateFormatter = [[NSDateFormatter alloc] init];
[_releaseDateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
}
}
- (NSString *)automaticName
{
HBTitle *title = self.title;
NSDate *releaseDate = title.metadata.releaseDate.length ? [_releaseDateFormatter dateFromString:title.metadata.releaseDate] : nil;
if (releaseDate == nil)
{
NSDictionary *fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:self.fileURL.path error:nil];
releaseDate = [fileAttribs objectForKey:NSFileCreationDate];
}
// Generate a new file name
NSString *fileName = [self automaticNameForSource:title.name
title:title.index
chapters:NSMakeRange(self.range.chapterStart + 1, self.range.chapterStop + 1)
quality:self.video.qualityType ? self.video.quality : 0
bitrate:!self.video.qualityType ? self.video.avgBitrate : 0
videoCodec:self.video.encoder
creationDate:releaseDate];
return fileName;
}
- (NSString *)automaticExt
{
NSString *extension = @(hb_container_get_default_extension(self.container));
if (self.container & HB_MUX_MASK_MP4)
{
BOOL anyCodecAC3 = [self.audio anyCodecMatches:HB_ACODEC_AC3] || [self.audio anyCodecMatches:HB_ACODEC_AC3_PASS];
// Chapter markers are enabled if the checkbox is ticked and we are doing p2p or we have > 1 chapter
BOOL chapterMarkers = (self.chaptersEnabled) &&
(self.range.type != HBRangeTypeChapters || self.range.chapterStart < self.range.chapterStop);
NSString *defaultExtension = [NSUserDefaults.standardUserDefaults stringForKey:HBDefaultMpegExtension];
if ([defaultExtension isEqualToString:@".m4v"] ||
((YES == anyCodecAC3 || YES == chapterMarkers) && [defaultExtension isEqualToString:@"Auto"]))
{
extension = @"m4v";
}
}
return extension;
}
- (NSString *)defaultName
{
// Generate a new file name
NSString *fileName = self.title.name;
// If Auto Naming is on. We create an output filename by using the
// format set int he preferences.
if ([NSUserDefaults.standardUserDefaults boolForKey:HBDefaultAutoNaming])
{
fileName = [self automaticName];
}
// use the correct extension based on the container
NSString *ext = [self automaticExt];
fileName = [fileName stringByAppendingPathExtension:ext];
return fileName;
}
- (NSString *)automaticNameForSource:(NSString *)sourceName
title:(NSUInteger)title
chapters:(NSRange)chaptersRange
quality:(double)quality
bitrate:(int)bitrate
videoCodec:(uint32_t)codec
creationDate:(NSDate *)creationDate
{
NSUserDefaults *ud = NSUserDefaults.standardUserDefaults;
NSMutableString *name = [[NSMutableString alloc] init];
// The format array contains the tokens as NSString
NSArray<NSString *> *format = [ud objectForKey:HBAutoNamingFormat];
for (NSString *formatKey in format)
{
if ([formatKey isEqualToString:@"{Source}"])
{
if ([ud boolForKey:HBAutoNamingRemoveUnderscore])
{
sourceName = [sourceName stringByReplacingOccurrencesOfString:@"_" withString:@" "];
}
if ([ud boolForKey:HBAutoNamingRemovePunctuation])
{
sourceName = [sourceName stringByReplacingOccurrencesOfString:@"-" withString:@""];
sourceName = [sourceName stringByReplacingOccurrencesOfString:@"." withString:@""];
sourceName = [sourceName stringByReplacingOccurrencesOfString:@"," withString:@""];
sourceName = [sourceName stringByReplacingOccurrencesOfString:@";" withString:@""];
}
if ([ud boolForKey:HBAutoNamingTitleCase])
{
sourceName = [sourceName capitalizedString];
}
[name appendString:sourceName];
}
else if ([formatKey isEqualToString:@"{Title}"])
{
[name appendFormat:@"%lu", (unsigned long)title];
}
else if ([formatKey isEqualToString:@"{Date}"])
{
NSDate *date = [NSDate date];
NSString *dateString = [[_dateFormatter stringFromDate:date] stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
[name appendString:dateString];
}
else if ([formatKey isEqualToString:@"{Time}"])
{
NSDate *date = [NSDate date];
[name appendString:[_timeFormatter stringFromDate:date]];
}
else if ([formatKey isEqualToString:@"{Creation-Date}"])
{
NSString *dateString = [[_dateFormatter stringFromDate:creationDate] stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
[name appendString:dateString];
}
else if ([formatKey isEqualToString:@"{Creation-Time}"])
{
[name appendString:[_timeFormatter stringFromDate:creationDate]];
}
else if ([formatKey isEqualToString:@"{Chapters}"])
{
if (chaptersRange.location == chaptersRange.length)
{
[name appendFormat:@"%lu", (unsigned long)chaptersRange.location];
}
else
{
[name appendFormat:@"%lu-%lu", (unsigned long)chaptersRange.location, (unsigned long)chaptersRange.length];
}
}
else if ([formatKey isEqualToString:@"{Quality/Bitrate}"])
{
if (bitrate)
{
[name appendString:@"abr"];
[name appendString:[NSString stringWithFormat:@"%d", bitrate]];
}
else
{
// Append the right quality suffix for the selected codec (rf/qp)
[name appendString:[@(hb_video_quality_get_name(codec)) lowercaseString]];
[name appendString:[NSString stringWithFormat:@"%0.2f", quality]];
}
}
else
{
[name appendString:formatKey];
}
}
return [name copy];
}
@end
|