動画を保存して、UIImagePicker から mp4 形式でキャプチャしたカスタム ALAsset に追加することはできますか? それとも、.mov に保存して、AVAssetExportSession で圧縮する必要がありますか?
1782 次
2 に答える
2
はい、を使用してビデオを圧縮できますAVAssetExportSession
。ここでは、圧縮ビデオのビデオ タイプ、品質、および出力 URL を指定できます。
以下の方法を参照してください。
- (void) saveVideoToLocal:(NSURL *)videoURL {
@try {
NSArray *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [documentsDirectory objectAtIndex:0];
NSString *videoName = [NSString stringWithFormat:@"sampleVideo.mp4"];
NSString *videoPath = [docPath stringByAppendingPathComponent:videoName];
NSURL *outputURL = [NSURL fileURLWithPath:videoPath];
NSLog(@"Loading video");
[self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession) {
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
NSLog(@"Compression is done");
}
[self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
}];
}
@catch (NSException *exception) {
NSLog(@"Exception :%@",exception.description);
[self performSelectorOnMainThread:@selector(doneCompressing) withObject:nil waitUntilDone:YES];
}
}
//---------------------------------------------------------------
- (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);
}];
}
ここでは、圧縮ビデオをアプリケーションのドキュメント ディレクトリに保存しました。以下のサンプルコードで、これの詳細な動作を確認できます。
サンプルデモ:
于 2014-11-26T09:35:00.753 に答える