1. 程式人生 > >iOS開發-進階:MOV格式視訊轉MP4格式

iOS開發-進階:MOV格式視訊轉MP4格式

AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:sourceUrl options:nil];

    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];

    NSLog(@"%@",compatiblePresets);

    if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) {

        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];

        NSDateFormatter *formater = [[NSDateFormatter alloc] init];//用時間給檔案全名,以免重複

        [formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];

        NSString * resultPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]];

        NSLog(@"resultPath = %@",resultPath);

        exportSession.outputURL = [NSURL fileURLWithPath:resultPath];

        exportSession.outputFileType = AVFileTypeMPEG4;

        exportSession.shouldOptimizeForNetworkUse = YES;

        [exportSession exportAsynchronouslyWithCompletionHandler:^(void)

         {

             switch (exportSession.status) {

                 case AVAssetExportSessionStatusUnknown:

                     NSLog(@"AVAssetExportSessionStatusUnknown");

                     break;

                 case AVAssetExportSessionStatusWaiting:

                     NSLog(@"AVAssetExportSessionStatusWaiting");

                     break;

                 case AVAssetExportSessionStatusExporting:

                     NSLog(@"AVAssetExportSessionStatusExporting");

                     break;

                 case AVAssetExportSessionStatusCompleted:

                     NSLog(@"AVAssetExportSessionStatusCompleted");

                     break;

                 case AVAssetExportSessionStatusFailed:

                     NSLog(@"AVAssetExportSessionStatusFailed");

                     break;

             }

         }];

    }