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