diff --git a/Classes/Document+Swift.swift b/Classes/Document+Swift.swift index 04477a7..1f1e75f 100644 --- a/Classes/Document+Swift.swift +++ b/Classes/Document+Swift.swift @@ -59,8 +59,10 @@ extension SBDocument: ChapterSearchControllerDelegate, MetadataSearchControllerD let map = metadata.mediaKind == 9 ? defaults.map(forKey: "SBMetadataMovieResultMap") : defaults.map(forKey: "SBMetadataTvShowResultMap") let keepEmptyKeys = defaults.bool(forKey: "SBMetadataKeepEmptyAnnotations") - let result = metadata.mappedMetadata(to: map!, keepEmptyKeys: keepEmptyKeys) - mp4.metadata.merge(result) + if let map = map { + let result = metadata.mappedMetadata(to: map, keepEmptyKeys: keepEmptyKeys) + mp4.metadata.merge(result) + } if let hdType = mp4.hdType { for item in mp4.metadata.metadataItemsFiltered(byIdentifier: MP42MetadataKeyHDVideo) { @@ -118,7 +120,6 @@ extension SBDocument: ChapterSearchControllerDelegate, MetadataSearchControllerD func didSelect(tracks: [MP42Track], metadata: MP42Metadata?) { for track in tracks { mp4.addTrack(track) - } if tracks.isEmpty == false { diff --git a/Classes/QueueAction.swift b/Classes/QueueAction.swift index 9f686a6..19a5456 100644 --- a/Classes/QueueAction.swift +++ b/Classes/QueueAction.swift @@ -7,7 +7,59 @@ import Foundation -/// An actions that applies a preset to the item. +/// SBQueue actions protocol, actions can be run by the queue's items. +//@objc(SBQueueActionProtocol) protocol QueueActionProtocol: NSSecureCoding { +// @objc func runAction(_ item: SBQueueItem) +// @objc var localizedDescription: String { get } +//} + +/// An action that search in the item source directory for additionals srt subtitles. +@objc(SBQueueSubtitlesAction) class QueueSubtitlesAction : NSObject, SBQueueActionProtocol { + + var localizedDescription: String { return NSLocalizedString("Loading subtitles", comment: "Action localized description.") } + override var description: String { return NSLocalizedString("Load Subtitles", comment: "Action description.") } + + @objc override init() {} + + private func loadExternalSubtitles(url: URL) -> [MP42FileImporter] { + let movieFilename = url.deletingPathExtension().lastPathComponent + var importers: [MP42FileImporter] = Array() + if let contents = try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [.skipsSubdirectoryDescendants, .skipsHiddenFiles, .skipsPackageDescendants]) { + for url in contents { + if url.pathExtension.caseInsensitiveCompare("srt") == ComparisonResult.orderedSame { + let subtitleFilename = url.deletingPathExtension().lastPathComponent + if movieFilename.count < subtitleFilename.count && + subtitleFilename.compare(subtitleFilename, options: String.CompareOptions.caseInsensitive, range: movieFilename.startIndex.. - -@class SBQueueItem; -@class MP42Metadata; - -NS_ASSUME_NONNULL_BEGIN - -/** - * SBQueue actions protocol, actions can be run by - * the queue's items. - */ -@protocol SBQueueActionProtocol -- (void)runAction:(SBQueueItem *)item; -@property (nonatomic, readonly) NSString *localizedDescription; -@end - -/** - * An actions that search in the item source directory for additionals srt subtitles - */ -@interface SBQueueSubtitlesAction : NSObject -@end - -/** - * An actions that organize the item tracks' groups. - */ -@interface SBQueueOrganizeGroupsAction : NSObject -@end - -/** - * An actions that fix the item tracks' fallbacks. - */ -@interface SBQueueFixFallbacksAction : NSObject -@end - -/** - * An actions that remove the tracks names. - */ -@interface SBQueueClearTrackNameAction : NSObject -@end - -/** - * An actions that set unknown language tracks to preferred one. - */ -@interface SBQueueSetLanguageAction : NSObject -- (instancetype)initWithLanguage:(NSString *)language; -@end - -/** - * An actions that set the video track color space. - */ - -typedef NS_ENUM(NSUInteger, SBQueueColorSpaceActionTag) { - SBQueueColorSpaceActionTagNone = 1, - SBQueueColorSpaceActionTagRec601PAL, - SBQueueColorSpaceActionTagRec601SMPTEC, - SBQueueColorSpaceActionTagRec709, - SBQueueColorSpaceActionTagRec2020 - }; -@interface SBQueueColorSpaceAction : NSObject -- (instancetype)initWithTag:(uint16_t)tag; -- (instancetype)initWithColorPrimaries:(uint16_t)colorPrimaries transferCharacteristics:(uint16_t)transferCharacteristics matrixCoefficients:(uint16_t)matrixCoefficients; -@end - -NS_ASSUME_NONNULL_END diff --git a/Classes/SBQueueAction.m b/Classes/SBQueueAction.m deleted file mode 100644 index fc27e68..0000000 --- a/Classes/SBQueueAction.m +++ /dev/null @@ -1,325 +0,0 @@ -// -// SBQueueAction.m -// Subler -// -// Created by Damiano Galassi on 12/03/14. -// -// - -#import "SBQueueAction.h" - -#import "SBQueueItem.h" - -#import -#import -#import -#import - -@implementation SBQueueSubtitlesAction - -/** - * Loads the subtitles in the parent directory - */ -- (NSArray *)loadSubtitles:(NSURL *)url { - NSError *error = nil; - NSMutableArray *importersArray = [[NSMutableArray alloc] init]; - NSArray *directory = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:url.URLByDeletingLastPathComponent - includingPropertiesForKeys:nil - options:NSDirectoryEnumerationSkipsSubdirectoryDescendants | - NSDirectoryEnumerationSkipsHiddenFiles | - NSDirectoryEnumerationSkipsPackageDescendants - error:nil]; - - for (NSURL *dirUrl in directory) { - if ([dirUrl.pathExtension caseInsensitiveCompare:@"srt"] == NSOrderedSame) { - NSComparisonResult result; - NSString *movieFilename = url.URLByDeletingPathExtension.lastPathComponent; - NSString *subtitleFilename = dirUrl.URLByDeletingPathExtension.lastPathComponent; - NSRange range = { 0, movieFilename.length }; - - if (movieFilename.length <= subtitleFilename.length) { - result = [subtitleFilename compare:movieFilename options:NSCaseInsensitiveSearch range:range]; - - if (result == NSOrderedSame) { - MP42FileImporter *fileImporter = [[MP42FileImporter alloc] initWithURL:dirUrl - error:&error]; - if (fileImporter) { - [importersArray addObject:fileImporter]; - } - } - } - } - } - - return importersArray; -} - -- (void)runAction:(SBQueueItem *)item { - // Search for external subtitles files - NSArray *subtitles = [self loadSubtitles:item.fileURL]; - for (MP42FileImporter *fileImporter in subtitles) { - for (MP42SubtitleTrack *subTrack in fileImporter.tracks) { - [item.mp4File addTrack:subTrack]; - } - } -} - -- (NSString *)description { - return NSLocalizedString(@"Load Subtitles", @"Action description."); -} - -- (NSString *)localizedDescription { - return NSLocalizedString(@"Loading subtitles", @"Action localized description."); -} - -+ (BOOL)supportsSecureCoding { - return YES; -} - -- (instancetype)initWithCoder:(NSCoder *)coder { - self = [super init]; - return self; -} - -- (void)encodeWithCoder:(NSCoder *)coder { -} - -@end - -@implementation SBQueueOrganizeGroupsAction - -- (void)runAction:(SBQueueItem *)item { - [item.mp4File organizeAlternateGroups]; - [item.mp4File inferMediaCharacteristics]; -} - -- (NSString *)description { - return NSLocalizedString(@"Organize Groups", @"Organize Groups action description"); -} - -- (NSString *)localizedDescription { - return NSLocalizedString(@"Organizing groups", @"Organize Groups action local description"); -} - -+ (BOOL)supportsSecureCoding { - return YES; -} - -- (instancetype)initWithCoder:(NSCoder *)coder { - self = [super init]; - return self; -} - -- (void)encodeWithCoder:(NSCoder *)coder {} - -@end - -/** - * An actions that fix the item tracks' fallbacks. - */ -@implementation SBQueueFixFallbacksAction - -- (void)runAction:(SBQueueItem *)item { - [item.mp4File setAutoFallback]; -} - -- (NSString *)description { - return NSLocalizedString(@"Fix Fallbacks", @"Action description."); -} - -- (NSString *)localizedDescription { - return NSLocalizedString(@"Fixing Fallbacks", @"Action localized description."); -} - -+ (BOOL)supportsSecureCoding { - return YES; -} - -- (instancetype)initWithCoder:(NSCoder *)coder { - self = [super init]; - return self; -} - -- (void)encodeWithCoder:(NSCoder *)coder {} - -@end - -/** - * An actions that remove the tracks names. - */ -@implementation SBQueueClearTrackNameAction - -- (void)runAction:(SBQueueItem *)item { - for (MP42Track *track in item.mp4File.tracks) { - track.name = @""; - } -} - -- (NSString *)description { - return NSLocalizedString(@"Clear tracks names", @"Action description."); -} - -- (NSString *)localizedDescription { - return NSLocalizedString(@"Clearing tracks names", @"Action localized description."); -} - -+ (BOOL)supportsSecureCoding { - return YES; -} - -- (instancetype)initWithCoder:(NSCoder *)coder { - self = [super init]; - return self; -} - -- (void)encodeWithCoder:(NSCoder *)coder {} - -@end - - -/** - * An actions that set unknown language tracks to preferred one. - */ -@interface SBQueueSetLanguageAction () -@property (nonatomic, readonly) NSString *language; -@end - -@implementation SBQueueSetLanguageAction - -- (instancetype)initWithLanguage:(NSString *)language { - self = [super init]; - if (self) { - _language = [language copy]; - } - return self; -} - -- (void)runAction:(SBQueueItem *)item { - for (MP42Track *track in item.mp4File.tracks) { - if ([track.language isEqualToString:@"und"]) { - track.language = self.language; - } - } -} - -- (NSString *)description { - return NSLocalizedString(@"Set tracks language", @"Set Language action description"); -} - -- (NSString *)localizedDescription { - return NSLocalizedString(@"Setting tracks language", @"Set Language action local description"); -} - -+ (BOOL)supportsSecureCoding { - return YES; -} - -- (instancetype)initWithCoder:(NSCoder *)coder { - self = [super init]; - _language = [coder decodeObjectOfClass:[NSString class] forKey:@"SBQueueSetLanguageAction"]; - return self; -} - -- (void)encodeWithCoder:(NSCoder *)coder { - [coder encodeObject:self.language forKey:@"SBQueueSetLanguageAction"]; -} - -@end - -@interface SBQueueColorSpaceAction () -@property(nonatomic, readonly) uint16_t colorPrimaries; -@property(nonatomic, readonly) uint16_t transferCharacteristics; -@property(nonatomic, readonly) uint16_t matrixCoefficients; -@end - -@implementation SBQueueColorSpaceAction - -- (instancetype)initWithTag:(uint16_t)tag { - self = [super init]; - if (self) { - switch (tag) { - case SBQueueColorSpaceActionTagRec601PAL: - _colorPrimaries = 5; - _transferCharacteristics = 1; - _matrixCoefficients = 6; - break; - - case SBQueueColorSpaceActionTagRec601SMPTEC: - _colorPrimaries = 6; - _transferCharacteristics = 1; - _matrixCoefficients = 6; - break; - - case SBQueueColorSpaceActionTagRec709: - _colorPrimaries = 1; - _transferCharacteristics = 1; - _matrixCoefficients = 1; - break; - - case SBQueueColorSpaceActionTagRec2020: - _colorPrimaries = 9; - _transferCharacteristics = 1; - _matrixCoefficients = 9; - break; - - case SBQueueColorSpaceActionTagNone: - default: - _colorPrimaries = 0; - _transferCharacteristics = 0; - _matrixCoefficients = 0; - break; - } - } - return self; -} - -- (instancetype)initWithColorPrimaries:(uint16_t)colorPrimaries transferCharacteristics:(uint16_t)transferCharacteristics matrixCoefficients:(uint16_t)matrixCoefficients { - self = [super init]; - if (self) { - _colorPrimaries = colorPrimaries; - _transferCharacteristics = transferCharacteristics; - _matrixCoefficients = matrixCoefficients; - } - return self; -} - -- (void)runAction:(SBQueueItem *)item { - for (MP42VideoTrack *track in [item.mp4File tracksWithMediaType:kMP42MediaType_Video]) { - if (track.format == kMP42VideoCodecType_H264 || - track.format == kMP42VideoCodecType_HEVC || - track.format == kMP42VideoCodecType_HEVC_PSinBitstream || - track.format == kMP42VideoCodecType_MPEG4Video) { - track.colorPrimaries = self.colorPrimaries; - track.transferCharacteristics = self.transferCharacteristics; - track.matrixCoefficients = self.matrixCoefficients; - } - } -} - -- (NSString *)description { - return NSLocalizedString(@"Set color space", @"Set track color space action description"); -} - -- (NSString *)localizedDescription { - return NSLocalizedString(@"Setting color space", @"Set track color space action local description"); -} - -+ (BOOL)supportsSecureCoding { - return YES; -} - -- (instancetype)initWithCoder:(NSCoder *)coder { - self = [super init]; - _colorPrimaries = [coder decodeInt32ForKey:@"SBQueueColorSpaceActionColorPrimaries"]; - _transferCharacteristics = [coder decodeInt32ForKey:@"SBQueueColorSpaceActionTransferCharacteristics"]; - _matrixCoefficients = [coder decodeInt32ForKey:@"SBQueueColorSpaceActionMatrixCoefficients"]; - return self; -} - -- (void)encodeWithCoder:(NSCoder *)coder { - [coder encodeInt32:self.colorPrimaries forKey:@"SBQueueColorSpaceActionColorPrimaries"]; - [coder encodeInt32:self.transferCharacteristics forKey:@"SBQueueColorSpaceActionTransferCharacteristics"]; - [coder encodeInt32:self.matrixCoefficients forKey:@"SBQueueColorSpaceActionMatrixCoefficients"]; -} - -@end diff --git a/Classes/SBQueueItem.h b/Classes/SBQueueItem.h index cbc55eb..670eab4 100755 --- a/Classes/SBQueueItem.h +++ b/Classes/SBQueueItem.h @@ -7,12 +7,22 @@ // #import -#import "SBQueueAction.h" - -@class MP42File; NS_ASSUME_NONNULL_BEGIN +@class MP42File; +@class SBQueueItem; + +/** + * SBQueue actions protocol, actions can be run by + * the queue's items. + */ +@protocol SBQueueActionProtocol +- (void)runAction:(SBQueueItem *)item; +@property (nonatomic, readonly) NSString *localizedDescription; +@end + + typedef NS_ENUM(NSUInteger, SBQueueItemStatus) { SBQueueItemStatusUnknown = 0, SBQueueItemStatusReady, diff --git a/Classes/SBQueueItem.m b/Classes/SBQueueItem.m index c076016..c5e0b88 100755 --- a/Classes/SBQueueItem.m +++ b/Classes/SBQueueItem.m @@ -7,7 +7,6 @@ // #import "SBQueueItem.h" -#import "SBQueueAction.h" #import #import diff --git a/Subler Tests/Subler_Tests.m b/Subler Tests/Subler_Tests.m index 2df9a85..2326f87 100644 --- a/Subler Tests/Subler_Tests.m +++ b/Subler Tests/Subler_Tests.m @@ -9,8 +9,6 @@ #import #import -#import "SBQueueAction.h" - @interface Subler_Tests : XCTestCase @end diff --git a/Subler-Bridging-Header.h b/Subler-Bridging-Header.h index 049394a..235c0dc 100644 --- a/Subler-Bridging-Header.h +++ b/Subler-Bridging-Header.h @@ -4,7 +4,6 @@ #import "SBDocument.h" #import "SBQueueItem.h" -#import "SBQueueAction.h" #import "SBMovieViewController.h" #import "SBMediaTagsController.h" diff --git a/Subler.xcodeproj/project.pbxproj b/Subler.xcodeproj/project.pbxproj index 75981c7..ad6dc8e 100644 --- a/Subler.xcodeproj/project.pbxproj +++ b/Subler.xcodeproj/project.pbxproj @@ -81,7 +81,6 @@ A9AB486817F0AEFD000AA72E /* VideoView.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9AB486217F0AEFD000AA72E /* VideoView.xib */; }; A9AE26C01F3B66470041A57D /* SquaredTVArt.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9AE26BD1F3B66470041A57D /* SquaredTVArt.swift */; }; A9AE70E11F2F7B3D001CF27C /* MetadataHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9AE70E01F2F7B3D001CF27C /* MetadataHelper.swift */; }; - A9AF6A7518D0496400FAAB08 /* SBQueueAction.m in Sources */ = {isa = PBXBuildFile; fileRef = A9AF6A7418D0496400FAAB08 /* SBQueueAction.m */; }; A9B46AA412B79BB10040517A /* Subler.icns in Resources */ = {isa = PBXBuildFile; fileRef = A9B46AA312B79BB10040517A /* Subler.icns */; }; A9B46ABF12B79D1C0040517A /* SBDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = A9B46ABE12B79D1C0040517A /* SBDocument.m */; }; A9B514191EFC12310055A035 /* TheTVDB.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9B514181EFC12310055A035 /* TheTVDB.swift */; }; @@ -268,8 +267,6 @@ A9AB485317F0AEAC000AA72E /* SBVideoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBVideoViewController.m; path = Classes/PropertyView/SBVideoViewController.m; sourceTree = ""; }; A9AE26BD1F3B66470041A57D /* SquaredTVArt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SquaredTVArt.swift; path = Classes/MetadataImporters/SquaredTVArt.swift; sourceTree = ""; }; A9AE70E01F2F7B3D001CF27C /* MetadataHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = MetadataHelper.swift; path = Classes/MetadataImporters/MetadataHelper.swift; sourceTree = ""; }; - A9AF6A7318D0496400FAAB08 /* SBQueueAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBQueueAction.h; path = Classes/SBQueueAction.h; sourceTree = ""; }; - A9AF6A7418D0496400FAAB08 /* SBQueueAction.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBQueueAction.m; path = Classes/SBQueueAction.m; sourceTree = ""; }; A9B3411B1679CA3E00E6EE0C /* QTKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QTKit.framework; path = System/Library/Frameworks/QTKit.framework; sourceTree = SDKROOT; }; A9B46AA312B79BB10040517A /* Subler.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = Subler.icns; path = images/Subler.icns; sourceTree = ""; }; A9B46ABD12B79D1C0040517A /* SBDocument.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBDocument.h; path = Classes/SBDocument.h; sourceTree = ""; }; @@ -564,8 +561,6 @@ A9D73C5118BF3F1100C6FFA6 /* SBQueue.m */, A982D0BE140AB66400B92357 /* SBQueueItem.h */, A982D0BF140AB66400B92357 /* SBQueueItem.m */, - A9AF6A7318D0496400FAAB08 /* SBQueueAction.h */, - A9AF6A7418D0496400FAAB08 /* SBQueueAction.m */, A91B196B1F2B0DCB0008675F /* QueueAction.swift */, A9A8CEF71E43754400BC2AE1 /* SBQueueScriptCommand.h */, A9A8CEF81E43754400BC2AE1 /* SBQueueScriptCommand.m */, @@ -856,7 +851,6 @@ A961476313F567E10068F161 /* SBQueueController.m in Sources */, A982D0C0140AB66400B92357 /* SBQueueItem.m in Sources */, A90057C01F99E35300FA4ED1 /* PopUpCellView.swift in Sources */, - A9AF6A7518D0496400FAAB08 /* SBQueueAction.m in Sources */, A91B196C1F2B0DCB0008675F /* QueueAction.swift in Sources */, A956CE651F99E5BB00093992 /* ActivityWindowController.swift in Sources */, A99189D11F2F88F700623ED7 /* ChapterSearchController.swift in Sources */,