GPUimageMovie 播放视频时间纠正

来源:互联网 发布:ubuntu 14.04卸载内核 编辑:程序博客网 时间:2024/05/21 01:31

http://blog.csdn.net/yi215415/article/details/52935570

由于解码需要时间 动态的减去上一次的延时时间 保证差值不会累积太大

GPUImage拥有简单的视频解析类GPUImageMovie

使用GPUImageMovie 播放视频,没有声音可是使用AVplayer同步播放音频达到效果。

GPUImageMovie 在使用 initWithAsset 与 initWithURL 实例化对象在startProcessing 
底层采用AVAssetReaderOutput从视频文件中一帧帧取出。 
在 设置playAtActualSpeed 是 YES 时。仅仅使用usleep延时来达到效果 
USLEEP受机型芯片,延时差异也较大。在模拟器上 这样播放时音频和视频相差是在是太大。延时时间也不浮动的 
因此还需要做个简单的延时差异纠正

 if (_playAtActualSpeed)            {                // Do this outside of the video processing queue to not slow that down while waiting                CMTime currentSampleTime = CMSampleBufferGetOutputPresentationTimeStamp(sampleBufferRef);                CMTime differenceFromLastFrame = CMTimeSubtract(currentSampleTime, previousFrameTime);                NSLog(@"%lld  %d",currentSampleTime.value,currentSampleTime.timescale);                if ( differenceFromLastFrame.value > 0 ) {                    CFAbsoluteTime currentActualTime = CFAbsoluteTimeGetCurrent();                    CGFloat frameTimeDifference = CMTimeGetSeconds(differenceFromLastFrame);                    CGFloat actualTimeDifference = currentActualTime - previousActualFrameTime;                    if (frameTimeDifference > actualTimeDifference ){                        CGFloat difTime = (frameTimeDifference - actualTimeDifference) - delayoOffsetTime;                        if(difTime > 0){                            double time = 1000000.0 * difTime;                            usleep(time);                        }                        delayoOffsetTime =  CFAbsoluteTimeGetCurrent() - currentActualTime - difTime;                        if (delayoOffsetTime < 0) {                            delayoOffsetTime = 0;                        }                        NSLog(@"date:%f   %f  dif:%f  difTime:%f",frameTimeDifference,actualTimeDifference,delayoOffsetTime,difTime);                    }                    previousFrameTime = currentSampleTime;                    previousActualFrameTime = CFAbsoluteTimeGetCurrent();                }            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

增加了delayoOffsetTime属性 记录 差异变化 下一帧时进行修正。 
在iphone6s 上音频和视频播放最终播放完,音视频播放时间差异仅在0.1秒左右。


原创粉丝点击