如何更改CreationDate资源值

时间:2017-08-21 09:20:42

标签: ios swift swift3 avfoundation nsfilemanager

我正在编写一个录制应用程序,使用户可以修剪以前录制的部分内容,并将它们连接成一个新的录制内容。

我的问题是:让我说我录制了一小时长的音轨,我想修剪该音轨的前2分钟。当我导出这2分钟时,此曲目的创建日期将是"现在",我需要它来匹配这2分钟实际发生的日期。

所以基本上我试图修改跟踪Url资源值,但我想只更改创建日期。

有办法做到这一点吗?或者有没有办法添加新的资源价值键?或者将所需日期附加到网址的方法?

func trimStatringPoint(_ from: Date, startOffSet: TimeInterval, duration: TimeInterval, fileName: String, file: URL, completion: fileExportaionBlock?) {

    if let asset = AVURLAsset(url: file) as AVAsset? {
        var trimmedFileUrl = documentsDirectory().appendingPathComponent(fileName)
        let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A)
        exporter?.outputFileType = AVFileTypeAppleM4A
        exporter?.outputURL = trimmedFileUrl
        let start = CMTimeMake(Int64(startOffSet), 1)
        let end = CMTimeMake(Int64(startOffSet + duration), 1)
        exporter?.timeRange = CMTimeRangeFromTimeToTime(start, end)

        exporter?.exportAsynchronously { handler in

            if exporter?.status != AVAssetExportSessionStatus.completed {
                print("Error while exporting \(exporter?.error?.localizedDescription ?? "unknown")")
                completion?(nil)
                return
            }
        }
        //------------------------------------------------------
        // this code needs to be replaced
        do {
                var resourceValus = URLResourceValues()
                resourceValus.creationDate = from
                try trimmedFileUrl.setResourceValues(resourceValus)
        } catch {
            deleteFile(atPath: trimmedFileUrl)
            print("Error while setting date - \(error.localizedDescription)")
            completion?(nil)
            return
        }
        //------------------------------------------------------
        completion?(trimmedFileUrl)
    }

1 个答案:

答案 0 :(得分:3)

您是否尝试过mofifying导出录音的元数据? https://developer.apple.com/documentation/avfoundation/avmetadatacommonkeycreationdate

AVMutableMetadataItem *item = [AVMutableMetadataItem metadataItem];
metaItem.key = AVMetadataCommonKeyCreationDate;
metaItem.keySpace = AVMetadataKeySpaceCommon;
metaItem.value = [NSDate date];
NSArray *metadata = @{ metaItem };
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetMediumQuality];
    exportSession.metadata = metadata;