我從ALASSET獲得了視頻URL,然後convertVideoToLowQuailtyWithInputURL
函數來壓縮視頻,但它不能工作。我壓縮後看到,視頻的大小始終爲0無法在iOS中壓縮來自ALASSET URL的視頻?
這是函數從ALASSET獲得視頻網址:
ALAsset *alasset = [allVideos objectAtIndex:i];
ALAssetRepresentation *rep = [alasset defaultRepresentation];
NSString * videoName = [rep filename];
//compress video data before uploading
NSURL *videoURL = [rep url];
NSLog(@"videoURL is %@",videoURL);
NSURL *uploadURL = [NSURL fileURLWithPath:[[NSTemporaryDirectory() stringByAppendingPathComponent:videoName] stringByAppendingString:@".mov"]];
NSLog(@"uploadURL temp is %@",uploadURL);
// Compress movie first
[self convertVideoToLowQuailtyWithInputURL:videoURL outputURL:uploadURL handler:^(AVAssetExportSession *session)
{
if (session.status == AVAssetExportSessionStatusCompleted)
{
// Success
}
else
{
// Error Handing
}
}];
NSString *path = [uploadURL path];
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
NSLog(@"size after compress video is %d",data.length);
}
功能壓縮視頻:
- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
handler:(void (^)(AVAssetExportSession*))handler
{
[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset: urlAsset presetName:AVAssetExportPresetLowQuality];
session.outputURL = outputURL;
session.outputFileType = AVFileTypeQuickTimeMovie;
[session exportAsynchronouslyWithCompletionHandler:^(void)
{
handler(session);
}];
}
當我打電話convertVideoToLowQuailtyWithInputURL
功能,我沒有看到它觸發到handler(session);
。
和NSLog(@"size after compress video is %d",data.length);
總是打印「尺寸爲0」。 我哪裏出錯了?請給我一些建議。提前致謝。