第49月第16天 iOS CIImage 旋转 iOS :视频嵌入srt字幕方式一
1.
- (void)dealWithSampleBuffer:(CMSampleBufferRef)buffer { CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(buffer); CIImage *ciimage = [CIImage imageWithCVPixelBuffer:pixelBuffer]; size_t width = CVPixelBufferGetWidth(pixelBuffer); size_t height = CVPixelBufferGetHeight(pixelBuffer); // 旋转的方法 CIImage *wImage = [ciimage imageByApplyingCGOrientation:kCGImagePropertyOrientationLeft]; CIImage *newImage = [wImage imageByApplyingTransform:CGAffineTransformMakeScale(0.5, 0.5)]; CVPixelBufferLockBaseAddress(pixelBuffer, 0); CVPixelBufferRef newPixcelBuffer = nil; CVPixelBufferCreate(kCFAllocatorDefault, height * 0.5, width * 0.5, kCVPixelFormatType_32BGRA, nil, &newPixcelBuffer); [_ciContext render:newImage toCVPixelBuffer:newPixcelBuffer]; // CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); CMVideoFormatDescriptionRef videoInfo = nil; CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, newPixcelBuffer, &videoInfo); CMTime duration = CMSampleBufferGetDuration(buffer); CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(buffer); CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(buffer); CMSampleTimingInfo sampleTimingInfo; sampleTimingInfo.duration = duration; sampleTimingInfo.presentationTimeStamp = presentationTimeStamp; sampleTimingInfo.decodeTimeStamp = decodeTimeStamp; // CMSampleBufferRef newSampleBuffer = nil; CMSampleBufferCreateForImageBuffer(kCFAllocatorMalloc, newPixcelBuffer, true, nil, nil, videoInfo, &sampleTimingInfo, &newSampleBuffer); // 对新buffer做处理 // release CVPixelBufferRelease(newPixcelBuffer); CFRelease(newSampleBuffer); }
https://www.jianshu.com/p/205083b93c1c
2.iOS :视频嵌入srt字幕方式一
AVAssetReader读取视频中的appendPixelBuffer,把它转换为UIImage,根据时间节点把字幕嵌入到UIImage中,这样一张图片就有了文字,然后再转换回CVPixelBufferRef,再用AVAssetWriterInputPixelBufferAdaptor的appendPixelBuffer方法写入一个新的视频流文件中,最终生成一个带有字幕的视频。
https://www.jianshu.com/p/72895188a54e
iOS :视频嵌入srt字幕方式二
根据时间节点在videoLayer上插入多个CATextLayer
CATextLayer默认是透明的,需要使用动画在对应的时间节点上显示CATextLayer,对它们设置透明度1.0,这样就能生成一个带字幕的视频了。
相比使用AVAssetReader+AVAssetWriter,这种方式代码简单,不需要考虑内存泄漏,相比使用AVAssetRead+AVAssetWriter方式性能提升很多
CATextLayer默认是透明的,需要使用动画在对应的时间节点上显示CATextLayer,对它们设置透明度1.0,这样就能生成一个带字幕的视频了。
相比使用AVAssetReader+AVAssetWriter,这种方式代码简单,不需要考虑内存泄漏,相比使用AVAssetRead+AVAssetWriter方式性能提升很多
+ (void)addSubtitles:(NSArray*)subtitles naturalSize:(CGSize)naturalSize mainCompositionInst:(AVMutableVideoComposition*)mainCompositionInst{ CALayer *parentLayer = [CALayer layer]; CALayer *videoLayer = [CALayer layer]; parentLayer.frame = CGRectMake(0, 0, naturalSize.width, naturalSize.height); videoLayer.frame = CGRectMake(0, 0, naturalSize.width, naturalSize.height); [parentLayer addSublayer:videoLayer]; //开始添加字幕图层 [subtitles enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { float begin = [subtitles[idx][@"begin"] floatValue]; float end = [subtitles[idx][@"end"] floatValue]; NSString* subtitle = subtitles[idx][@"subtitle"]; CATextLayer * textLayer = [CATextLayer layer]; textLayer.opacity = 0;//优先透明 textLayer.backgroundColor = [UIColor redColor].CGColor; textLayer.string = subtitle; textLayer.frame = CGRectMake(0, 0, naturalSize.width, 80); textLayer.alignmentMode = kCAAlignmentCenter; //添加动画 [textLayer addAnimation:[ccSubtitleComposition addAnimationWithBegin:begin end:end] forKey:@"animateOpacity"]; [parentLayer addSublayer:textLayer]; mainCompositionInst.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer]; }]; }
https://github.com/qw9685/srt-2-
https://www.jianshu.com/p/e372a7b98b29