如何在CMTimeMake的时标Int32中传递Float [英] How to pass Float in CMTimeMake's timescale Int32

查看:285
本文介绍了如何在CMTimeMake的时标Int32中传递Float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究CMTimeMake,以便为视频添加慢动作和快动作效果.在这里我们需要按视频比例除以实现快速效果,而要对视频比例乘以进行慢效果.

I am working on CMTimeMake in order to add slow and fast motion effect to Video. Where we have to divide by Video scale for Fast effect and multiply by Video scale for Slow effect.

这里是:

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)

现在按照我的要求,有一个Slider(介于0.1到2.0之间),用户可以在其中为慢速和快速效果选择特定的视频比例值.该值即将在Float中出现.

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. This Value is coming in Float.

我的问题是,当我在上面的代码中传递诸如0.8的Float值时,然后:

My problem is when I am passing my Float value like 0.8 in my above code, then:

let videoScaleFactor = Int64(0.8)//这会返回0

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

如何向其中返回精确值0.8?请告诉我.

How can I return exact value 0.8 into this? Please advise me.

推荐答案

您写道:

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

这很正常,因为根据定义,不能有十进制值.所以0.8 => 0.

That's normal, because by definition can't have decimal value. So 0.8 => 0.

请根据需要的精度使用Float(或Double).

Instead use a Float (or Double) depending on the precision you need.

所以我们尝试一下:

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)

这引起了另一个问题:

二进制运算符'/'不能应用于'CMTimeValue'(aka'Int64')和'Float'类型的操作数

Binary operator '/' cannot be applied to operands of type 'CMTimeValue' (aka 'Int64') and 'Float'

实际上,在Swift中,您无法像这样操作各种类型的Int/Float等.

Indeed in Swift you can't manipulate various types of Int/Float etc like that.

所以要解决此问题:

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)

现在您将Float与其他Float

但是

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

所以CMTimeMake(_:_:)等待一个Int64值,所以您会得到一个错误,因为Float(videoAsset.duration.value) / videoScaleFactor(对于第一个)正在返回一个Float,而该方法需要一个Int64.

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.

所以就做

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)

现在应该可以使用.

但是我不能离开那个代码.您的行很长,很难阅读.实际上,您只需修改CMTimeMake(_:_:)value参数.

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(_:_:).

让我们分解:

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)

这篇关于如何在CMTimeMake的时标Int32中传递Float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