iOS 视频技术点【经典】【值得研究】

   闲话少说,今天有点空闲时间,分享一点技术点,

QQ群2:198952330【未满】【注意】:非视频技术谢绝加入。本人的业余网站 http://zhece.com    欢迎来访
 
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:AVMediaTypeAudio] objectAtIndex: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 alloc] init];

                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:imageData attributes:nil];

                        

                        if (iswrite) {

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

                        }else {

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

                        }

                    }

                    else {

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

                    }

                };

         [assetLibrary assetForURL:videoURL

                              resultBlock:ALAssetsLibraryAssetForURLResultBlock

                             failureBlock:^(NSError *error){

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

                             }];

 
 
3.iOS 有关视频旋转90度的问题【解决】
 
 

http://blog.sina.com.cn/s/blog_801997310101ixv9.html

 

posted @   codeTao  阅读(239)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示