0

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")
                }
            }
Shawn Frank
  • 4,381
  • 2
  • 19
  • 29
Amin Rezaew
  • 298
  • 1
  • 14
  • What do you mean by breaks ? Does it ot escape the guard condition ? Is there anyway to access the mkv file you use ? – Shawn Frank Mar 16 '22 at 12:21
  • yes, it scapes from guard – Amin Rezaew Mar 16 '22 at 12:42
  • By the way, you say you are trying to convert `MKV`, but you are trying to load an `MOV`, maybe this is your issue `let sourceUrl = Bundle.main.url(forResource: "sample", withExtension: "mov")!` - trying change `mov` to `mkv` – Shawn Frank Mar 16 '22 at 12:43
  • @Shawn_Frank you know how can i extract video and audio from MKV containe and merge it in MP4 in swift? – Amin Rezaew Mar 16 '22 at 12:49
  • @ShawnFrank when i changed MOV to MKV my video asset track breaked – Amin Rezaew Mar 16 '22 at 12:52
  • So do you even have an MKV or an MOV ? Add a screenshot of your file in your folder. If it breaks when you add MKV, it means you probably have another format. – Shawn Frank Mar 16 '22 at 12:55
  • i have two videos with one name (sample) and with two extensions (MOV and MKV) when i add my MOV video and run my code that is work right but when i add MKV video that's break – Amin Rezaew Mar 16 '22 at 13:00
  • Ok, I understand better now, I will try a bit later if I can find an mkv video file or better if some way you know I can see the sample file you use ? – Shawn Frank Mar 16 '22 at 13:03
  • just search in google (MKV sample videos) and download one of them, thanks for you – Amin Rezaew Mar 16 '22 at 13:07
  • I tried at my end and I see that actually AVFoundation / AVKit does not support `MKV` format: https://stackoverflow.com/a/45898816/1619193 - you will probably need to use some library to work with this. One library is https://code.videolan.org/videolan/VLCKit or try to use online service / API to convert this – Shawn Frank Mar 16 '22 at 17:11

0 Answers0