计算视频之间的淡入淡出时间

时间:2019-01-09 23:13:14

标签: ios swift avfoundation

我必须在视频中应用不透明度。我必须在一秒钟的视频结束之前应用它。我正在使用“ firstInstruction”获得视频的总时长。但是,当我调用“ firstInstruction.setOpacityRamp”方法时,我无法减去第二个方法。

    let mainInstruction = AVMutableVideoCompositionInstruction()
    mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeAdd(firstAsset.duration, secondAsset.duration))
    let firstInstruction = VideoHelper.videoCompositionInstruction(firstTrack, asset: firstAsset)
    firstInstruction.setOpacityRamp(fromStartOpacity: 1, toEndOpacity: 0.1, timeRange: mainInstruction.timeRange)

1 个答案:

答案 0 :(得分:2)

我将使用三条指令进行淡入淡出:

  1. 一条“通过”指令,仅显示第一个视频轨道,直到第一个资产结束前一秒钟。
  2. 淡入淡出指令,同时显示第一条视频轨道的最后一秒和第二条视频轨道的第一秒,并具有不透明度渐变。
  3. 一条“通过”指令,仅显示第二条视频轨道,从一秒钟开始到第二条视频轨道。

所以,首先,让我们获得曲目:

import AVFoundation
import CoreVideo

