如何在iPhone应用程序中压缩录制的视频

时间:2013-10-08 05:51:22

标签: iphone ios video compression

我有一个iPhone应用程序,我正在录制视频和播放该视频它工作正常。但文件大小来了巨大的3MB 30秒任何想法或方式压缩到文件在这里我想压缩按钮是点击屏幕,它应压缩录制的视频。

  Saved Video 



    NSURL*videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    NSLog(@"found a video");

    videoData = [[NSData dataWithContentsOfURL:videoURL] retain];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormat setDateFormat:@"dd-MM-yyyy_HH:mm:SS"];
    NSDate *now = [[[NSDate alloc] init] autorelease];
    NSDate* theDate = [dateFormat stringFromDate:now];


NSString*myDate=[dateFormat stringFromDate:theDate];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];



NSString*test=@"test";


NSString*testUser=[test stringByReplacingOccurrencesOfString:@" " withString:@""];



videopath= [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mp4",documentsDirectory,testUser]] autorelease];
    BOOL success = [videoData writeToFile:videopath atomically:NO];

    NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
    NSLog(@"video path --> %@",videopath);

NSURL *movieURL = [NSURL fileURLWithPath:videopath];
AVURLAsset *avUrl = [AVURLAsset assetWithURL:movieURL];
CMTime time1 = [avUrl duration];  
int seconds = ceil(time1.value/time1.timescale);

1 个答案:

答案 0 :(得分:0)

试试这个

+ (void)convertVideoToLowQualityWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL successHandler:(void (^)())successHandler failureHandler:(void (^)(NSError *))failureHandler {
    if([[NSFileManager defaultManager] fileExistsAtURL:outputURL]) [[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) {
        if (exportSession.status == AVAssetExportSessionStatusCompleted) {
            successHandler();
        } else {
            NSError *error = [NSError errorWithDomain:domain code:code userInfo:userInfo]; 
            failureHandler(error); 
        }
     }];
}