@@ -243,27 +243,22 @@ func mergeAssetWithSubtitles(videoAsset: AVURLAsset, subtitleAsset: AVURLAsset)
243
243
/// - duration: A `CMTime` value representing the total duration of the media.
244
244
/// This value must be valid for the calculation to work correctly.
245
245
/// - Returns: A `CMTime` value representing the resolved seek position within the media.
246
- func getSeekTime( for time: Double , duration : CMTime ) -> CMTime ? {
247
-
248
- guard duration. value != 0 else { return nil }
249
-
250
- let endTime = CMTimeGetSeconds ( duration)
251
- let seekTime : CMTime
252
-
253
- if time < 0 {
254
- // If the time is negative, seek to the start of the video
255
- seekTime = . zero
256
- } else if time >= endTime {
257
- // If the time exceeds the video duration, seek to the end of the video
258
- let endCMTime = CMTime ( seconds: endTime, preferredTimescale: duration. timescale)
259
- seekTime = endCMTime
260
- } else {
261
- // Otherwise, seek to the specified time
262
- let seekCMTime = CMTime ( seconds: time, preferredTimescale: duration. timescale)
263
- seekTime = seekCMTime
264
- }
265
-
266
- return seekTime
246
+ func getSeekTime( for time: Double , duration: CMTime ) -> CMTime ? {
247
+ guard duration. isValid,
248
+ duration. isNumeric,
249
+ duration. timescale != 0
250
+ else { return nil }
251
+
252
+ let endSeconds = CMTimeGetSeconds ( duration)
253
+ guard endSeconds. isFinite, endSeconds >= 0 else { return nil }
254
+
255
+ if time <= 0 { return . zero }
256
+ if time >= endSeconds { return duration }
257
+
258
+ let clamped = max ( 0 , min ( time, endSeconds) )
259
+
260
+ let scale = max ( Int32 ( 600 ) , duration. timescale)
261
+ return CMTime ( seconds: clamped, preferredTimescale: scale)
267
262
}
268
263
269
264
/// Creates an `AVPlayerItem` with optional subtitle merging.
0 commit comments