简体   繁体   English

如何在CMTimeMake的时标Int32中传递Float

[英]How to pass Float in CMTimeMake's timescale Int32

I am working on CMTimeMake in order to add slow and fast motion effect to Video. 我正在CMTimeMake上工作,以便为视频添加慢动作和快动作效果。 Where we have to divide by Video scale for Fast effect and multiply by Video scale for Slow effect. 对于快速效果,我们必须按视频比例除以,对于慢速效果,我们必须按视频比例除以。

Here it is: 这里是:

let videoScaleFactor = Int64(2)

// Get the scaled video duration
let scaledVideoDuration = (mode == .Faster) ? CMTimeMake(videoAsset.duration.value / videoScaleFactor, videoAsset.duration.timescale) : CMTimeMake(videoAsset.duration.value * videoScaleFactor, videoAsset.duration.timescale)

Now as per my requirement, there is one Slider (between 0.1 to 2.0) where User will select the particular Video scale value for Slow and Fast effect. 现在,按照我的要求,有一个滑块(0.1到2.0之间),用户可以在其中为慢速和快速效果选择特定的视频比例值。 This Value is coming in Float. 此值即将浮现。

My problem is when I am passing my Float value like 0.8 in my above code, then: 我的问题是当我在上面的代码中传递诸如0.8的Float值时,则:

let videoScaleFactor = Int64(0.8) // this returns me 0 让videoScaleFactor = Int64(0.8)//返回我0

How can I return exact value 0.8 into this? 我该如何返回精确值0.8? Please advise me. 请建议我。

You wrote: 你写了:

let videoScaleFactor = Int64(0.8) // this returns me 0

That's normal, because by definition can't have decimal value. 这很正常,因为根据定义,不能有十进制值。 So 0.8 => 0. 因此0.8 => 0。

Instead use a Float (or Double ) depending on the precision you need. 而是根据需要的精度使用Float (或Double )。

So let's try it: 因此,让我们尝试一下:

let videoScaleFactor = Float(0.8)

// Get the scaled video duration
let scaledVideoDuration = (mode == .Faster) ? CMTimeMake(videoAsset.duration.value / videoScaleFactor, videoAsset.duration.timescale) : CMTimeMake(videoAsset.duration.value * videoScaleFactor, videoAsset.duration.timescale)

That rises another issue: 这引起了另一个问题:

Binary operator '/' cannot be applied to operands of type 'CMTimeValue' (aka 'Int64') and 'Float' 二进制运算符'/'不能应用于类型'CMTimeValue'(aka'Int64')和'Float'的操作数

Indeed in Swift you can't manipulate various types of Int/Float etc like that. 确实,在Swift中,您无法像这样操纵各种类型的Int / Float等。

So to fix it: 所以要解决它:

let videoScaleFactor = Float(0.8)

// Get the scaled video duration
let scaledVideoDuration = (mode == .Faster) ? CMTimeMake(Float(videoAsset.duration.value) / videoScaleFactor, videoAsset.duration.timescale) : CMTimeMake(Float(videoAsset.duration.value) * videoScaleFactor, videoAsset.duration.timescale)

Now you multiply/divide Float with other Float 现在您将Float与其他Float相乘/ Float

But

func CMTimeMake(_ value: Int64, _ timescale: Int32) -> CMTime

So CMTimeMake(_:_:) awaits for a Int64 value, so you get an error because Float(videoAsset.duration.value) / videoScaleFactor (for the first one) is returning a Float while the method wants an Int64. 所以CMTimeMake(_:_:)等待一个Int64值,所以您会得到一个错误,因为Float(videoAsset.duration.value) / videoScaleFactor (对于第一个)正在返回Float而该方法需要一个Int64。

So just do 所以做

let videoScaleFactor = Float(0.8)

// Get the scaled video duration
let scaledVideoDuration = (mode == .Faster) ? CMTimeMake(Int64(Float(videoAsset.duration.value) / videoScaleFactor), videoAsset.duration.timescale) : CMTimeMake(Int64(Float(videoAsset.duration.value) * videoScaleFactor), videoAsset.duration.timescale)

That should work now. 现在应该可以了。

But I can't leave with that code. 但是我不能离开那个代码。 Your line is quite long and it's hard to read. 您的行很长,很难阅读。 In fact, you just modify the value param of CMTimeMake(_:_:) . 实际上,您只需修改CMTimeMake(_:_:)value参数。

Let's factorize: 让我们分解:

let videoScaleFactor = Float(0.8)

// Get the scaled video duration
let scaledVideoDuration = CMTimeMake((mode == .Faster) ? Int64(Float(videoAsset.duration.value) / videoScaleFactor) : Int64(Float(videoAsset.duration.value) * videoScaleFactor), videoAsset.duration.timescale)

Now, it's personal, by I'd prefer (nothing wrong with an extra line explicit): 现在,这是个人的,我更喜欢(没有多余的行,这没错):

let videoScaleFactor = Float(0.8) 
let value = (mode == .Faster) ? Float(videoAsset.duration.value) / videoScaleFactor : Float(videoAsset.duration.value) * videoScaleFactor 
let scaledVideoDuration = CMTimeMake(Int64(value), videoAsset.duration.timescale)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM