scaleTimeRange(timeRange: CMTimeRange, toDuration duration: CMTime)
방법은 비디오에 슬로우 모션 효과를 적용하려는 경우 매우 효과적입니다.비디오의 일부분 만 ScaleTimeRange
그러나 전체 비디오의 지속 시간에 적용하면 만 작동한다는 것을 알았습니다. 임의의 timeRange (예 : CMTimeRangeMake(_ start: 2, duration: 3)
이 전달되면 메서드가 전혀 작동하지 않는 것 같습니다.
Q 1가)에이 scaleTimeRange
방법을 적용 할 수있는 방법이 있나요 - 또는 MP4 비디오를 내보낼 때 즉, 그것은 (0시 0분 5초 0시 0분 2초에서) 원하는 슬로우 모션 효과가 없습니다 비디오의 일부분 만? 그렇다면 어떻게 할 수 있습니까?
Q2) 그렇지 않은 경우이 슬로우 모션 효과를 동영상의 일부에만 어떻게 적용 할 수 있습니까? 다른 방법이 있습니까?
CODE :
var asset: AVAsset?
func setupAsset(){
let videoAsset = AVURLAsset(url: Bundle.main.url(forResource: "Sample", withExtension: "mp4")!)
let comp = AVMutableComposition()
let videoAssetSourceTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo).first! as AVAssetTrack
let videoCompositionTrack = comp.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
do {
try videoCompositionTrack.insertTimeRange(
CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(9 , 600)),
of: videoAssetSourceTrack,
at: kCMTimeZero)
let videoScaleFactor = Int64(3.0)
let videoDuration: CMTime = videoAsset.duration
let tstStartTime = CMTime(value: 2, timescale: videoDuration.timescale)
let tstDuration = CMTime(value: 1 , timescale: videoDuration.timescale)
//1. Applies slow motion correctly (to entire video)
videoCompositionTrack.scaleTimeRange(CMTimeRangeMake(kCMTimeZero , videoDuration), toDuration: CMTimeMake(videoDuration.value * videoScaleFactor, videoDuration.timescale))
//2. Replace with 1 , the exported video plays as is with no slow motion effect
videoCompositionTrack.scaleTimeRange(CMTimeRangeMake(kCMTimeZero , tstDuration), toDuration: CMTimeMake(tstDuration.value * videoScaleFactor, videoDuration.timescale))
// 3. Replace with 1, unexpected behaviour : video only displays first frame for CMTimeMakes's value then proceeds to play video normally.
videoCompositionTrack.scaleTimeRange(CMTimeRangeMake(tstStartTime , tstDuration), toDuration: CMTimeMake(tstDuration.value * videoScaleFactor, videoDuration.timescale))
videoCompositionTrack.preferredTransform = videoAssetSourceTrack.preferredTransform
}catch { print(error) }
asset = comp
}
를 "방법은 전혀 작동하지 않는 것?" – shallowThought
비디오는 성공적으로 저장되지만 슬로우 모션 효과는 없습니다. 내 보낸 mp4 파일은 소스 mp4 파일과 동일하며 프레임 속도가 동일합니다. –
'CMTime' 값은 무엇입니까? 관련 코드를 게시 할 수 있습니까? – shallowThought