Retag queued Matroska files in place instead of converting to MP4
The queue now mirrors the document window for .mkv/.mka/.mks sources. The window's whole save branching (importer-order track snapshot, chapter row snapshot, staleness guard, tag-writer-vs-remuxer choice) moved out of Document into a shared MatroskaSaver used by both Document and QueueItem so the two paths cannot drift. Queue items import tags-only, keep the Matroska extension (in place, or stream-copy into a destination folder), MP4-only actions no-op on them, and send-to-queue hands the item the document's model plus its identity pairing baseline via attachMatroskaSaver. Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
1 parent
a1da1c8106
commit
80ca3970ec
7 files changed
+442
-195
No files matched your search
+116
-23
@@ -41,6 +41,26 @@ import MP42Foundation
|
||||
var mp4File: MP42File?
|
||||
var localizedWorkingDescription: String?
|
||||
|
||||
// Matroska items mirror the window's save: a tags-only model whose
|
||||
// metadata is written back into the original file (never muxed to MP4).
|
||||
// The pairing baseline (which importer tracks back which TrackEntry
|
||||
// elements, chapter rows, file size/mtime) lives in the saver; it's
|
||||
// transient — a resumed queue re-imports the file instead.
|
||||
private var matroskaSaver: MatroskaSaver?
|
||||
/// An item is Matroska iff its source file is one: always true for both
|
||||
/// entry paths (a queue file chosen by extension, a document's source),
|
||||
/// and derived (never archived) so prepare() and process() can't disagree.
|
||||
var isMatroska: Bool {
|
||||
return MP42FileImporter.isMatroskaFileType(fileURL.pathExtension)
|
||||
}
|
||||
|
||||
/// A document being sent to the queue hands its Matroska baseline to the
|
||||
/// item: the pairing is by object identity, so it travels with the exact
|
||||
/// model the document was editing.
|
||||
func attachMatroskaSaver(_ saver: MatroskaSaver) {
|
||||
matroskaSaver = saver
|
||||
}
|
||||
|
||||
weak var delegate: Queue?
|
||||
|
||||
private var statusInternal: Status
|
||||
@@ -107,6 +127,21 @@ import MP42Foundation
|
||||
}
|
||||
}
|
||||
|
||||
/// Document handoff (send to queue / AppleScript) that must keep the
|
||||
/// original file as the source: unlike init(mp4:destURL:...) the
|
||||
/// destination may differ from the source — a Matroska item writes back
|
||||
/// into the source in place, or stream-copy remuxes to a different
|
||||
/// destination (plus the baseline from attachMatroskaSaver).
|
||||
convenience init(mp4: MP42File, fileURL: URL, destURL: URL, attributes: [String : Any] = [:], optimize: Bool = false) {
|
||||
self.init(fileURL: fileURL, destURL: destURL)
|
||||
self.mp4File = mp4
|
||||
self.attributes = attributes
|
||||
|
||||
if optimize {
|
||||
addAction(QueueOptimizeAction())
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Public properties
|
||||
|
||||
@objc dynamic var status: Status {
|
||||
@@ -253,28 +288,19 @@ import MP42Foundation
|
||||
#endif
|
||||
|
||||
if mp4File == nil {
|
||||
let value = try? fileURL.resourceValues(forKeys: [URLResourceKey.typeIdentifierKey])
|
||||
|
||||
if let type = value?.typeIdentifier, type == "com.apple.m4a-audio" || type == "com.apple.m4v-video" || type == "public.mpeg-4" {
|
||||
mp4File = try MP42File(url: fileURL)
|
||||
} else {
|
||||
let mp4 = MP42File()
|
||||
let importer = try MP42FileImporter(url: fileURL)
|
||||
let activeTracks = importer.tracks.filter { isTrackMuxable($0.format) || trackNeedConversion($0.format) }
|
||||
|
||||
mp4.metadata.merge(importer.metadata, overwrite: true)
|
||||
|
||||
for track in activeTracks {
|
||||
|
||||
if let track = track as? MP42AudioTrack {
|
||||
configureAudio(track: track, mp4: mp4)
|
||||
} else if let track = track as? MP42SubtitleTrack {
|
||||
configureSubtitles(track: track, mp4: mp4)
|
||||
}
|
||||
|
||||
mp4.addTrack(track)
|
||||
}
|
||||
if isMatroska {
|
||||
// Match the window's MKV open (Document.readMatroska): the
|
||||
// tags-only model gets retagged into the original file at
|
||||
// write time instead of being converted to MP4. The saver
|
||||
// snapshots the pairing/staleness baseline from this import;
|
||||
// a handoff item carries its model (and attached saver) and
|
||||
// never gets here.
|
||||
let importer = try MP42FileImporter(url: fileURL, tagsOnly: true)
|
||||
let mp4 = MatroskaSaver.buildModel(from: importer)
|
||||
matroskaSaver = MatroskaSaver(sourceURL: fileURL, importer: importer, model: mp4)
|
||||
mp4File = mp4
|
||||
} else {
|
||||
try importForConversion()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,6 +311,32 @@ import MP42Foundation
|
||||
}
|
||||
}
|
||||
|
||||
private func importForConversion() throws {
|
||||
let value = try? fileURL.resourceValues(forKeys: [URLResourceKey.typeIdentifierKey])
|
||||
|
||||
if let type = value?.typeIdentifier, type == "com.apple.m4a-audio" || type == "com.apple.m4v-video" || type == "public.mpeg-4" {
|
||||
mp4File = try MP42File(url: fileURL)
|
||||
} else {
|
||||
let mp4 = MP42File()
|
||||
let importer = try MP42FileImporter(url: fileURL)
|
||||
let activeTracks = importer.tracks.filter { isTrackMuxable($0.format) || trackNeedConversion($0.format) }
|
||||
|
||||
mp4.metadata.merge(importer.metadata, overwrite: true)
|
||||
|
||||
for track in activeTracks {
|
||||
|
||||
if let track = track as? MP42AudioTrack {
|
||||
configureAudio(track: track, mp4: mp4)
|
||||
} else if let track = track as? MP42SubtitleTrack {
|
||||
configureSubtitles(track: track, mp4: mp4)
|
||||
}
|
||||
|
||||
mp4.addTrack(track)
|
||||
}
|
||||
mp4File = mp4
|
||||
}
|
||||
}
|
||||
|
||||
func process() throws {
|
||||
|
||||
guard let filePathURL = (fileURL as NSURL?)?.filePathURL else { return }
|
||||
@@ -296,6 +348,7 @@ import MP42Foundation
|
||||
defer {
|
||||
mp4File?.progressHandler = nil
|
||||
mp4File = nil
|
||||
matroskaSaver = nil
|
||||
}
|
||||
|
||||
// The file has been added directly to the queue
|
||||
@@ -310,6 +363,9 @@ import MP42Foundation
|
||||
#endif
|
||||
|
||||
mp4.progressHandler = { [weak self] in self?.delegate?.updateProgress($0) }
|
||||
matroskaSaver?.progressHandler = { [weak self] progress in
|
||||
self?.delegate?.updateProgress(progress)
|
||||
}
|
||||
|
||||
// Check if there is enough space on the destination disk
|
||||
if filePathURL != destURL {
|
||||
@@ -320,7 +376,24 @@ import MP42Foundation
|
||||
}
|
||||
|
||||
if cancelled == false {
|
||||
if filePathURL == destURL && mp4.hasFileRepresentation {
|
||||
if isMatroska {
|
||||
// Match the window's Matroska save: write metadata and the
|
||||
// four per-track fields back into the container instead of
|
||||
// muxing an MP4. Added subtitle tracks (QueueSubtitlesAction)
|
||||
// and edited chapters ride the stream-copy remuxer, as do a
|
||||
// renamed/moved destination (set-output-filename action,
|
||||
// queue destination folder) and optimize — the queue's
|
||||
// equivalent of the window's save-and-optimize full rebuild.
|
||||
// The branching is exactly Document.writeMatroska's, down to
|
||||
// in-place temp file + atomic rename.
|
||||
guard let saver = matroskaSaver else {
|
||||
throw NSError(domain: NSCocoaErrorDomain, code: NSFileWriteUnknownError,
|
||||
userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("The Matroska source file is unknown.", comment: "Document MKV remux missing source error.")])
|
||||
}
|
||||
let optimizeRequested = actions.contains { $0 is QueueOptimizeAction }
|
||||
try saver.save(model: mp4, to: destURL,
|
||||
forceStructural: optimizeRequested || destURL != fileURL)
|
||||
} else if filePathURL == destURL && mp4.hasFileRepresentation {
|
||||
// We have an existing mp4 file, update it
|
||||
try mp4.update(options: attributes)
|
||||
} else {
|
||||
@@ -345,6 +418,7 @@ import MP42Foundation
|
||||
func cancel() {
|
||||
queue.sync {
|
||||
self.cancelled = true
|
||||
matroskaSaver?.cancel()
|
||||
mp4File?.cancel()
|
||||
}
|
||||
}
|
||||
@@ -378,7 +452,12 @@ import MP42Foundation
|
||||
aCoder.encode(5, forKey: "SBQueueItemTagEncodeVersion")
|
||||
|
||||
aCoder.encode(uniqueID, forKey: "SBQueueItemID")
|
||||
aCoder.encode(mp4File, forKey: "SBQueueItemMp4File")
|
||||
// A tags-only Matroska model comes back by re-importing the file —
|
||||
// its TrackEntry pairing is object identity and cannot be archived,
|
||||
// and a restored MP4 write branch could never save it anyway.
|
||||
if isMatroska == false {
|
||||
aCoder.encode(mp4File, forKey: "SBQueueItemMp4File")
|
||||
}
|
||||
|
||||
#if SB_SANDBOX
|
||||
|
||||
@@ -468,6 +547,20 @@ import MP42Foundation
|
||||
QueueSetOutputFilenameAction.classForCoder(), QueueClearExistingMetadataAction.classForCoder(),
|
||||
QueueOptimizeAction.classForCoder(), QueueSendToiTunesAction.classForCoder(),
|
||||
QueueChangeAudioLanguageAction.classForCoder(), QueueChangeSubtitleLanguageAction.classForCoder()], forKey: "SBQueueItemActions") as? [QueueActionProtocol] ?? []
|
||||
|
||||
// Matroska items always rebuild their tags-only model on resume
|
||||
// (prepare() re-imports — pre-actions re-run against it, so the
|
||||
// filename action re-derives the name too): the identity pairing
|
||||
// cannot survive archiving. Older archives may also carry the
|
||||
// retired convert-to-mp4 destination — point it back at the source
|
||||
// for the in-place retag.
|
||||
// (stored-property access only: super.init hasn't run yet)
|
||||
if MP42FileImporter.isMatroskaFileType(fileURL.pathExtension) {
|
||||
mp4File = nil
|
||||
if MP42FileImporter.isMatroskaFileType(destURLInternal.pathExtension) == false {
|
||||
destURLInternal = fileURL
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user