iOS以编程方式将AVI转换为MP4格式 [英] iOS Convert AVI to MP4 Format programatically
问题描述
我的应用程序中有一个查询,因为我想将AVI格式的视频转换为MP4电影格式,所以有什么方法可以通过编程方式实现.
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.
推荐答案
您需要使用AVAssetExportSession
将视频转换为.mp4
格式,以下方法将.avi
格式的视频转换为.mp4
.
You need to use AVAssetExportSession
to convert videos to .mp4
format, below method convert .avi
format videos to .mp4
.
检查exportSession.outputFileType = AVFileTypeMPEG4;
行,该行指定视频的输出格式.
Check the line exportSession.outputFileType = AVFileTypeMPEG4;
it specify the output format of the video.
此处inputURL
是视频的网址,需要转换,outputURL
将是视频的最终目标.
Here inputURL
is an url of video which needs to be converted and outputURL
will be the final destination of video.
还有一件别忘了在outputURL
视频文件中指定.mp4
扩展名.
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);
}];
}
这篇关于iOS以编程方式将AVI转换为MP4格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!