gpt4 book ai didi

iPhone:以编程方式压缩录制的视频以进行共享?

转载 作者:行者123 更新时间:2023-12-03 18:12:24 24 4
gpt4 key购买 nike

在录制视频之前调用相机 View 时,我实现了覆盖 View 。

pickerController.cameraOverlayView =myOverlay;

视频录制以及录制视频后将视频保存到相册并通过电子邮件等共享,一切正常。

如果我将视频质量设置为“高质量”,则录制的视频会变得很大。例如,如果我录制 30 秒的高质量视频,则录制的视频大约为 30 - 40 mb。

pickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;

如何编程在共享之前压缩高质量录制的视频,就像 Apple 使用内置视频录像机所做的那样?

请指导我解决这个问题。

谢谢!

更新:

这是我最近尝试的,但仍然没有成功:我想压缩拍摄到 didFinishPickingMediaWithInfo 的录制视频并将其存储在同一个相册中实际视频路径本身,而不是其他任何地方。我测试了当我从照片库中选取时,相同的视频被压缩到非常小的尺寸,但从相机拍摄并通过 didFinishPickingMediaWithInfo 获得的相同视频未压缩,尽管我使用了下面的 AVAssetExportSession 代码。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];


if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{

NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSString *urlPath = [videoURL path];

if ([[urlPath lastPathComponent] isEqualToString:@"capturedvideo.MOV"])
{
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (urlPath))
{
[self copyTempVideoToMediaLibrary :urlPath];


}
else
{
NSLog(@"Video Capture Error: Captured video cannot be saved...didFinishPickingMediaWithInfo()");
}
}
else
{
NSLog(@"Processing soon to saved photos album...else loop of lastPathComponent..didFinishPickingMediaWithInfo()");
}
}
[self dismissModalViewControllerAnimated:YES];
}

- (void)copyTempVideoToMediaLibrary :(NSString *)videoURL {

dispatch_queue_t mainQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(mainQueue, ^{

ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];

ALAssetsLibraryWriteVideoCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {
NSLog(@"Saved URL: %@", assetURL);
NSLog(@"Error: %@", error);

if (assetURL != nil) {

AVURLAsset *theAsset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:videoURL] options:nil];

NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:theAsset];

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:theAsset presetName:AVAssetExportPresetLowQuality];

[exportSession setOutputURL:[NSURL URLWithString:videoURL]];
[exportSession setOutputFileType:AVFileTypeQuickTimeMovie];

[exportSession exportAsynchronouslyWithCompletionHandler:^ {
switch ([exportSession status]) {
case AVAssetExportSessionStatusFailed:
NSLog(@"Export session faied with error: %@", [exportSession error]);
break;
default:
//[self mediaIsReady];
break;
}
}];
}
};

[library writeVideoAtPathToSavedPhotosAlbum:[NSURL URLWithString:videoURL] completionBlock:completionBlock];
});
}

最佳答案

如果您想压缩视频以进行远程共享并在 iPhone 上的本地存储中保持原始质量,您应该查看 AVAssetExportSessionAVAssetWriter .

另请阅读 iOS 如何管理 Assets .

- (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:AVAssetExportPresetLowQuality];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
{
handler(exportSession);
[exportSession release];
}];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSURL *outputURL = [NSURL fileURLWithPath:@"/Users/josh/Desktop/output.mov"];
[self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession)
{
if (exportSession.status == AVAssetExportSessionStatusCompleted)
{
printf("completed\n");
}
else
{
printf("error\n");

}
}];

}

关于iPhone:以编程方式压缩录制的视频以进行共享?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5687341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com