@@ -12,20 +12,49 @@ import CoreImage
12
12
#endif
13
13
14
14
/// Retrieves an `AVURLAsset` based on specified video settings.
15
- /// - Parameter settings: The `VideoSettings` object containing details like name and extension of the video .
16
- /// - Returns: An optional `AVURLAsset`. Returns `nil` if the video cannot be located either by URL or in the app bundle.
15
+ /// - Parameter settings: A `VideoSettings` object containing the video name and extension.
16
+ /// - Returns: An optional `AVURLAsset`. Returns `nil` if a valid URL cannot be created or the file cannot be found in the bundle.
17
17
func assetFor( _ settings: VideoSettings ) -> AVURLAsset ? {
18
18
let name = settings. name
19
- let ext = settings. ext
19
+ // If the name already includes an extension, use that; otherwise, use `settings.ext`.
20
+ let ext = extractExtension ( from: name) ?? settings. ext
20
21
21
- // Attempt to create a URL directly from the provided video name string
22
+ // Leverage the common helper to construct the `AVURLAsset`.
23
+ return assetFrom ( name: name, fileExtension: ext)
24
+ }
25
+
26
+ /// Retrieves an `AVURLAsset` for the subtitles specified in `VideoSettings`.
27
+ /// - Parameter settings: A `VideoSettings` object containing the subtitle file name.
28
+ /// - Returns: An optional `AVURLAsset` for the subtitle file, or `nil` if `subtitles` is empty or cannot be found.
29
+ func subtitlesAssetFor( _ settings: VideoSettings ) -> AVURLAsset ? {
30
+ let subtitleName = settings. subtitles
31
+ // If no subtitle name is provided, early return `nil`.
32
+ guard !subtitleName. isEmpty else {
33
+ return nil
34
+ }
35
+
36
+ // Use a default `.vtt` extension for subtitles.
37
+ return assetFrom ( name: subtitleName, fileExtension: " vtt " )
38
+ }
39
+
40
+ /// A common helper that attempts to build an `AVURLAsset` from a given name and optional file extension.
41
+ /// - Parameters:
42
+ /// - name: The base file name or a URL string.
43
+ /// - fileExtension: An optional file extension to be appended if `name` isn't a valid URL.
44
+ /// - Returns: An optional `AVURLAsset`, or `nil` if neither a valid URL nor a local resource file is found.
45
+ fileprivate func assetFrom( name: String , fileExtension: String ? ) -> AVURLAsset ? {
46
+ // Attempt to create a valid URL from the provided string.
22
47
if let url = URL . validURLFromString ( name) {
23
48
return AVURLAsset ( url: url)
24
- // If direct URL creation fails, attempt to locate the video in the main bundle using the name and extension
25
- } else if let fileUrl = Bundle . main. url ( forResource: name, withExtension: extractExtension ( from: name) ?? ext) {
49
+ }
50
+
51
+ // If not a valid URL, try to locate the file in the main bundle with the specified extension.
52
+ if let fileExtension = fileExtension,
53
+ let fileUrl = Bundle . main. url ( forResource: name, withExtension: fileExtension) {
26
54
return AVURLAsset ( url: fileUrl)
27
55
}
28
56
57
+ // If all attempts fail, return `nil`.
29
58
return nil
30
59
}
31
60
@@ -106,26 +135,6 @@ internal func handleVideoComposition(request: AVAsynchronousCIImageFilteringRequ
106
135
request. finish ( with: currentImage, context: nil )
107
136
}
108
137
109
- /// Retrieves an `AVURLAsset` for the subtitles specified in `VideoSettings`.
110
- /// - Parameter settings: The `VideoSettings` object containing details about the subtitle file (e.g., name and extension).
111
- /// - Returns: An optional `AVURLAsset` for the subtitle file, or `nil` if the file does not exist.
112
- func subtitlesFor( _ settings: VideoSettings ) -> AVURLAsset ? {
113
- let name = settings. subtitles
114
- let ext = " vtt "
115
-
116
- if name. isEmpty { return nil }
117
-
118
- // Attempt to create a URL directly from the provided video name string
119
- if let url = URL . validURLFromString ( name) {
120
- return AVURLAsset ( url: url)
121
- // If direct URL creation fails, attempt to locate the video in the main bundle using the name and extension
122
- } else if let fileUrl = Bundle . main. url ( forResource: name, withExtension: ext) {
123
- return AVURLAsset ( url: fileUrl)
124
- }
125
-
126
- return nil
127
- }
128
-
129
138
/// Merges a video asset with an external WebVTT subtitle file into an AVMutableComposition.
130
139
/// Returns a new AVAsset that has both the video/audio and subtitle tracks.
131
140
///
0 commit comments