1 year ago

#297798

test-img

Amin Rezaew

how to get video and audio from MKV? (swift)

i want to convert MKV (Matroska) to MP4 in swift

when i add a file with MKV format my code break in line 8 , what should i do to fix that?

this is my code:

 let composition = AVMutableComposition()   

            do {
                let sourceUrl = Bundle.main.url(forResource: "sample", withExtension: "mov")! 
                let asset = AVURLAsset(url: sourceUrl)
8 ->here        guard let videoAssetTrack = asset.tracks(withMediaType: AVMediaType.video).first else { return } 
                guard let audioCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid) else { return }
                try audioCompositionTrack.insertTimeRange(videoAssetTrack.timeRange, of: videoAssetTrack, at: CMTime.zero)
            } catch {
                print(error)
            }

            // Create an export session
            let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetPassthrough)!
            exportSession.outputFileType = AVFileType.mp4
            exportSession.outputURL = browseURL

            // Export file
            exportSession.exportAsynchronously {
                guard case exportSession.status = AVAssetExportSession.Status.completed else { return }

                DispatchQueue.main.async {
                    // Present a UIActivityViewController to share audio file
                   print("completed")
                }
            }

ios

swift

video

avmutablecomposition

mkv

0 Answers

Your Answer

Accepted video resources