iOS~視頻編碼轉換

在iOS中,攝像頭錄製的視頻是mov格式的,雖然mov兼容mp4,可是有些需求須要用到mp4格式的視頻文件。在翻邊了stackoverflow以後找到了一段轉換視頻格式的代碼。以此記錄一下。ui

 

AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:path] options:nil]; NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset]; if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality]) { AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough]; NSString *exportPath = [NSString stringWithFormat:@"%@/%@.mp4", [NSHomeDirectory() stringByAppendingString:@"/tmp"], @"1"]; exportSession.outputURL = [NSURL fileURLWithPath:exportPath]; NSLog(@"%@", exportPath); exportSession.outputFileType = AVFileTypeMPEG4; [exportSession exportAsynchronouslyWithCompletionHandler:^{ switch ([exportSession status]) { case AVAssetExportSessionStatusFailed: NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]); break; case AVAssetExportSessionStatusCancelled: NSLog(@"Export canceled"); break; case AVAssetExportSessionStatusCompleted: NSLog(@"轉換成功"); break; default: break; } }]; }
相關文章
相關標籤/搜索