gpt4 book ai didi

ios - 在多个分区中拆分视频

转载 作者:行者123 更新时间:2023-11-29 12:43:29 25 4
gpt4 key购买 nike

我在 Objective-C 中开发 iOS 应用程序。

根据我的要求,我想将视频分成多个部分。

假设我有一个 50 秒的视频,我想将它分成 5 个部分,每个部分 10 秒。

如果你们有任何想法,请告诉我。

最佳答案

@MilanPatel 答案的 Swift 5 版本,我发现它非常有用:

func splitSecondVideo() {

guard did<splitdivide else {return}
guard let videourl = URL(string: "YOUR_VIDEO_URL_HERE") else {return}
let asset = AVURLAsset(url: videourl, options: nil)

let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)

let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).map(\.path)
let documentsDirectory = paths[0]
var myPathDocs: String?
var starttime: CMTime
var duration: CMTime

myPathDocs = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("SplitFinalVideo\(did).mov").path
let endt = CMTimeGetSeconds(asset.duration)
print("All Duration : \(endt)")
let divide = CMTimeGetSeconds(asset.duration) / splitdivide
print("All Duration : \(divide)")

starttime = CMTimeMakeWithSeconds(Float64(divide * did), preferredTimescale: 1)
duration = CMTimeMakeWithSeconds(Float64(divide), preferredTimescale: 1)

let fileManager = FileManager()
var error: Error?
if fileManager.fileExists(atPath: myPathDocs ?? "") == true {
do {
try fileManager.removeItem(atPath: myPathDocs ?? "")
} catch {
}
}

exportSession?.outputURL = URL(fileURLWithPath: myPathDocs ?? "")
exportSession?.shouldOptimizeForNetworkUse = true
exportSession?.outputFileType = .mov
// Trim to half duration

let secondrange = CMTimeRangeMake(start: starttime, duration: duration)

exportSession?.timeRange = secondrange
exportSession?.exportAsynchronously(completionHandler: { [self] in
exportDidFinish(exportSession)
did += 1
self.splitSecondVideo()

})

}

func exportDidFinish(_ session: AVAssetExportSession?) {
if session?.status == .completed {
let outputURL = session?.outputURL
print("Before Exported")
saveVideoAtPath(with: outputURL)
}
}

另一个更新:session?.status 总是失败。为了解决这个问题,我补充说:

 asset.resourceLoader.setDelegate(self, queue: .main)

并符合 AVAssetResourceLoaderDelegate 协议(protocol)并开始工作。感谢this answer通过@panychyk.dima

关于ios - 在多个分区中拆分视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24243354/

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