Refactor the queue optimise feature to a stand alone action.

This commit is contained in:
Damiano Galassi committed 2018-03-21 18:00:15 +01:00
1 parent 0aaaa6bfd3
commit e3cd4fd77c
6 files changed
+83 -38

No files matched your search

+55 -13
View File
@@ -9,24 +9,27 @@ import Foundation
/// SBQueue actions protocol, actions can be run by the queue's items. /// SBQueue actions protocol, actions can be run by the queue's items.
//@objc(SBQueueActionProtocol) protocol QueueActionProtocol: NSSecureCoding { //@objc(SBQueueActionProtocol) protocol QueueActionProtocol: NSSecureCoding {
// @objc func runAction(_ item: SBQueueItem) // @objc func runAction(_ item: SBQueueItem) -> Bool
// @objc var localizedDescription: String { get } // @objc var localizedDescription: String { get }
//} //}
/// An action to remove existing metadata. /// An action to remove existing metadata.
@objc(SBQueueClearExistingMetadataAction) class QueueClearExistingMetadataAction: NSObject, SBQueueActionProtocol { @objc(SBQueueClearExistingMetadataAction) class QueueClearExistingMetadataAction: NSObject, SBQueueActionProtocol {
var type: SBQueueActionType { return .pre }
var localizedDescription: String { return NSLocalizedString("Clearing Metadata", comment: "Action localized description.") } var localizedDescription: String { return NSLocalizedString("Clearing Metadata", comment: "Action localized description.") }
override var description: String { return NSLocalizedString("Clear Metadata", comment: "Action description.") } override var description: String { return NSLocalizedString("Clear Metadata", comment: "Action description.") }
@objc override init() {} @objc override init() {}
func runAction(_ item: SBQueueItem) { func runAction(_ item: SBQueueItem) -> Bool {
if let metadata = item.mp4File?.metadata { if let metadata = item.mp4File?.metadata {
let dataTypes: UInt = MP42MetadataItemDataType.string.rawValue | MP42MetadataItemDataType.stringArray.rawValue | MP42MetadataItemDataType.bool.rawValue | MP42MetadataItemDataType.integer.rawValue | MP42MetadataItemDataType.integerArray.rawValue | MP42MetadataItemDataType.date.rawValue | MP42MetadataItemDataType.image.rawValue let dataTypes: UInt = MP42MetadataItemDataType.string.rawValue | MP42MetadataItemDataType.stringArray.rawValue | MP42MetadataItemDataType.bool.rawValue | MP42MetadataItemDataType.integer.rawValue | MP42MetadataItemDataType.integerArray.rawValue | MP42MetadataItemDataType.date.rawValue | MP42MetadataItemDataType.image.rawValue
metadata.removeItems(metadata.metadataItemsFiltered(by: MP42MetadataItemDataType(rawValue: dataTypes))) metadata.removeItems(metadata.metadataItemsFiltered(by: MP42MetadataItemDataType(rawValue: dataTypes)))
return true
} }
return false
} }
func encode(with aCoder: NSCoder) {} func encode(with aCoder: NSCoder) {}
@@ -37,17 +40,20 @@ import Foundation
/// An action that set a formatted file name. /// An action that set a formatted file name.
@objc(SBQueueSetOutputFilenameAction) class QueueSetOutputFilenameAction: NSObject, SBQueueActionProtocol { @objc(SBQueueSetOutputFilenameAction) class QueueSetOutputFilenameAction: NSObject, SBQueueActionProtocol {
var type: SBQueueActionType { return .pre }
var localizedDescription: String { return NSLocalizedString("Setting Name", comment: "Action localized description.") } var localizedDescription: String { return NSLocalizedString("Setting Name", comment: "Action localized description.") }
override var description: String { return NSLocalizedString("Set Name", comment: "Action description.") } override var description: String { return NSLocalizedString("Set Name", comment: "Action description.") }
@objc override init() {} @objc override init() {}
func runAction(_ item: SBQueueItem) { func runAction(_ item: SBQueueItem) -> Bool {
if let formattedName = item.mp4File?.formattedFileName() { if let formattedName = item.mp4File?.formattedFileName() {
let pathExtension = item.destURL.pathExtension let pathExtension = item.destURL.pathExtension
let destURL = item.destURL.deletingLastPathComponent().appendingPathComponent(formattedName).appendingPathExtension(pathExtension) let destURL = item.destURL.deletingLastPathComponent().appendingPathComponent(formattedName).appendingPathExtension(pathExtension)
item.destURL = destURL item.destURL = destURL
return true
} }
return false
} }
func encode(with aCoder: NSCoder) {} func encode(with aCoder: NSCoder) {}
@@ -58,6 +64,7 @@ import Foundation
/// An action that search in the item source directory for additionals srt subtitles. /// An action that search in the item source directory for additionals srt subtitles.
@objc(SBQueueSubtitlesAction) class QueueSubtitlesAction : NSObject, SBQueueActionProtocol { @objc(SBQueueSubtitlesAction) class QueueSubtitlesAction : NSObject, SBQueueActionProtocol {
var type: SBQueueActionType { return .pre }
var localizedDescription: String { return NSLocalizedString("Loading subtitles", comment: "Action localized description.") } var localizedDescription: String { return NSLocalizedString("Loading subtitles", comment: "Action localized description.") }
override var description: String { return NSLocalizedString("Load Subtitles", comment: "Action description.") } override var description: String { return NSLocalizedString("Load Subtitles", comment: "Action description.") }
@@ -84,7 +91,7 @@ import Foundation
return importers return importers
} }
func runAction(_ item: SBQueueItem) { func runAction(_ item: SBQueueItem) -> Bool {
let subtitlesImporters = loadExternalSubtitles(url: item.fileURL) let subtitlesImporters = loadExternalSubtitles(url: item.fileURL)
for importer in subtitlesImporters { for importer in subtitlesImporters {
@@ -92,6 +99,8 @@ import Foundation
item.mp4File?.addTrack(track) item.mp4File?.addTrack(track)
} }
} }
return subtitlesImporters.isEmpty == false
} }
func encode(with aCoder: NSCoder) {} func encode(with aCoder: NSCoder) {}
@@ -111,11 +120,12 @@ import Foundation
self.preset = preset.copy() as! MetadataPreset self.preset = preset.copy() as! MetadataPreset
} }
var type: SBQueueActionType { return .pre }
var localizedDescription: String { return String.localizedStringWithFormat(NSLocalizedString("Applying %@ preset", comment: "Action localized description."), preset.title) } var localizedDescription: String { return String.localizedStringWithFormat(NSLocalizedString("Applying %@ preset", comment: "Action localized description."), preset.title) }
override var description: String { return String.localizedStringWithFormat(NSLocalizedString("Apply %@ preset", comment: "Action description"), preset.title) } override var description: String { return String.localizedStringWithFormat(NSLocalizedString("Apply %@ preset", comment: "Action description"), preset.title) }
func runAction(_ item: SBQueueItem) { func runAction(_ item: SBQueueItem) -> Bool {
guard let metadata = item.mp4File?.metadata else { return } guard let metadata = item.mp4File?.metadata else { return false }
let dataTypes: UInt = MP42MetadataItemDataType.string.rawValue | MP42MetadataItemDataType.stringArray.rawValue | MP42MetadataItemDataType.bool.rawValue | MP42MetadataItemDataType.integer.rawValue | let dataTypes: UInt = MP42MetadataItemDataType.string.rawValue | MP42MetadataItemDataType.stringArray.rawValue | MP42MetadataItemDataType.bool.rawValue | MP42MetadataItemDataType.integer.rawValue |
MP42MetadataItemDataType.integerArray.rawValue | MP42MetadataItemDataType.date.rawValue MP42MetadataItemDataType.integerArray.rawValue | MP42MetadataItemDataType.date.rawValue
@@ -141,7 +151,8 @@ import Foundation
if artworks.isEmpty == false { if artworks.isEmpty == false {
metadata.addItems(artworks) metadata.addItems(artworks)
} }
return true
} }
func encode(with aCoder: NSCoder) { func encode(with aCoder: NSCoder) {
@@ -190,6 +201,7 @@ extension Array where Element == Artwork {
private let preferredArtwork: ArtworkType private let preferredArtwork: ArtworkType
var type: SBQueueActionType { return .pre }
var localizedDescription: String { return NSLocalizedString("Searching metadata", comment: "Action localized description.") } var localizedDescription: String { return NSLocalizedString("Searching metadata", comment: "Action localized description.") }
override var description: String { return NSLocalizedString("Search Metadata", comment: "Action description.") } override var description: String { return NSLocalizedString("Search Metadata", comment: "Action description.") }
@@ -270,7 +282,7 @@ extension Array where Element == Artwork {
return nil return nil
} }
func runAction(_ item: SBQueueItem) { func runAction(_ item: SBQueueItem) -> Bool {
if let file = item.mp4File { if let file = item.mp4File {
let searchTerms = file.extractSearchTerms(fallbackURL: item.fileURL) let searchTerms = file.extractSearchTerms(fallbackURL: item.fileURL)
if let metadata = searchMetadata(terms: searchTerms) { if let metadata = searchMetadata(terms: searchTerms) {
@@ -286,8 +298,10 @@ extension Array where Element == Artwork {
} }
file.metadata.merge(metadata) file.metadata.merge(metadata)
return true
} }
} }
return false
} }
func encode(with aCoder: NSCoder) { func encode(with aCoder: NSCoder) {
@@ -320,14 +334,16 @@ extension Array where Element == Artwork {
/// An action that organize the item tracks' groups. /// An action that organize the item tracks' groups.
@objc(SBQueueOrganizeGroupsAction) class QueueOrganizeGroupsAction : NSObject, SBQueueActionProtocol { @objc(SBQueueOrganizeGroupsAction) class QueueOrganizeGroupsAction : NSObject, SBQueueActionProtocol {
var type: SBQueueActionType { return .pre }
var localizedDescription: String { return NSLocalizedString("Organizing groups", comment: "Organize Groups action local description") } var localizedDescription: String { return NSLocalizedString("Organizing groups", comment: "Organize Groups action local description") }
override var description: String { return NSLocalizedString("Organize Groups", comment: "Organize Groups action description") } override var description: String { return NSLocalizedString("Organize Groups", comment: "Organize Groups action description") }
@objc override init() {} @objc override init() {}
func runAction(_ item: SBQueueItem) { func runAction(_ item: SBQueueItem) -> Bool {
item.mp4File?.organizeAlternateGroups() item.mp4File?.organizeAlternateGroups()
item.mp4File?.inferMediaCharacteristics() item.mp4File?.inferMediaCharacteristics()
return true
} }
func encode(with aCoder: NSCoder) {} func encode(with aCoder: NSCoder) {}
@@ -341,13 +357,15 @@ extension Array where Element == Artwork {
/// An action that fix the item tracks' fallbacks. /// An action that fix the item tracks' fallbacks.
@objc(SBQueueFixFallbacksAction) class QueueFixFallbacksAction : NSObject, SBQueueActionProtocol { @objc(SBQueueFixFallbacksAction) class QueueFixFallbacksAction : NSObject, SBQueueActionProtocol {
var type: SBQueueActionType { return .pre }
var localizedDescription: String { return NSLocalizedString("Fixing Fallbacks", comment: "Action localized description.") } var localizedDescription: String { return NSLocalizedString("Fixing Fallbacks", comment: "Action localized description.") }
override var description: String { return NSLocalizedString("Fix Fallbacks", comment: "Action description.") } override var description: String { return NSLocalizedString("Fix Fallbacks", comment: "Action description.") }
@objc override init() {} @objc override init() {}
func runAction(_ item: SBQueueItem) { func runAction(_ item: SBQueueItem) -> Bool {
item.mp4File?.setAutoFallback() item.mp4File?.setAutoFallback()
return true
} }
func encode(with aCoder: NSCoder) {} func encode(with aCoder: NSCoder) {}
@@ -361,6 +379,7 @@ extension Array where Element == Artwork {
/// An action that set unknown language tracks to preferred one. /// An action that set unknown language tracks to preferred one.
@objc(SBQueueSetLanguageAction) class QueueSetLanguageAction : NSObject, SBQueueActionProtocol { @objc(SBQueueSetLanguageAction) class QueueSetLanguageAction : NSObject, SBQueueActionProtocol {
var type: SBQueueActionType { return .pre }
var localizedDescription: String { return NSLocalizedString("Setting tracks language", comment: "Set Language action local description") } var localizedDescription: String { return NSLocalizedString("Setting tracks language", comment: "Set Language action local description") }
override var description: String { return NSLocalizedString("Set tracks language", comment: "Set Language action description") } override var description: String { return NSLocalizedString("Set tracks language", comment: "Set Language action description") }
@@ -370,7 +389,7 @@ extension Array where Element == Artwork {
self.language = language self.language = language
} }
func runAction(_ item: SBQueueItem) { func runAction(_ item: SBQueueItem) -> Bool {
if let tracks = item.mp4File?.tracks { if let tracks = item.mp4File?.tracks {
for track in tracks { for track in tracks {
if track.language == "und" { if track.language == "und" {
@@ -378,6 +397,7 @@ extension Array where Element == Artwork {
} }
} }
} }
return true
} }
func encode(with aCoder: NSCoder) { func encode(with aCoder: NSCoder) {
@@ -397,17 +417,19 @@ extension Array where Element == Artwork {
/// An action that remove the tracks names. /// An action that remove the tracks names.
@objc(SBQueueClearTrackNameAction) class QueueClearTrackNameAction : NSObject, SBQueueActionProtocol { @objc(SBQueueClearTrackNameAction) class QueueClearTrackNameAction : NSObject, SBQueueActionProtocol {
var type: SBQueueActionType { return .pre }
var localizedDescription: String { return NSLocalizedString("Clearing tracks names", comment: "Action localized description") } var localizedDescription: String { return NSLocalizedString("Clearing tracks names", comment: "Action localized description") }
override var description: String { return NSLocalizedString("Clear tracks names", comment: "Action description") } override var description: String { return NSLocalizedString("Clear tracks names", comment: "Action description") }
@objc override init() {} @objc override init() {}
func runAction(_ item: SBQueueItem) { func runAction(_ item: SBQueueItem) -> Bool {
if let tracks = item.mp4File?.tracks { if let tracks = item.mp4File?.tracks {
for track in tracks { for track in tracks {
track.name = "" track.name = ""
} }
} }
return true
} }
func encode(with aCoder: NSCoder) {} func encode(with aCoder: NSCoder) {}
@@ -429,6 +451,7 @@ extension Array where Element == Artwork {
/// An action that set the video track color space. /// An action that set the video track color space.
@objc(SBQueueColorSpaceAction) class QueueColorSpaceAction : NSObject, SBQueueActionProtocol { @objc(SBQueueColorSpaceAction) class QueueColorSpaceAction : NSObject, SBQueueActionProtocol {
var type: SBQueueActionType { return .pre }
var localizedDescription: String { return NSLocalizedString("Setting color space", comment: "Set track color space action local description") } var localizedDescription: String { return NSLocalizedString("Setting color space", comment: "Set track color space action local description") }
override var description: String { return NSLocalizedString("Set color space", comment: "Set track color space action description") } override var description: String { return NSLocalizedString("Set color space", comment: "Set track color space action description") }
@@ -461,7 +484,7 @@ extension Array where Element == Artwork {
} }
} }
func runAction(_ item: SBQueueItem) { func runAction(_ item: SBQueueItem) -> Bool {
if let tracks = item.mp4File?.tracks(withMediaType: kMP42MediaType_Video) as? [MP42VideoTrack] { if let tracks = item.mp4File?.tracks(withMediaType: kMP42MediaType_Video) as? [MP42VideoTrack] {
for track in tracks { for track in tracks {
if track.format == kMP42VideoCodecType_H264 || if track.format == kMP42VideoCodecType_H264 ||
@@ -473,7 +496,9 @@ extension Array where Element == Artwork {
track.matrixCoefficients = matrixCoefficients; track.matrixCoefficients = matrixCoefficients;
} }
} }
return true
} }
return false
} }
func encode(with aCoder: NSCoder) { func encode(with aCoder: NSCoder) {
@@ -491,3 +516,20 @@ extension Array where Element == Artwork {
static var supportsSecureCoding: Bool { return true } static var supportsSecureCoding: Bool { return true }
} }
@objc(SBQueueOptimizeAction) class QueueOptimizeAction: NSObject, SBQueueActionProtocol {
var type: SBQueueActionType { return .post }
var localizedDescription: String { return NSLocalizedString("Optimizing", comment: "Action localized description.") }
override var description: String { return NSLocalizedString("Optimize", comment: "Action description.") }
@objc override init() {}
func runAction(_ item: SBQueueItem) -> Bool {
return item.mp4File?.optimize() ?? false
}
func encode(with aCoder: NSCoder) {}
required init?(coder aDecoder: NSCoder) {}
static var supportsSecureCoding: Bool { return true }
}
-1
View File
@@ -27,7 +27,6 @@ extern NSString *SBQueueCancelledNotification;
@interface SBQueue : NSObject @interface SBQueue : NSObject
@property (atomic, readonly) SBQueueStatus status; @property (atomic, readonly) SBQueueStatus status;
@property (atomic) BOOL optimize;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithURL:(NSURL *)queueURL NS_DESIGNATED_INITIALIZER; - (instancetype)initWithURL:(NSURL *)queueURL NS_DESIGNATED_INITIALIZER;
+1 -1
View File
@@ -259,7 +259,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification";
self.currentItem.delegate = self; self.currentItem.delegate = self;
[self handleSBStatusWorking:0 index:self.currentIndex]; [self handleSBStatusWorking:0 index:self.currentIndex];
noErr = [self.currentItem processWithOptions:self.optimize error:&outError]; noErr = [self.currentItem process:&outError];
// Check results // Check results
if (self.cancelled) { if (self.cancelled) {
+4 -13
View File
@@ -80,9 +80,6 @@ static void *SBQueueContex = &SBQueueContex;
// Drag & Drop // Drag & Drop
[self.table registerForDraggedTypes:@[NSFilenamesPboardType, SublerBatchTableViewDataType]]; [self.table registerForDraggedTypes:@[NSFilenamesPboardType, SublerBatchTableViewDataType]];
// Observe the changes to SBQueueOptimize
[self addObserver:self forKeyPath:@"options.SBQueueOptimize" options:NSKeyValueObservingOptionInitial context:SBQueueContex];
// Register to the queue notifications // Register to the queue notifications
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
@@ -174,16 +171,6 @@ static void *SBQueueContex = &SBQueueContex;
return YES; return YES;
} }
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == SBQueueContex) {
if ([keyPath isEqualToString:@"options.SBQueueOptimize"]) {
self.queue.optimize = [(self.options)[SBQueueOptimize] boolValue];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark - Queue methods #pragma mark - Queue methods
/** /**
@@ -294,6 +281,10 @@ static void *SBQueueContex = &SBQueueContex;
[item addAction:[[SBQueueSetAction alloc] initWithPreset:self.options[SBQueueSet]]]; [item addAction:[[SBQueueSetAction alloc] initWithPreset:self.options[SBQueueSet]]];
} }
if ([self.options[SBQueueOptimize] boolValue]) {
[item addAction:[[SBQueueOptimizeAction alloc] init]];
}
id type; id type;
[url getResourceValue:&type forKey:NSURLTypeIdentifierKey error:NULL]; [url getResourceValue:&type forKey:NSURLTypeIdentifierKey error:NULL];
+8 -2
View File
@@ -13,13 +13,19 @@ NS_ASSUME_NONNULL_BEGIN
@class MP42File; @class MP42File;
@class SBQueueItem; @class SBQueueItem;
typedef NS_ENUM(NSUInteger, SBQueueActionType) {
SBQueueActionTypePre = 0,
SBQueueActionTypePost
};
/** /**
* SBQueue actions protocol, actions can be run by * SBQueue actions protocol, actions can be run by
* the queue's items. * the queue's items.
*/ */
@protocol SBQueueActionProtocol <NSObject, NSSecureCoding> @protocol SBQueueActionProtocol <NSObject, NSSecureCoding>
- (void)runAction:(SBQueueItem *)item; - (BOOL)runAction:(SBQueueItem *)item;
@property (nonatomic, readonly) NSString *localizedDescription; @property (nonatomic, readonly) NSString *localizedDescription;
@property (nonatomic, readonly) SBQueueActionType type;
@end @end
@@ -57,7 +63,7 @@ typedef NS_ENUM(NSUInteger, SBQueueItemStatus) {
- (void)removeAction:(id<SBQueueActionProtocol>)action; - (void)removeAction:(id<SBQueueActionProtocol>)action;
- (BOOL)prepare:(NSError * __autoreleasing *)outError; - (BOOL)prepare:(NSError * __autoreleasing *)outError;
- (BOOL)processWithOptions:(BOOL)optimize error:(NSError * __autoreleasing *)outError; - (BOOL)process:(NSError * __autoreleasing *)outError;
- (void)cancel; - (void)cancel;
@end @end
+15 -8
View File
@@ -310,9 +310,11 @@
} }
for (id<SBQueueActionProtocol> action in self.actions) { for (id<SBQueueActionProtocol> action in self.actions) {
self.localizedWorkingDescription = action.localizedDescription; if (action.type == SBQueueActionTypePre) {
[self progressStatus:0]; self.localizedWorkingDescription = action.localizedDescription;
[action runAction:self]; [self progressStatus:0];
[action runAction:self];
}
} }
self.localizedWorkingDescription = nil; self.localizedWorkingDescription = nil;
@@ -320,7 +322,7 @@
return YES; return YES;
} }
- (BOOL)processWithOptions:(BOOL)optimize error:(NSError * __autoreleasing *)outError { - (BOOL)process:(NSError * __autoreleasing *)outError {
BOOL noErr = YES; BOOL noErr = YES;
#ifdef SB_SANDBOX #ifdef SB_SANDBOX
@@ -376,9 +378,14 @@
{ {
// Optimize the file // Optimize the file
if (!self.cancelled && optimize) { if (!self.cancelled /*&& optimize*/) {
self.localizedWorkingDescription = NSLocalizedString(@"Optimizing", @""); for (id<SBQueueActionProtocol> action in self.actions) {
noErr = [self.mp4File optimize]; if (action.type == SBQueueActionTypePost) {
self.localizedWorkingDescription = action.localizedDescription;
[self progressStatus:0];
noErr = [action runAction:self];
}
}
self.localizedWorkingDescription = nil; self.localizedWorkingDescription = nil;
} }
@@ -460,7 +467,7 @@ bail:
[SBQueueSetLanguageAction class], [SBQueueFixFallbacksAction class], [SBQueueSetLanguageAction class], [SBQueueFixFallbacksAction class],
[SBQueueClearTrackNameAction class], [SBQueueOrganizeGroupsAction class], [SBQueueClearTrackNameAction class], [SBQueueOrganizeGroupsAction class],
[SBQueueColorSpaceAction class], [SBQueueSetOutputFilenameAction class], [SBQueueColorSpaceAction class], [SBQueueSetOutputFilenameAction class],
[SBQueueClearExistingMetadataAction class], nil] [SBQueueClearExistingMetadataAction class], [SBQueueOptimizeAction class], nil]
forKey:@"SBQueueItemActions"]; forKey:@"SBQueueItemActions"];
_status = [decoder decodeIntForKey:@"SBQueueItemStatus"]; _status = [decoder decodeIntForKey:@"SBQueueItemStatus"];