我有一个应用程序,允许用户录制视频,UIImagePickerController然后将其上传到YouTube.问题是UIImagePickerController创建的视频文件是巨大的,即使视频只有5秒长.例如,5秒长的视频是16-20兆字节.我想保持540或720质量的视频,但我想减少文件大小.
我一直在尝试AVFoundation并AVAssetExportSession试图获得更小的文件大小.我试过以下代码:
AVAsset *video = [AVAsset assetWithURL:videoURL];
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:video presetName:AVAssetExportPresetPassthrough];
exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = [pathToSavedVideosDirectory URLByAppendingPathComponent:@"vid1.mp4"];
[exportSession exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"done processing video!");
}];
Run Code Online (Sandbox Code Playgroud)
但这并没有减少文件大小.我知道我正在做的事情是可能的,因为在Apple的照片应用程序中,当您选择"在YouTube上分享"时,会自动处理视频文件,因此它的小到可以上传.我想在我的应用程序中做同样的事情.
我怎么能做到这一点?
我有以下问题 - 我正在尝试创建一个记录视频的应用程序,然后将其保存到相机胶卷,之后我将该视频上传到网络.问题是唯一支持的格式是"mp4",但我的视频是"mov".
所以我的问题是如何以"mp4"格式从相机保存视频,或将其保存在"mov"中,然后将其转换为"mp4".
这是我的代码:
这是我打开相机的方式:
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.showsCameraControls = YES;
picker.allowsEditing = YES;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
[self presentViewController:picker animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)这是我保存视频的方式:
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
{
NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
videoURL = info[UIImagePickerControllerMediaURL];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath))
{
UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self, nil, nil);
}
}
[nextScreenButton setTitle:@"????????" forState:UIControlStateNormal];
[self dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)提前致谢!
AVAssetExportSession输出文件应该如何?我正在尝试从ALAsset项目压缩视频,但它不起作用.我猜输出文件与它有关.
这是我正在使用的代码:
NSString *destinationPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie"];
[self convertVideoToLowQualityWithInputURL:asset.defaultRepresentation.url outputURL:[NSURL URLWithString:destinationPath]];
- (void)convertVideoToLowQualityWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL {
if([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithContentsOfURL:outputURL encoding: 0 error:Nil]]) [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *assetAV = [AVURLAsset URLAssetWithURL:inputURL options:nil];
NSLog(@"url string from asset: %@", assetAV.URL);
NSLog(@"output url: %@", [outputURL absoluteString]);
[[NSFileManager defaultManager] createFileAtPath:[outputURL path] contents:nil attributes:nil];
NSLog(@"duration: %lld", assetAV.duration.value); //it logs a valid value, so it's all good so far
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:assetAV presetName:AVAssetExportPresetLowQuality];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
if …Run Code Online (Sandbox Code Playgroud) 是否可以保存视频并将其添加到自定义 ALAsset,以 mp4 格式从 UIImagePicker 捕获?或者我必须将它保存在 .mov 中并通过 AVAssetExportSession 进行压缩?
我正在使用 UIImagePickerController 从我的 swift iOS 应用程序中选择一个视频。我正在保存此 URL,现在想将其转换为数据以使用以下方法发送到我的服务器进行存储:
let messageVideoData = NSData(contentsOfURL: chosenVideoURL)
Run Code Online (Sandbox Code Playgroud)
问题是文件大小非常大。对于在我的 iPhone 6s 上拍摄的 7 秒视频,分辨率为 1280、720,帧速率为 30,文件大小超过 4 MB。我注意到用 whatsapp 和其他聊天应用程序发送的相同图像减少到几百 KB。
减少外部存储文件大小的最佳方法是什么?该视频主要针对手机,因此将分辨率降低到 800 或更低就可以了。
我尝试将 UIImagePickerController 质量设置为:
picker.videoQuality = UIImagePickerControllerQualityType.Type640x480
Run Code Online (Sandbox Code Playgroud)
但这只会将文件大小减少到 3.5 MB。
使用:
picker.videoQuality = UIImagePickerControllerQualityType.TypeLow
Run Code Online (Sandbox Code Playgroud)
将分辨率降低到远低于所需的值。
我应该采取另一种方法来减少存储在服务器上的视频文件大小吗?
我尝试过以下方法:
- (void)compress:(NSURL *)videoPath completionBlock:(void(^)(id data, BOOL result))block{
self.outputFilePath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
NSURL *outputURL = [NSURL fileURLWithPath:self.outputFilePath];
[self compressVideoWithURL:self.movieURL outputURL:outputURL handler:^(AVAssetExportSession *exportSession) {
}];
}
- (void)compressVideoWithURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
handler:(void (^)(AVAssetExportSession*))handler {
AVURLAsset *asset = [AVURLAsset assetWithURL:self.movieURL];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
exportSession.fileLengthLimit = 3000000;
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
exportSession.shouldOptimizeForNetworkUse = YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
NSData *newOutputData = [NSData dataWithContentsOfURL:outputURL];
NSLog(@"Size of New Video(bytes):%d",[newOutputData length]);
}];
}
Run Code Online (Sandbox Code Playgroud)
我知道self.movieUrl不是 …
ios ×5
video ×3
avfoundation ×2
iphone ×2
avasset ×1
compression ×1
file-upload ×1
mp4 ×1
objective-c ×1
swift ×1
xcode ×1