// 獲取視頻第一幀
- (UIImage*) getVideoPreViewImage:(AVURLAsset*)asset
{
? ? AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
? ? // 設(shè)定縮略圖的方向
? ? // 如果不設(shè)定好啰,可能會在視頻旋轉(zhuǎn)90/180/270°時柑晒,獲取到的縮略圖是被旋轉(zhuǎn)過的缴守,而不是正向的
? ? gen.appliesPreferredTrackTransform = YES;
? ? gen.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
? ? // 設(shè)置圖片的最大size(分辨率)
? ? gen.maximumSize = CGSizeMake(300, 169);
? ? CMTime time = CMTimeMakeWithSeconds(0.0, 10); //取第0秒固耘,一秒鐘600幀
? ? NSError*error =nil;
? ? CMTimeactualTime;
? ? CGImageRefimage = [gencopyCGImageAtTime:timeactualTime:&actualTimeerror:&error];
? ? if(error) {
? ? ? ? UIImage*placeHoldImg = [UIImageimageNamed:@"付費頁"];
? ? ? ? returnplaceHoldImg;
? ? }
? ? UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
? ? CGImageRelease(image);
? ? returnthumb;
}
這個方法有時候會崩潰
//獲取m3u8視頻幀畫面
- (UIImage*)getPixelBufferForItem:(AVPlayerItem*)playerItem{
? ? AVPlayerItemVideoOutput *output = [[AVPlayerItemVideoOutput alloc] init];
? ? [playerItemaddOutput:output];
? ? CVPixelBufferRef ref =[output copyPixelBufferForItemTime:CMTimeMake(1000, 60) itemTimeForDisplay:nil];
? ? UIImage*image = [selfCVImageToUIImage:ref];
? ? returnimage;
}
//CVPixelBufferRef轉(zhuǎn)UIImage
- (UIImage*)CVImageToUIImage:(CVPixelBufferRef)imageBuffer{
? ? CVPixelBufferLockBaseAddress(imageBuffer, 0);
? ? void*baseAddress =CVPixelBufferGetBaseAddress(imageBuffer);
? ? size_t width = CVPixelBufferGetWidth(imageBuffer);
? ? size_t height = CVPixelBufferGetHeight(imageBuffer);
? ? size_tbufferSize =CVPixelBufferGetDataSize(imageBuffer);
? ? size_t bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0);
? ? CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
? ? CGDataProviderRefprovider =CGDataProviderCreateWithData(NULL, baseAddress, bufferSize,NULL);
? ? CGImageRef cgImage = CGImageCreate(width, height, 8, 32, bytesPerRow, rgbColorSpace, kCGImageAlphaNoneSkipFirst|kCGBitmapByteOrder32Little, provider, NULL, true, kCGRenderingIntentDefault);
? ? UIImage *image = [UIImage imageWithCGImage:cgImage];
? ? CGImageRelease(cgImage);
? ? CGDataProviderRelease(provider);
? ? CGColorSpaceRelease(rgbColorSpace);
? ? NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
? ? image = [UIImageimageWithData:imageData];
? ? CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
? ? returnimage;
}
//獲取視頻中的音頻素材
? ? [selfsetUpAndAddAudioAtPath:urlAssettoComposition:compositionstart:startTimedura:trackDurationoffset:CMTimeMake(14*44100,44100)andArray:audioMixParams];
? ? //創(chuàng)建一個可變的音頻混合
? ? AVMutableAudioMix *audioMix =[AVMutableAudioMix audioMix];
? ? audioMix.inputParameters =[NSArray arrayWithArray:audioMixParams];//從數(shù)組里取出處理后的音頻軌道參數(shù)
? ? //創(chuàng)建一個輸出
? ? AVAssetExportSession *exporter =[[AVAssetExportSession alloc]
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? initWithAsset:composition
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? presetName:AVAssetExportPresetAppleM4A];
? ? exporter.audioMix= audioMix;
? ? exporter.outputFileType=@"com.apple.m4a-audio";
? ? //視頻的聲音题篷,合成后存儲
? ? NSArray? *paths =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES);
? ? NSString*path=[paths objectAtIndex:0];
? ? //NSString *movDirectory = [path stringByAppendingPathComponent:@"/%@.m4a"];
? ? ? ? NSString* movDirectory =? [pathstringByAppendingPathComponent:
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [NSStringstringWithFormat:@"/%@.m4a",[selfgetCurrentTimes]]];
? ? if([[NSFileManager defaultManager]fileExistsAtPath:movDirectory]) {
? ? ? ? [[NSFileManager defaultManager]removeItemAtPath:movDirectory error:nil];
? ? }
? ? NSURL*exportURL =[NSURLfileURLWithPath:movDirectory];
? ? exporter.outputURL= exportURL;
? ? ? ? MAKAudioObject * object = [[MAKAudioObject alloc]init];
? ? ? ? object.url= movDirectory;
? ? ? ? object.name= [movDirectorysubstringFromIndex:movDirectory.length-19];
? ? ? ? object.localam? = [movDirectorysubstringFromIndex:movDirectory.length-19];
? ? [exporterexportAsynchronouslyWithCompletionHandler:^{
? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? ? ? NSLog(@"Export status not yet completed. Error: %@", exporter.error.description);
? ? ? ? ? ? ? ? ? ? if (exporter.status == AVAssetExportSessionStatusCompleted) {
? ? ? ? ? ? ? ? ? ? ? ? NSURL* outputURL = exporter.outputURL;
? ? ? ? ? ? ? ? ? ? ? ? AVURLAsset*songAsset = [AVURLAssetURLAssetWithURL:outputURLoptions:nil];
? ? ? ? ? ? ? ? ? ? ? ? NSData* data = [NSDatadataWithContentsOfFile:movDirectory];
? ? ? ? ? ? ? ? ? ? ? ? NSLog(@"data:%@",data);
? ? ? ? ? ? ? ? ? ? ? ? object.time=[NSStringstringWithFormat:@"%.2f",CMTimeGetSeconds(songAsset.duration)];
? ? ? ? ? ? ? ? ? ? ? ? NSNumber*size;
? ? ? ? ? ? ? ? ? ? ? ? [songAsset.URLgetResourceValue:&sizeforKey:NSURLFileSizeKeyerror:nil];
? ? ? ? ? ? ? ? ? ? ? ? NSLog(@"size is %.2f",[sizefloatValue]/(1024.0*1024.0));//size is 43.703005
? ? ? ? ? ? ? ? ? ? ? ? object.cunchu=[NSStringstringWithFormat:@"%.2fMb",[sizefloatValue]/(1024.0*1024.0)];
? ? ? ? ? ? ? ? ? ? ? ? if ( [[MAKAudioMusicDBManager sharedManager] insertAudioWith:object]) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [selfbackButtonclick];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? }];
-(void)setUpAndAddAudioAtPath:(AVURLAsset*)songAsset toComposition:(AVMutableComposition*)composition start:(CMTime)start dura:(CMTime)dura offset:(CMTime)offset andArray:(NSMutableArray*)audioMixParams{
? ? AVMutableCompositionTrack *track = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
? ? AVAssetTrack *sourceAudioTrack = [[songAsset tracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0];
? ? NSError*error =nil;
? ? BOOLok =NO;
? ? CMTimestartTime = start;
? ? CMTimetrackDuration = dura;
? ? CMTimeRangetRange =CMTimeRangeMake(startTime,trackDuration);
? ? //設(shè)置音量
? ? //AVMutableAudioMixInputParameters(輸入?yún)?shù)可變的音頻混合)
? ? //audioMixInputParametersWithTrack(音頻混音輸入?yún)?shù)與軌道)
? ? //AVMutableAudioMixInputParameters *trackMix = [AVMutableAudioMixInputParameters audioMixInputParametersWithTrack:track];
? ? //[trackMix setVolume:0.8f atTime:startTime];
? ? //素材加入數(shù)組
? ? //[audioMixParams addObject:trackMix];
? ? //Insert audio into track? //offsetCMTimeMake(0, 44100)
? ? ok =? [trackinsertTimeRange:tRangeofTrack:sourceAudioTrackatTime:kCMTimeInvaliderror:&error];
}
-(NSString*)getCurrentTimes{
? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
? ? // ----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時制,24小時制
? ? [formattersetDateFormat:@"YYYYMMdd_HHmmss"];
? ? //現(xiàn)在時間,你可以輸出來看下是什么格式
? ? NSDate*datenow = [NSDatedate];
? ? //----------將nsdate按formatter格式轉(zhuǎn)成nsstring
? ? NSString*currentTimeString = [formatterstringFromDate:datenow];
? ? NSLog(@"currentTimeString =? %@",currentTimeString);
? ? returncurrentTimeString;
}