-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodingService.fs
More file actions
202 lines (140 loc) 路 5.23 KB
/
Copy pathEncodingService.fs
File metadata and controls
202 lines (140 loc) 路 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
锘縨odule TrickyCat.VideoEncoder.EncodingService
open System.IO
open MediaInfo
open MediaEncoder
let private searchPatterns = [| "*.mp4"; "*.ts"; "*.avi"; "*.wmv"; "*.mov" |]
let private getFiles' folderPath searchPattern : Result<string array, string> =
try
Ok <| Directory.GetFiles(folderPath, searchPattern, SearchOption.AllDirectories)
with
| e ->
e.Message
|> sprintf "Getting files (%s) from folder (%s) failed with: %s"
searchPattern folderPath
|> Error
let private getFiles log folderPath =
searchPatterns
|> Seq.fold
(fun filesAcc searchPattern ->
match getFiles' folderPath searchPattern with
| Ok fileNames -> fileNames
| Error e ->
log e
Array.empty<string>
|> Seq.append filesAcc
)
Seq.empty<string>
let private ensureFolderExists folderPath : Result<string, string> =
if Directory.Exists folderPath then
Ok folderPath
else
folderPath
|> sprintf "Folder does not exist: %s"
|> Error
let private endsWithSeparatorChar (s: string) =
if s.EndsWith(Path.DirectorySeparatorChar) then
s
else
sprintf "%s%c" s Path.DirectorySeparatorChar
let private normalizeFolder (s: string) = Path.TrimEndingDirectorySeparator s
let private getTargetFilePath sourceFolder targetFolder (sourceFilePath: string) =
let parent = Directory.GetParent(sourceFolder |> normalizeFolder).FullName |> endsWithSeparatorChar
Path.ChangeExtension(
Path.Combine(
targetFolder,
sourceFilePath.Substring(parent.Length)
),
".mp4")
let private createParentFolders filePath =
try
Directory.CreateDirectory(FileInfo(filePath).DirectoryName)
|> ignore
|> Ok
with
| e ->
e.Message
|> sprintf "Creation of parent folders for file [ %s ] failed with: %s"
filePath
|> Error
type private HandleOutcome = Encoded | Skipped
let private mapMediaInfoError<'a> : Result<'a, MediaInfoError> -> Result<'a, string> =
Result.mapError (function MediaInfoError e -> sprintf "[MediaInfoError]: %s" e)
let private mapMediaEncoderError<'a> : Result<'a, MediaEncoderError> -> Result<'a, string> =
Result.mapError
(fun e ->
sprintf "[MediaEncoderError]: Source file: [ %s ] Target file: [ %s ] Error: %s"
e.sourceFilePath e.targetFilePath e.error)
let private targetMediaFileAlreadyExists' sourceFilePath targetFilePath =
match targetMediaFileAlreadyExists sourceFilePath targetFilePath with
| Ok ok -> Ok ok
| Error _ -> Ok false
let private handleFile' log sourceFolder targetFolder sourceFilePath = result {
let targetFilePath = getTargetFilePath sourceFolder targetFolder sourceFilePath
targetFilePath
|> sprintf "Target file path: %s"
|> log
do! createParentFolders targetFilePath
let! targetIsOk = targetMediaFileAlreadyExists' sourceFilePath targetFilePath
if targetIsOk then
return Skipped
else
let! dimensions = getDimensions sourceFilePath |> mapMediaInfoError
let! encodedFilePath = encodeFile sourceFilePath targetFilePath dimensions |> mapMediaEncoderError
return Encoded
}
type private HandlingStat() = class
let encodedCount = ref 0
let skippedCount = ref 0
let failedCount = ref 0
member _.encoded () = encodedCount.Value <- encodedCount.Value + 1
member _.skipped () = skippedCount.Value <- skippedCount.Value + 1
member _.failed () = failedCount.Value <- failedCount.Value + 1
member _.logTo log =
sprintf "Handling Stat: encoded = %i skipped = %i failed = %i"
encodedCount.Value
skippedCount.Value
failedCount.Value
|> log
end
let private handleFile (stat: HandlingStat) log sourceFolder targetFolder sourceFilePath =
sourceFilePath
|> sprintf "Source file path: %s"
|> log
handleFile' log sourceFolder targetFolder sourceFilePath
|> Result.map (function
| Encoded -> log "Encoded"; stat.encoded()
| Skipped -> log "Skipped"; stat.skipped())
|> Result.mapError (sprintf "Failed: %s" >> log >> stat.failed)
|> ignore
let encode
log
(targetFolder: string)
(sourceFolder: string)
:
Result<unit, string> =
ensureFolderExists sourceFolder
|> Result.map
(fun sourceFolder ->
sourceFolder
|> sprintf "Processing folder: [ %s ]"
|> log
let filePaths = getFiles log sourceFolder |> Array.ofSeq
let n = filePaths.Length
let nLen = n.ToString().Length
let stat = HandlingStat()
filePaths
|> Array.iteri
(fun idx sourceFilePath ->
let prefix = sprintf "[ %0*i / %i ]\n" nLen (idx + 1) n
log prefix
handleFile
stat
(sprintf "%*c%s" prefix.Length ' ' >> log)
sourceFolder
targetFolder
sourceFilePath
log ""
)
stat.logTo log
)
|> Result.mapError (tee log)