天天看點

iOS視訊拍攝壓縮上傳和播放

iOS視訊拍攝壓縮上傳和播放

視訊分享

想實作iPhone手機拍攝一段視訊,上傳到雲端分享給手機好友。

技術實作

視訊壓縮。iPhone拍攝的視訊很大,幾秒鐘就幾十兆檔案了,需要做壓縮,原來考慮用ffmpeg,安裝起來比較複雜,網上也有很多教程,不過在通過appstore稽核的時候有一定風險。

首先是點選開始錄制的代碼:

    if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];

        imagePicker.videoQuality =UIImagePickerControllerQualityTypeIFrame960x540;//視訊品質設定

        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        imagePicker.delegate = self;

        imagePicker.allowsEditing = YES;

        imagePicker.videoMaximumDuration = 300.0f;//設定最長錄制5分鐘

        imagePicker.mediaTypes = [NSArray arrayWithObject:@"public.movie"];

        [self presentModalViewController:imagePicker animated:YES];

    }

錄制完之後的回調方法:

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

{

    NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];

    NSString *appDocumentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];

    NSURL *uploadURL = [NSURL fileURLWithPath:[[appDocumentPathstringByAppendingPathComponent:[self dateString]]stringByAppendingString:@".mp4"]];

    // Compress movie first

    [self convertVideoToLowQuailtyWithInputURL:url outputURL:uploadURL];

    [self dismissModalViewControllerAnimated:YES];

    [picker release];

}

iOS視訊拍攝壓縮上傳和播放
iOS視訊拍攝壓縮上傳和播放