iphone ios CMTime CMTimeMake CMTimeMakeWithSeconds 意义和作用

使用到CMTime的时候感觉很奇怪,我要的是第几秒的东西,直接赋值第几秒就ok,何必用什么CMTime呢,感觉画蛇添足。不过经一段时间的编码,感觉这么写还是有道理的。

CMTime主要用在ios的AVFoundation的音视频编码中。看看定义:

http://developer.apple.com/library/mac/#documentation/CoreMedia/Reference/CMTime/Reference/reference.html

typedef struct
{
	CMTimeValue	value;		/*! @field value The value of the CMTime. value/timescale = seconds. */
	CMTimeScale	timescale;	/*! @field timescale The timescale of the CMTime. value/timescale = seconds.  */
	CMTimeFlags	flags;		/*! @field flags The flags, eg. kCMTimeFlags_Valid, kCMTimeFlags_PositiveInfinity, etc. */
	CMTimeEpoch	epoch;		/*! @field epoch Differentiates between equal timestamps that are actually different because
												 of looping, multi-item sequencing, etc.  
												 Will be used during comparison: greater epochs happen after lesser ones. 
												 Additions/subtraction is only possible within a single epoch,
												 however, since epoch length may be unknown/variable. */
} CMTime;

编码中,我们经常使用

CMTime firstframe=CMTimeMake(1,10);
CMTime lastframe=CMTimeMake(10, 10);



“CMTime可是專門用來表示影片時間用的類別,
他的用法為: CMTimeMake(time, timeScale)

time指的就是時間(不是秒),
而時間要換算成秒就要看第二個參數timeScale了.
timeScale指的是1秒需要由幾個frame構成(可以視為fps),
因此真正要表達的時間就會是 time / timeScale 才會是秒.”

上面的代码可以这么理解,视频的fps(帧率)是10,firstframe是第一帧的视频时间为0.1秒,lastframe是第10帧视频时间为1秒。

或者换种写法   CMTime curFrame = CMTimeMake(第几帧, 帧率)

看看另一篇博客的写法:http://blog.riaproject.com/objective-c/1745.html

这么看,

CMTime firstframe=CMTimeMake(32,16);
CMTime lastframe=CMTimeMake(48, 24);

这两个都表示2秒的时间。但是帧率是完全不同的。

关于帧率说一下题外话,通常帧率是多少呢?如下:

You frequently use a timescale of 600, since this is a common multiple of several commonly-used frame-rates: 24 frames per second (fps) for film, 30 fps for NTSC (used for TV in North America and Japan), and 25 fps for PAL (used for TV in Europe). Using a timescale of 600, you can exactly represent any number of frames in these systems.

这个地方有说明http://developer.apple.com/library/mac/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/05_MediaRepresentations.html#//apple_ref/doc/uid/TP40010188-CH2-SW1

CMTimeMakeWithSeconds 和CMTimeMake 区别在于,第一个函数的第一个参数可以是float,其他一样。




你可能感兴趣的:(ios,struct,iPhone,float)