swift: how to delete part of audio?

。_饼干妹妹 提交于 2019-11-27 07:24:18

问题


I'm creating a simple audio editing tool to trim and delete from an audio. I implemented the trim function and it is working fine. However I searched and tried to implement the delete function and here is my code:

func deleteExportAsset(_ asset: AVAsset, fileName: String, completeAudioTime: CGFloat) -> URL {
    print("\(#function)")

    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let trimmedSoundFileURL = documentsDirectory.appendingPathComponent(fileName)
    print("saving to \(trimmedSoundFileURL.absoluteString)")

    if FileManager.default.fileExists(atPath: trimmedSoundFileURL.absoluteString) {
        print("sound exists, removing \(trimmedSoundFileURL.absoluteString)")
        do {
            if try trimmedSoundFileURL.checkResourceIsReachable() {
                print("is reachable")
            }

            try FileManager.default.removeItem(atPath: trimmedSoundFileURL.absoluteString)
        } catch {
            print("could not remove \(trimmedSoundFileURL)")
            print(error.localizedDescription)
        }

    }

    print("creating export session for \(asset)")

    if let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) {
        exporter.outputFileType = AVFileType.m4a
        exporter.outputURL = trimmedSoundFileURL

        let timeRange1 = CMTimeRangeFromTimeToTime(CMTime(seconds: 0, preferredTimescale: 100), CMTime(seconds: endTimeOfRange1, preferredTimescale: 100))
        let timeRange2 = CMTimeRangeFromTimeToTime(CMTime(seconds: startTimeOfRange2)), preferredTimescale: 100), CMTime(seconds: Double(completeAudioTime), preferredTimescale: 100))
        exporter.timeRange = CMTimeRangeGetUnion(timeRange1, timeRange2)

        // do it
        exporter.exportAsynchronously(completionHandler: {
            print("export complete \(exporter.status)")

            switch exporter.status {
            case  AVAssetExportSessionStatus.failed:

                if let e = exporter.error {
                    print("export failed \(e)")
                }


            case AVAssetExportSessionStatus.cancelled:
                print("export cancelled \(String(describing: exporter.error))")
            default:
                print("export complete")
            }
        })
    } else {
        print("cannot create AVAssetExportSession for asset \(asset)")
    }

    return trimmedSoundFileURL
}

what I'm doing here is creating 2 Ranges. Range1 from 0 ->time1 and Range2 from time2->endOfAudio. (I want to delete from time1 -> time2)

then I'm creating the union between 2 ranges. however, nothing is happening to the audio. It is saved exactly like it was before this function.


回答1:


CMTimeRangeGetUnion returns another CMTimeRange, which is just a (start-)time and a duration. So there is nothing than can hold the two time ranges required to do what you are expecting. In extension, AVAssetExportSession has no API that takes a list of time ranges to export.

But there is a way to accomplish it. The idea is to create an editable copy of the asset, delete the time range, and then export the editable copy. AVMutableComposition does this:

// assuming 'asset', 'endTimeOfRange1' and 'startTimeOfRange2' from the question:

// create empty mutable composition
let composition: AVMutableComposition = AVMutableComposition()
// copy all of original asset into the mutable composition, effectively creating an editable copy
try composition.insertTimeRange( CMTimeRangeMake( kCMTimeZero, asset.duration), of: asset, at: kCMTimeZero)

// now edit as required, e.g. delete a time range
let startTime = CMTime(seconds: endTimeOfRange1, preferredTimescale: 100)
let endTime = CMTime(seconds: startTimeOfRange2, preferredTimescale: 100)
composition.removeTimeRange( CMTimeRangeFromTimeToTime( startTime, endTime))

// since AVMutableComposition is an AVAsset subclass, it can be exported with AVAssetExportSession (or played with an AVPlayer(Item))
if let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A) 
{
    // configure session and exportAsynchronously as above. 
    // You don't have to set the timeRange of the exportSession
}

Note that copying from the asset to the composition only modifies some in-memory structures defining which samples go where on the time line, but doesn't actually moves any media samples around. This is not done until exporting; as result, editing is (relatively) fast, and you have to keep the source file around at least until export is finished.



来源:https://stackoverflow.com/questions/53928988/swift-how-to-delete-part-of-audio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!