Last active
February 5, 2025 12:11
-
-
Save iAmrSalman/0f2624ef3f4982600f53a4c9f0201fb0 to your computer and use it in GitHub Desktop.
[Convert MOV to MP4] function to convert MOV to MP4 video format#swift #avkit #avasset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Don't forget to import AVKit | |
func encodeVideo(at videoURL: URL, completionHandler: ((URL?, Error?) -> Void)?) { | |
let avAsset = AVURLAsset(url: videoURL, options: nil) | |
let startDate = Date() | |
//Create Export session | |
guard let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) else { | |
completionHandler?(nil, nil) | |
return | |
} | |
//Creating temp path to save the converted video | |
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL | |
let filePath = documentsDirectory.appendingPathComponent("rendered-Video.mp4") | |
//Check if the file already exists then remove the previous file | |
if FileManager.default.fileExists(atPath: filePath.path) { | |
do { | |
try FileManager.default.removeItem(at: filePath) | |
} catch { | |
completionHandler?(nil, error) | |
} | |
} | |
exportSession.outputURL = filePath | |
exportSession.outputFileType = AVFileType.mp4 | |
exportSession.shouldOptimizeForNetworkUse = true | |
let start = CMTimeMakeWithSeconds(0.0, 0) | |
let range = CMTimeRangeMake(start, avAsset.duration) | |
exportSession.timeRange = range | |
exportSession.exportAsynchronously(completionHandler: {() -> Void in | |
switch exportSession.status { | |
case .failed: | |
print(exportSession.error ?? "NO ERROR") | |
completionHandler?(nil, exportSession.error) | |
case .cancelled: | |
print("Export canceled") | |
completionHandler?(nil, nil) | |
case .completed: | |
//Video conversion finished | |
let endDate = Date() | |
let time = endDate.timeIntervalSince(startDate) | |
print(time) | |
print("Successful!") | |
print(exportSession.outputURL ?? "NO OUTPUT URL") | |
completionHandler?(exportSession.outputURL, nil) | |
default: break | |
} | |
}) | |
} |
i got error with ios 16:
[Error: convert failed for Users/thehe/Library/Developer/CoreSimulator/Devices/A69D905C-4531-438C-9123-85D8EE81EC54/data/Containers/Data/Application/06CC0F2D-3D96-4734-8AAB-05816F14F6B7/tmp/com.vedax.vedaxlinkv2-Inbox/IMG_1668.mp4.MOV, error: The operation could not be completed]
Not working with latest iOS versions
This code working fine for me
// Function to encode the video to MP4 format
// The function to convert video to MP4
func encodeVideo(at videoURL: URL, completionHandler: ((URL?, Error?) -> Void)?) {
let avAsset = AVURLAsset(url: videoURL, options: nil)
let startDate = Date()
// Create Export session
guard let exportSession = AVAssetExportSession(asset: avAsset, presetName: AVAssetExportPresetPassthrough) else {
completionHandler?(nil, nil)
return
}
// Creating temp path to save the converted video
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL
let filePath = documentsDirectory.appendingPathComponent("rendered-Video.mp4")
// Check if the file already exists then remove the previous file
if FileManager.default.fileExists(atPath: filePath.path) {
do {
try FileManager.default.removeItem(at: filePath)
} catch {
completionHandler?(nil, error)
}
}
exportSession.outputURL = filePath
exportSession.outputFileType = AVFileType.mp4
exportSession.shouldOptimizeForNetworkUse = true
let start = CMTimeMakeWithSeconds(0.0, preferredTimescale: 0)
let range = CMTimeRangeMake(start: start, duration: avAsset.duration)
exportSession.timeRange = range
exportSession.exportAsynchronously(completionHandler: {() -> Void in
switch exportSession.status {
case .failed:
print(exportSession.error ?? "NO ERROR")
completionHandler?(nil, exportSession.error)
case .cancelled:
print("Export canceled")
completionHandler?(nil, nil)
case .completed:
// Video conversion finished
let endDate = Date()
let time = endDate.timeIntervalSince(startDate)
print(time)
print("Successful!")
print(exportSession.outputURL ?? "NO OUTPUT URL")
completionHandler?(exportSession.outputURL, nil)
default: break
}
})
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's code is very good.
but it don't work in ios 13.
do you have any solution to fix it in ios 13?
if you have solution, please contact to [email protected]
Best Regards.