iOS 视频技术点【经典】

来源:互联网 发布:移动测速软件 编辑:程序博客网 时间:2024/05/17 00:56
1.iOS 给视频添加背景音乐

- (void)addVideoMusic

{

    NSString *audiopath = [[NSBundle mainBundle]pathForResource:@"SIMPLE2" ofType:@"m4a"];

    NSString *videopath = [[NSBundle mainBundle]pathForResource:@"video" ofType:@"mov"];

     AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:audiopath] options:nil];

    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:videopath] options:nil];

        AVMutableComposition* mixComposition = [AVMutableComposition composition];

    AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio

                                                                                        preferredTrackID:kCMPersistentTrackID_Invalid];

   

    [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)

                                        ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudioobjectAtIndex:0]

                                         atTime:kCMTimeZero error:nil];

  AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo

                                                                                   preferredTrackID:kCMPersistentTrackID_Invalid];

    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)

                                   ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]

                                    atTime:kCMTimeZero error:nil];

    

    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition

                                                                          presetName:AVAssetExportPresetPassthrough];

  NSString* videoName = @"RongTian.mov";

NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];

    NSURL    *exportUrl = [NSURL fileURLWithPath:exportPath];

 if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])

    {

        [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];

    }

 _assetExport.outputFileType = @"com.apple.quicktime-movie";

    _assetExport.outputURL = exportUrl;

    _assetExport.shouldOptimizeForNetworkUse = YES;

    

    [_assetExport exportAsynchronouslyWithCompletionHandler:

     ^(void ) {

        

  }

     ];

}

2.iOS 从相册拿gif图或者视频 存放本地

   NSURL *imageRefURL = [info valueForKey:UIImagePickerControllerReferenceURL];

                ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary allocinit];

                void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *rtAsset) {

          if (rtAsset != nil) {

  ALAssetRepresentation *rep = [rtAsset defaultRepresentation];

                        Byte *imageBuffer = (Byte*)malloc(rep.size);

                        NSUInteger bufferSize = [rep getBytes:imageBuffer fromOffset:0.0 length:rep.size error:nil];

                        NSData *imageData = [NSData dataWithBytesNoCopy:imageBuffer length:bufferSize freeWhenDone:YES];

                        BOOL iswrite =  [manager createFileAtPath:[[RTMethod getVideoDirectory]stringByAppendingPathComponent:imageName] contents:imageDataattributes:nil];

                        

                        if (iswrite) {

                            NSLog(@"视频写入成功 ==  %@",[[RTMethod getVideoDirectory]stringByAppendingPathComponent:imageName] );

                        }else {

                            NSLog(@"视频写入失败 ");

                        }

                    }

                    else {

                        NSLog(@"视频文件不存在");

                    }

                };

         [assetLibrary assetForURL:videoURL

                              resultBlock:ALAssetsLibraryAssetForURLResultBlock

                             failureBlock:^(NSError *error){

                                 NSLog(@"视频写入失败");

                             }];


0 0
原创粉丝点击