forked from HandBrake/HandBrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHBJob+HBAdditions.m
285 lines (259 loc) · 10.2 KB
/
HBJob+HBAdditions.m
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* 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 "handbrake/handbrake.h"
static NSDateFormatter *_timeFormatter = nil;
static NSDateFormatter *_dateFormatter = nil;
static NSDateFormatter *_dateISOFormatter = nil;
static NSDateFormatter *_releaseDateFormatter = nil;
@implementation HBJob (HBAdditions)
+ (void)initialize
{
if (self == [HBJob class])
{
_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateStyle:NSDateFormatterShortStyle];
[_dateFormatter setTimeStyle:NSDateFormatterNoStyle];
_dateISOFormatter = [[NSDateFormatter alloc] init];
[_dateISOFormatter setDateFormat:@"yyyy-MM-dd"];
_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
{
NSUserDefaults *ud = NSUserDefaults.standardUserDefaults;
NSMutableString *name = [[NSMutableString alloc] init];
NSDateFormatter *formatter = [ud boolForKey:HBAutoNamingISODateFormat] ? _dateISOFormatter : _dateFormatter;
NSString *sourceName = self.title.name;
NSDate *creationDate = self.title.metadata.releaseDate.length ?
[_releaseDateFormatter dateFromString:self.title.metadata.releaseDate] :
nil;
if (creationDate == nil)
{
NSDictionary *attrs = [NSFileManager.defaultManager attributesOfItemAtPath:self.fileURL.path error:nil];
creationDate = attrs[NSFileCreationDate];
}
for (NSString *formatKey in [ud objectForKey:HBAutoNamingFormat])
{
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)self.title.index];
}
else if ([formatKey isEqualToString:@"{Chapters}"])
{
NSRange chaptersRange = NSMakeRange(self.range.chapterStart + 1, self.range.chapterStop + 1);
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:@"{Preset}"])
{
[name appendString:self.presetName];
}
else if ([formatKey isEqualToString:@"{Width}"])
{
[name appendFormat:@"%d", self.picture.storageWidth];
}
else if ([formatKey isEqualToString:@"{Height}"])
{
[name appendFormat:@"%d", self.picture.storageHeight];
}
else if ([formatKey isEqualToString:@"{Codec}"])
{
int vcodec = self.video.encoder;
NSString *codecName = @"Unknown";
if (vcodec & HB_VCODEC_AV1_MASK)
{
codecName = @"AV1";
}
else if (vcodec & HB_VCODEC_H264_MASK)
{
codecName = @"H.264";
}
else if (vcodec & HB_VCODEC_H265_MASK)
{
codecName = @"H.265";
}
else if (vcodec == HB_VCODEC_THEORA)
{
codecName = @"Theora";
}
else if (vcodec == HB_VCODEC_FFMPEG_MPEG2)
{
codecName = @"MPEG-2";
}
else if (vcodec == HB_VCODEC_FFMPEG_MPEG4)
{
codecName = @"MPEG-4";
}
else if (vcodec == HB_VCODEC_FFMPEG_VP8)
{
codecName = @"VP8";
}
else if (vcodec == HB_VCODEC_FFMPEG_VP9 || vcodec == HB_VCODEC_FFMPEG_VP9_10BIT)
{
codecName = @"VP9";
}
[name appendString:codecName];
}
else if ([formatKey isEqualToString:@"{Encoder}"])
{
[name appendString:@(hb_video_encoder_get_short_name(self.video.encoder))];
}
else if ([formatKey isEqualToString:@"{Bit-Depth}"])
{
[name appendFormat:@"%dbit", hb_video_encoder_get_depth(self.video.encoder)];
}
else if ([formatKey isEqualToString:@"{Quality/Bitrate}"])
{
if (self.video.qualityType == HBVideoQualityTypeAvgBitrate)
{
[name appendString:[NSString stringWithFormat:@"%d", self.video.avgBitrate]];
}
else
{
[name appendString:[NSString stringWithFormat:@"%0.2f", self.video.quality]];
}
}
else if ([formatKey isEqualToString:@"{Quality-Type}"])
{
if (self.video.qualityType == HBVideoQualityTypeAvgBitrate)
{
[name appendString:@"kbps"];
}
else
{
// Append the right quality suffix for the selected codec (rf/qp)
[name appendString:[@(hb_video_quality_get_name(self.video.encoder)) lowercaseString]];
}
}
else if ([formatKey isEqualToString:@"{Date}"])
{
NSDate *date = [NSDate date];
NSString *dateString = [[formatter 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}"])
{
if (creationDate)
{
NSString *dateString = [[formatter stringFromDate:creationDate] stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
[name appendString:dateString];
}
}
else if ([formatKey isEqualToString:@"{Creation-Time}"])
{
if (creationDate)
{
[name appendString:[_timeFormatter stringFromDate:creationDate]];
}
}
else if ([formatKey isEqualToString:@"{Modification-Date}"])
{
NSDictionary *attrs = [NSFileManager.defaultManager attributesOfItemAtPath:self.fileURL.path error:nil];
NSDate *modificationDate = attrs[NSFileModificationDate];
if (modificationDate)
{
NSString *dateString = [[formatter stringFromDate:modificationDate] stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
[name appendString:dateString];
}
}
else if ([formatKey isEqualToString:@"{Modification-Time}"])
{
NSDictionary *attrs = [NSFileManager.defaultManager attributesOfItemAtPath:self.fileURL.path error:nil];
NSDate *modificationDate = attrs[NSFileModificationDate];
if (modificationDate)
{
[name appendString:[_timeFormatter stringFromDate:modificationDate]];
}
}
else
{
[name appendString:formatKey];
}
}
return [name copy];
}
- (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
{
NSString *fileName = self.title.name;
// Create an output filename by using the format set in the preferences
if ([NSUserDefaults.standardUserDefaults boolForKey:HBDefaultAutoNaming])
{
fileName = self.automaticName;
}
// Automatic name can be empty if the format is empty
if (fileName.length == 0)
{
fileName = self.title.name;
}
return [fileName stringByAppendingPathExtension:self.automaticExt];
}
- (void)setDestinationFolderURL:(NSURL *)destinationFolderURL sameAsSource:(BOOL)useSourceFolderDestination
{
// If destination mode is set to same as source, try to set the source folder url
if (self.title.isStream && useSourceFolderDestination)
{
NSURL *titleParentURL = self.title.url.URLByDeletingLastPathComponent;
if ([titleParentURL.path hasPrefix:destinationFolderURL.path])
{
destinationFolderURL = titleParentURL;
}
}
self.destinationFolderURL = destinationFolderURL;
}
@end