func crossFade(asset0: AVAsset, asset1: AVAsset, crossFadeDuration: CMTime, to outputUrl: URL) throws {
    guard
        let asset0Track = asset0.tracks(withMediaType: .video).first,
        let asset1Track = asset1.tracks(withMediaType: .video).first,
        case let composition = AVMutableComposition(),
        case let compositionTrack0Id = composition.unusedTrackID(),
        let compositionTrack0 = composition.addMutableTrack(
            withMediaType: .video, preferredTrackID: compositionTrack0Id),
        case let compositionTrack1Id = composition.unusedTrackID(),
        let compositionTrack1 = composition.addMutableTrack(
            withMediaType: .video, preferredTrackID: compositionTrack1Id)
        else { return }

现在让我们计算所需的所有时间。首先,构图中的asset0Track的整个范围,包括通过和淡入淡出周期:

    // When does asset0Track start, in the composition?
    let asset0TrackStartTime = CMTime.zero

    // When does asset0Track end, in the composition?
    let asset0TrackEndTime = asset0TrackStartTime + asset0Track.timeRange.duration

接下来,淡入淡出的时间范围:

    // When does the cross-fade end, in the composition?
    // It should end exactly at the end of asset0's video track.
    let crossFadeEndTime = asset0TrackEndTime

    // When does the cross-fade start, in the composition?
    let crossFadeStartTime = crossFadeEndTime - crossFadeDuration

    // What is the entire time range of the cross-fade, in the composition?
    let crossFadeTimeRange = CMTimeRangeMake(
        start: crossFadeStartTime,
        duration: crossFadeDuration)

接下来,构图中的asset1Track的整个范围都包括淡入淡出和通过时间:

    // When does asset1Track start, in the composition?
    // It should start exactly at the start of the cross-fade.
    let asset1TrackStartTime = crossFadeStartTime

    // When does asset1Track end, in the composition?
    let asset1TrackEndTime = asset1TrackStartTime + asset1Track.timeRange.duration

最后,两个通过时间范围:

    // What is the time range during which only asset0 is visible, in the composition?
    let compositionTrack0PassThroughTimeRange = CMTimeRangeMake(
        start: asset0TrackStartTime,
        duration: crossFadeStartTime - asset0TrackStartTime)

    // What is the time range during which only asset1 is visible, in the composition?
    let compositionTrack1PassThroughTimeRange = CMTimeRangeMake(
        start: crossFadeEndTime,
        duration: asset1TrackEndTime - crossFadeEndTime)

现在,我们可以将输入轨道插入到合成的轨道中:

    // Put asset0Track into compositionTrack0.
    try compositionTrack0.insertTimeRange(
        asset0Track.timeRange,of: asset0Track, at: asset0TrackStartTime)

    // Put asset1Track into compositionTrack1.
    try compositionTrack1.insertTimeRange(
        asset1Track.timeRange, of: asset1Track, at: asset1TrackStartTime)

这就是AVMutableComposition要做的全部工作。但是我们还需要制作一个AVMutableVideoComposition

    let videoComposition = AVMutableVideoComposition()
    videoComposition.frameDuration =
        min(asset0Track.minFrameDuration, asset1Track.minFrameDuration)
    videoComposition.renderSize = CGSize(
        width: max(asset0Track.naturalSize.width, asset1Track.naturalSize.width),
        height: max(asset0Track.naturalSize.height, asset1Track.naturalSize.height))

我们需要设置视频合成的说明。第一条指令是在适当的时间范围内仅通过compositionTrack0

    // I'm using a helper function defined below.
    let compositionTrack0PassThroughInstruction = AVMutableVideoCompositionInstruction.passThrough(
        trackId: compositionTrack0Id, timeRange: compositionTrack0PassThroughTimeRange)

第二条指令用于淡入淡出,因此更为复杂。它需要两个子指令,即淡入淡出中的每一层。每层指令以及整个淡入淡出指令都使用相同的时间范围:

    let crossFadeLayer0Instruction = AVMutableVideoCompositionLayerInstruction()
    crossFadeLayer0Instruction.trackID = compositionTrack0Id
    crossFadeLayer0Instruction.setOpacityRamp(fromStartOpacity: 1, toEndOpacity: 0, timeRange: crossFadeTimeRange)

    let crossFadeLayer1Instruction = AVMutableVideoCompositionLayerInstruction()
    crossFadeLayer1Instruction.trackID = compositionTrack1Id
    crossFadeLayer1Instruction.setOpacityRamp(fromStartOpacity: 0, toEndOpacity: 1, timeRange: crossFadeTimeRange)

    let crossFadeInstruction = AVMutableVideoCompositionInstruction()
    crossFadeInstruction.timeRange = crossFadeTimeRange
    crossFadeInstruction.layerInstructions = [crossFadeLayer0Instruction, crossFadeLayer1Instruction]

第三条指令是在适当的时间范围内仅通过compositionTrack1

    let compositionTrack1PassThroughInstruction = AVMutableVideoCompositionInstruction.passThrough(
        trackId: compositionTrack1Id, timeRange: compositionTrack1PassThroughTimeRange)

现在我们已经掌握了全部三个指令,可以将它们用于视频合成:

    videoComposition.instructions = [compositionTrack0PassThroughInstruction, crossFadeInstruction, compositionTrack1PassThroughInstruction]

现在我们可以一起使用compositionvideoComposition,例如导出一个新的电影文件:

    let export = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetMediumQuality)!
    export.outputURL = outputUrl
    export.videoComposition = videoComposition
    export.exportAsynchronously {
        exit(0)
    }
}

这是我用来创建传递说明的助手:

extension AVMutableVideoCompositionInstruction {
    static func passThrough(trackId: CMPersistentTrackID, timeRange: CMTimeRange) -> AVMutableVideoCompositionInstruction {
        let layerInstruction = AVMutableVideoCompositionLayerInstruction()
        layerInstruction.trackID = trackId

        let instruction = AVMutableVideoCompositionInstruction()
        instruction.timeRange = timeRange
        instruction.layerInstructions = [layerInstruction]

        return instruction
    }
}

这是我的测试代码。我使用了macOS命令行应用进行测试:

let asset0 = AVURLAsset(url: URL(fileURLWithPath: "/tmp/asset0.mp4"))
let asset1 = AVURLAsset(url: URL(fileURLWithPath: "/tmp/asset1.mp4"))

let outputUrl = URL(fileURLWithPath: "/tmp/output.mp4")
try! crossFade(asset0: asset0, asset1: asset1, crossFadeDuration: CMTimeMake(value: 1, timescale: 1), to: outputUrl)

dispatchMain()

结果:

demo

请注意,由于Stack Overflow对图片文件大小的限制,我必须使动画小而色彩淡。

Jeffrey Beach提供输入视频。