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.
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. This Value is coming in Float.
My problem is when I am passing my Float value like 0.8 in my above code, then:
let videoScaleFactor = Int64(0.8) // this returns me 0
How can I return exact value 0.8 into this? Please advise me.
You wrote:
That's normal, because by definition can't have decimal value. So 0.8 => 0.
Instead use a
Float
(orDouble
) depending on the precision you need.So let's try it:
That rises another issue:
Indeed in Swift you can't manipulate various types of Int/Float etc like that.
So to fix it:
Now you multiply/divide
Float
with otherFloat
But
So
CMTimeMake(_:_:)
awaits for aInt64
value, so you get an error becauseFloat(videoAsset.duration.value) / videoScaleFactor
(for the first one) is returning aFloat
while the method wants an Int64.So just do
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 ofCMTimeMake(_:_:)
.Let's factorize:
Now, it's personal, by I'd prefer (nothing wrong with an extra line explicit):