0

I have one query in my application, for that i want to convert my AVI formatted video to MP4 movie format, so is there any way to do this programatically.

Any code snippet will be appreciated.

chridam
  • 100,957
  • 23
  • 236
  • 235
Tejas Patel
  • 850
  • 10
  • 26

2 Answers2

5

You need to use AVAssetExportSession to convert videos to .mp4 format, below method convert .avi format videos to .mp4.

Check the line exportSession.outputFileType = AVFileTypeMPEG4; it specify the output format of the video.

Here inputURL is an url of video which needs to be converted and outputURL will be the final destination of video.

One more thing don't forget to specify .mp4 extension in outputURL video file.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyVideo.mp4"];
NSURL *outputURL = [NSURL fileURLWithPath:filePath];

[self convertVideoToLowQuailtyWithInputURL:localUrl outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
{
    if (exportSession.status == AVAssetExportSessionStatusCompleted) {
        // Video conversation completed
    }          
}];

- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler {
    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
    exportSession.outputURL = outputURL;
    exportSession.outputFileType = AVFileTypeMPEG4;
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
         handler(exportSession);
     }];
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
  • In the above code it won't compress the video as you've passed presetName:AVAssetExportPresetPassthrough right ? If yes I can use the above code to only trim the video and compression I can do later. Let me know if I'm missing something – Rahul Vyas Feb 26 '21 at 14:45
2

Swift 4.2

An updated version of the accepted answer in Swift 4.2.

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let filePath = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("MyVideo.mp4").absoluteString
let outputURL = URL(fileURLWithPath: filePath)
convertVideoToLowQuailty(withInputURL: inputUrl, outputURL: outputURL, handler: { exportSession in
    if exportSession?.status == .completed {
        // Video conversation completed
    }
})

func convertVideoToLowQuailty(withInputURL inputURL: URL?, outputURL: URL?, handler: @escaping (AVAssetExportSession?) -> Void) {
    if let anURL = outputURL {
        try? FileManager.default.removeItem(at: anURL)
    }
    var asset: AVURLAsset? = nil
    if let anURL = inputURL {
        asset = AVURLAsset(url: anURL, options: nil)
    }
    var exportSession: AVAssetExportSession? = nil
    if let anAsset = asset {
        exportSession = AVAssetExportSession(asset: anAsset, presetName: AVAssetExportPresetPassthrough)
    }
    exportSession?.outputURL = outputURL
    exportSession?.outputFileType = .mp4
    exportSession?.exportAsynchronously(completionHandler: {
        handler(exportSession)
    })
}

Took help from an online conversion tool.

Community
  • 1
  • 1
Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45