avfoundation scaleTimeRange音频慢动作

时间:2014-02-05 19:45:20

标签: ios objective-c avfoundation

我有一个关于如何使用视频以慢动作制作音频的问题,我设法使视频以慢动作但声音不是。声音工作得很好并且在视频的一半中结束,我想让它在视频中播放速度很慢...继承我的代码。

AVMutableComposition  * mixComposition = [[AVMutableComposition alloc] init];
//Get the Asset file from bundle.
NSURL *myMovieURL = [[NSBundle mainBundle] URLForResource:@"Movie1" withExtension:@"m4v"];
//create avfoundation asset file.
AVURLAsset * movieAsset = [[AVURLAsset alloc] initWithURL:myMovieURL options:nil];

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                               preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compsitionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                              preferredTrackID:kCMPersistentTrackID_Invalid];
NSError *error = nil;

BOOL videoInsertResult = [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, movieAsset.duration)
                                                        ofTrack:[[movieAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                                         atTime:kCMTimeZero
                                                          error:&error];


BOOL adudioInsertResult = [compsitionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, movieAsset.duration)
                                                        ofTrack:[[movieAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                                                         atTime:kCMTimeZero
                                                          error:&error];

if (!videoInsertResult || nil != error) {
    //handle error
    return;
}

if (!adudioInsertResult || nil != error) {
    //handle error
    return;
}

double videoScaleFactor = 3.0;
CMTime videoDuration = movieAsset.duration;


[compositionVideoTrack scaleTimeRange:CMTimeRangeMake(kCMTimeZero, videoDuration)
                           toDuration:CMTimeMake(videoDuration.value*videoScaleFactor, videoDuration.timescale)];

[compsitionAudioTrack scaleTimeRange:CMTimeRangeMake(kCMTimeZero, videoDuration)
                           toDuration:CMTimeMake(videoDuration.value*videoScaleFactor, videoDuration.timescale)];


//export
AVAssetExportSession* assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition
                                                                     presetName:AVAssetExportPresetMediumQuality];


assetExport.outputURL = [self tempFileURL];
assetExport.outputFileType = AVFileTypeMPEG4;
assetExport.shouldOptimizeForNetworkUse = YES;
//assetExport.videoComposition = mixComposition;
[assetExport exportAsynchronouslyWithCompletionHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self exportDidFinish:assetExport];
    });
}];

0 个答案:

没有答案