[英]What is the best way to transcode a video in objective-c (iOS)?
我想,根據我的應用程序中的設備和設置,將視頻轉碼為特定的視頻格式。 例如,如果用戶有iPhone 4S並在我的應用程序中選擇中等設置,我想在開始處理之前將視頻轉換為540p。 如果他選擇高,那么我想轉碼為720p。
我可以逐幀讀取視頻,調整大小並保存到光盤但這似乎不是很有效。 什么是最簡單,最快速的方式來轉換我可以提供給我的視頻處理庫的視頻?
我已嘗試在我的UIImagePickerController上使用videoQuality設置,但似乎它不起作用,即使我將其設置為UIImagePickerControllerQualityTypeIFrame960x540我的視頻出現為720p(640x480正在工作,但我需要更細粒度)。
您可能希望查看AVAssetExportSession,這使得重新編碼視頻變得相當簡單。 我認為它也可以像AVFoundation的其余部分一樣支持硬件:
請注意,它永遠不會使視頻大於現有視頻,因此無法保證獲得您請求的輸出大小。 假設您有一個ALAsset實例,以下代碼可能是您想要的開頭:
- (void)transcodeLibraryVideo:(ALAsset *)libraryAsset
toURL:(NSURL *)fileURL
withQuality:(NSString *quality) {
// get a video asset for the original video file
AVAsset *asset = [AVAsset assetWithURL:
[NSURL URLWithString:
[NSString stringWithFormat:@"%@",
[[libraryAsset defaultRepresentation] url]]]];
// see if it's possible to export at the requested quality
NSArray *compatiblePresets = [AVAssetExportSession
exportPresetsCompatibleWithAsset:asset];
if ([compatiblePresets containsObject:quality]) {
// set up the export
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
initWithAsset:asset presetName:quality];
exportSession.outputURL = fileURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
// run the export
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status]) {
case AVAssetExportSessionStatusFailed:
//TODO: warn of failure
break;
case AVAssetExportSessionStatusCancelled:
//TODO: warn of cancellation
break;
default:
//TODO: do whatever is next
break;
}
[exportSession release];
}];
}
else {
//TODO: warn that the requested quality is not available
}
}
例如,您可能希望為540p傳遞AVAssetExportPreset960x540的質量,為720p傳遞AVAssetExportPreset1280x720的質量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.