gpt4 book ai didi

swift - 无缝连接 AVAssets

转载 作者:行者123 更新时间:2023-11-28 13:49:47 24 4
gpt4 key购买 nike

我有一些简单的 AVFoundation 代码可以将一堆四秒长的 mp4 文件连接在一起,如下所示:

func
compose(parts inParts: [Part], progress inProgress: (CMTime) -> ())
-> AVAsset?
{
guard
let composition = self.composition,
let videoTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid),
let audioTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)
else
{
debugLog("Unable to create tracks for composition")
return nil
}

do
{
var time = CMTime.zero
for p in inParts
{
let asset = AVURLAsset(url: p.path.url)
if let track = asset.tracks(withMediaType: .video).first
{
try videoTrack.insertTimeRange(CMTimeRange(start: .zero, duration: asset.duration), of: track, at: time)
}
if let track = asset.tracks(withMediaType: .audio).first
{
try audioTrack.insertTimeRange(CMTimeRange(start: .zero, duration: asset.duration), of: track, at: time)
}

time = CMTimeAdd(time, asset.duration)
inProgress(time)
}
}

catch (let e)
{
debugLog("Error adding clips: \(e)")
return nil
}

return composition
}

不幸的是,每隔四秒您就会听到音频中断片刻,这向我表明这并不是一个完全无缝的连接。我可以做些什么来改善这一点吗?

解决方案

感谢下面 NoHalfBits 的出色回答,我用以下内容更新了上面的循环,并且效果很好:

        for p in inParts
{
let asset = AVURLAsset(url: p.path.url)

// It’s possible (and turns out, it’s often the case with UniFi NVR recordings)
// for the audio and video tracks to be of slightly different start time
// and duration. Find the intersection of the two tracks’ time ranges and
// use that range when inserting both tracks into the composition…

// Calculate the common time range between the video and audio tracks…

let sourceVideo = asset.tracks(withMediaType: .video).first
let sourceAudio = asset.tracks(withMediaType: .audio).first
var commonTimeRange = CMTimeRange.zero
if sourceVideo != nil && sourceAudio != nil
{
commonTimeRange = CMTimeRangeGetIntersection(sourceVideo!.timeRange, otherRange: sourceAudio!.timeRange)
}
else if sourceVideo != nil
{
commonTimeRange = sourceVideo!.timeRange
}
else if sourceAudio != nil
{
commonTimeRange = sourceAudio!.timeRange
}
else
{
// There’s neither video nor audio tracks, bail…

continue
}

debugLog("Asset duration: \(asset.duration.seconds), common time range duration: \(commonTimeRange.duration.seconds)")

// Insert the video and audio tracks…

if sourceVideo != nil
{
try videoTrack.insertTimeRange(commonTimeRange, of: sourceVideo!, at: time)
}
if sourceAudio != nil
{
try audioTrack.insertTimeRange(commonTimeRange, of: sourceAudio!, at: time)
}

time = time + commonTimeRange.duration
inProgress(time)
}

最佳答案

在 mp4 容器中,每个轨道都可以有自己的开始时间和持续时间。特别是在录制的 Material 中,音频和视频轨道的时间范围略有不同的情况并不少见(在 insertTimeRange 附近插入一些 CMTimeRangeShow(track.timeRange) 以查看这个)。

为了克服这个问题,而不是盲目地从 CMTime.zero 插入整个 Assets 的持续时间(所有轨道的最大结束时间):

  • 获取源音视频轨道的timeRange
  • 根据这些计算公共(public)时间范围(CMTimeRangeGetIntersection 为您完成)
  • 在将片段从源轨道插入到目标轨道时使用公共(public)时间范围
  • 将您的时间增加到公共(public)时间范围的持续时间

关于swift - 无缝连接 AVAssets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54840166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com