• 第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方式性能提升很多
    + (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
  • 相关阅读:
    sql2000如何完美压缩.mdf文件
    SQL Server读懂语句运行的统计信息 SET STATISTICS TIME IO PROFILE ON
    Sql Server性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON
    理解性能的奥秘——应用程序中慢,SSMS中快(6)——SQL Server如何编译动态SQL
    理解性能的奥秘——应用程序中慢,SSMS中快(5)——案例:如何应对参数嗅探
    理解性能的奥秘——应用程序中慢,SSMS中快(4)——收集解决参数嗅探问题的信息
    理解性能的奥秘——应用程序中慢,SSMS中快(3)——不总是参数嗅探的错
    理解性能的奥秘——应用程序中慢,SSMS中快(2)——SQL Server如何编译存储过程
    巧用Map缓存提升"翻译"速度
    pt-archiver配置自动归档
  • 原文地址:https://www.cnblogs.com/javastart/p/13824600.html
Copyright © 2020-2023  润新知