我有一个本地文件数组,我从.m3u8播放列表中下载了它们,因为我必须保存它们以便稍后在本地播放。所有的文件都是.ts格式的,我想把它们合并成一个单一的视频文件。我已经尝试使用AVMutableComposition合并文件,我尝试将所有本地文件转换为AVAsset,但属性.tracks总是返回0,因此我假定AVAsset不正确,然后我尝试将所有文件重命名为MPEG,但问题仍然存在。
有没有人知道如何正确阅读这些文件,这是我目前为止的代码:
func mergeAllVideos(filesPath: URL) {
let allVideos = extractAllFile(atPath: filesPath.absoluteString)
var arrayVideos = [AVURLAsset]()
var atTimeM: CMTime = CMTimeMake(0, 0)
var lastAsset: AVAsset!
var layerInstructionsArray = [AVVideoCompositionLayerInstruction]()
var completeTrackDuration: CMTime = CMTimeMake(0, 1)
var videoSize: CGSize = CGSize(width: 0.0, height: 0.0)
var totalTime : CMTime = CMTimeMake(0, 0)
for asset in allVideos.enumerated() {
if let url = URL(string: asset.element) {
let newAsset = AVURLAsset(url: url)
arrayVideos.append(newAsset)
}
}
let mixComposition = AVMutableComposition()
for videoAsset in arrayVideos {
let player = AVPlayer(url: videoAsset.url)
let videoTrack = mixComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))
do {
if videoAsset == arrayVideos.first{
atTimeM = kCMTimeZero
} else{
atTimeM = totalTime
}
print(videoAsset.tracks)
try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), of: videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0], at: atTimeM)
videoSize = videoTrack.naturalSize
} catch let error as NSError {
print("error: \(error)")
}
totalTime = CMTimeAdd(totalTime, videoAsset.duration)
completeTrackDuration = CMTimeAdd(completeTrackDuration, videoAsset.duration)
let videoInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
if let asset = arrayVideos.last, videoAsset != asset {
videoInstruction.setOpacity(0.0, at: completeTrackDuration)
}
layerInstructionsArray.append(videoInstruction)
lastAsset = videoAsset
}
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, completeTrackDuration)
mainInstruction.layerInstructions = layerInstructionsArray
let mainComposition = AVMutableVideoComposition()
mainComposition.instructions = [mainInstruction]
mainComposition.frameDuration = CMTimeMake(1, 30)
mainComposition.renderSize = CGSize(width: videoSize.width, height: videoSize.height)
let documentDirectory = NSSearchPathForDirectoriesInDomains(.developerApplicationDirectory, .userDomainMask, true)[0]
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .short
let date = dateFormatter.string(from: NSDate() as Date)
let savePath = (documentDirectory as NSString).appendingPathComponent("mergeVideo-\(date).mov")
let url = NSURL(fileURLWithPath: savePath)
let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
exporter!.outputURL = url as URL
exporter!.outputFileType = AVFileTypeQuickTimeMovie
exporter!.shouldOptimizeForNetworkUse = true
exporter!.videoComposition = mainComposition
exporter!.exportAsynchronously {
print("exported")
}
}发布于 2017-07-03 17:50:32
您不能处理.ts文件,而且将.tv扩展名重命名为.mp4是不够的
您需要将.tv文件转换为mp4。您可以使用这个库https://github.com/Keemotion/TS2MP4来实现这一点
然后你就可以混合视频了
https://stackoverflow.com/questions/44752232
复制相似问题