首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift合并.ts或mpeg文件

Swift合并.ts或mpeg文件
EN

Stack Overflow用户
提问于 2017-06-26 09:55:39
回答 1查看 1K关注 0票数 3

我有一个本地文件数组,我从.m3u8播放列表中下载了它们,因为我必须保存它们以便稍后在本地播放。所有的文件都是.ts格式的,我想把它们合并成一个单一的视频文件。我已经尝试使用AVMutableComposition合并文件,我尝试将所有本地文件转换为AVAsset,但属性.tracks总是返回0,因此我假定AVAsset不正确,然后我尝试将所有文件重命名为MPEG,但问题仍然存在。

有没有人知道如何正确阅读这些文件,这是我目前为止的代码:

代码语言:javascript
复制
 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")
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2017-07-03 17:50:32

您不能处理.ts文件,而且将.tv扩展名重命名为.mp4是不够的

您需要将.tv文件转换为mp4。您可以使用这个库https://github.com/Keemotion/TS2MP4来实现这一点

然后你就可以混合视频了

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44752232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档