Swift合并.ts或mpeg文件

时间:2017-06-26 01:55:39

标签: swift macos cocoa video avasset

我有一系列本地文件,我是从.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")
        }
    }

1 个答案:

答案 0 :(得分:2)

您无法使用.ts个文件,将.tv扩展名重命名为.mp4也不够

您需要将.tv文件翻译为mp4。您可以将此库https://github.com/Keemotion/TS2MP4用于此

然后你就可以混合视频了