gpt4 book ai didi

ios - Swift - M3u8 的解决方法/替代方案以播放 mp4 片段或将片段合并到 mp4

转载 作者:行者123 更新时间:2023-12-04 23:13:24 25 4
gpt4 key购买 nike

我用了 AVAssetExportSession 下载 session URL 但您无法下载直播流的问题因此要解决它,直播流被分成 10 秒的 mp4 片段,这些片段使用 m3u8 下载以创建 URL。然后我使用 AVAssetExportSession 合并那些 mp4 片段。

我可以将这些剪辑一一合并到一个 mp4 文件中,这是我想要的,但随着文件变大,处理数千个片段所需的时间越长,这变得不切实际。

我考虑过使用 AVplayerLooper 但我不能像单个视频一样在 mp4 片段中​​擦洗、倒带或前进。

有没有一种方法可以将 mp4 剪辑组合在一起以像 m3u8 那样在不合并的情况下作为一个视频播放?或者有没有快速合并视频的方法?

备注 :服务器使用 FFmpeg,但我不允许在应用程序中使用 FFmpeg 或 pod。

下面是合并视频的功能

var mp4Array: [AVAsset] = []
var avAssetExportSession: AVAssetExportSession?

var firstAsset: AVAsset?
var secondAsset: AVAsset?

func mergeVideos() {

firstAsset = mp4Array.first
secondAsset = mp4Array[1]

guard let firstAsset = firstAsset, let secondAsset = secondAsset else { return }
let mixComposition = AVMutableComposition()

guard let firstTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid)) else {return}

do {

try firstTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: firstAsset.duration),
of: firstAsset.tracks(withMediaType: .video)[0],
at: CMTime.zero)

} catch {
print("Couldn't load track 1")
return
}

guard let secondTrack = mixComposition.addMutableTrack(withMediaType: .video, preferredTrackID: Int32(kCMPersistentTrackID_Invalid)) else {return}

do {
try secondTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: secondAsset.duration),
of: secondAsset.tracks(withMediaType: .video)[0],
at: firstAsset.duration)
} catch {
print("couldn't load track 2")
return
}

let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: CMTimeAdd(firstAsset.duration, secondAsset.duration))

let firstAssetInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: firstTrack)
firstAssetInstruction.setOpacity(0.0, at: firstAsset.duration)

let secondAssetInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: secondTrack)

mainInstruction.layerInstructions = [firstAssetInstruction, secondAssetInstruction]
let mainComposition = AVMutableVideoComposition()
mainComposition.instructions = [mainInstruction]
mainComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
mainComposition.renderSize = firstTrack.naturalSize

guard let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let url = documentDirectory.appendingPathComponent("MergedVideos/mergeVideo\(videoInt).mp4")

guard let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) else {return}

exporter.outputURL = url
exporter.outputFileType = AVFileType.mp4
exporter.shouldOptimizeForNetworkUse = true
exporter.videoComposition = mainComposition

exporter.exportAsynchronously {

if exporter.status == .completed {
let avasset = AVAsset(url:url)
self.mergeUrl = avasset
if self.mp4Array.count > 1{
print("This add the merged video to the front of the mp4array")
self.mp4Array.remove(at: 1)
self.mp4Array.removeFirst()
self.videoInt = self.videoInt + 1
self.mp4Array.append(self.mergeUrl!)
self.mp4Array.bringToFront(item: self.mp4Array.last!)
}

if (self.mp4Array.count > 1){
if self.mergeUrl != nil {
self.mergeVideos()
}
} else {
var numberofvideosdeleted = 0
while (numberofvideosdeleted < self.videoInt - 1){
do {
print("deleting")
let url = documentDirectory.appendingPathComponent("MergedVideos/mergeVideo\(numberofvideosdeleted).mp4")
try FileManager.default.removeItem(at: url)
numberofvideosdeleted = numberofvideosdeleted + 1
} catch {
print("Error removing videos")
}
}

self.deleteCurrentSegementsInFolder()
}
}
}
}

最佳答案

我最终使用 FFmpeg Mobile 连接视频,效果非常好。连接 3GB 电影文件大约需要 1 分钟。

下面链接到 cocoapod:
https://github.com/tanersener/mobile-ffmpeg

关于ios - Swift - M3u8 的解决方法/替代方案以播放 mp4 片段或将片段合并到 mp4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58313414/

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