Jat*_*tin 5 avfoundation ios avmutablecomposition avvideocomposition avasset
我有一个可以加载到 AVAsset 中的视频文件。我想更改播放此视频的速率并将其速度加倍,以便如果视频时长为 10 秒,则它会加快速度以在 5 秒内完成。
这是我正在尝试的代码,有人能告诉我我哪里出错了吗?
帧持续时间是我实现目标所需的属性。
AVAsset *anAsset = self.moviePlayer.currentItem.asset;
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:anAsset];
if ([compatiblePresets containsObject:AVAssetExportPreset640x480]) {
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
initWithAsset:anAsset presetName:AVAssetExportPreset640x480];
// Implementation continues.
NSString *tmpStr = [[aclip selectedTakeUrl] stringByReplacingOccurrencesOfString:@".m4v" withString:@""];
NSString *filePath = [NSString stringWithFormat:@"%@_applied.m4v", tmpStr];
exportSession.outputURL = [NSURL fileURLWithPath:filePath];
AVMutableVideoComposition *vidcomp = [AVMutableVideoComposition videoCompositionWithPropertiesOfAsset:anAsset];
vidcomp.frameDuration = CMTimeMake(1, 24);
//self.moviePlayer.currentItem.videoComposition = vidcomp;
exportSession.videoComposition = vidcomp;
//exportSession.videoComposition.frameDuration = CMTimeMake(1, 50);
// what is the song URL before loading startRecordingViewController?
NSLog(@"From Save settgins Choose Song -Song URL : %@", exportSession.outputURL);
// NSLog(@"start time%f, end time %f", CMTimeGetSeconds(self.startTime),CMTimeGetSeconds(self.endTime));
exportSession.outputFileType = AVFileTypeMPEG4;
//CMTimeRange range = CMTimeRangeMake(CMTimeMake(0.0, 1), CMTimeMake(diffTime/currentround,1));
// startTime and endTime is the duration that we need to save.
//exportSession.timeRange = range;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status]) {
case AVAssetExportSessionStatusCompleted:
NSLog(@"Export Completed");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"Export Waiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"Export Exporting");
break;
case AVAssetExportSessionStatusFailed:
{
NSError *error = [exportSession error];
NSLog(@"Export failed: %@", [error localizedDescription]);
break;
}
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
default:
break;
}
dispatch_async(dispatch_get_main_queue(), ^{
//[activityIndicator stopAnimating];
//CMTime range = CMTimeSubtract(self.endTime, self.startTime);
//NSUInteger theLength = (int)CMTimeGetSeconds(range);
//[ self.songPickedDelegate songPickerDidMakeSelection:self.songTitle :self.audioPath :theLength];
});
//[exportSession release];
}];
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用 -[AVMutableCompositionTrack scaleTimeRange:toDuration:] 将整个资产从 10 秒缩放到 5 秒。frameDuration 属性将简单地执行帧速率转换。
| 归档时间: |
|
| 查看次数: |
2596 次 |
| 最近记录: |