ContentView.swift
4.1 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
//
// ContentView.swift
// SherpaOnnxSubtitle
//
// Created by knight on 2023/9/23.
//
import AVKit
import MediaPlayer
import PhotosUI
import SwiftUI
struct ContentView: View {
@StateObject var subtitleViewModel = SubtitleViewModel()
var body: some View {
VStack {
VStack {
Text("SherpaOnnxSubtitle")
.font(.title)
VStack(alignment: .leading) {
Text("Audio format should be **mono** channel and **16khz** sample rate")
Text("You can convert file with the help of ffmpeg")
Text("```ffmpeg -i ./foo.mov -acodec pcm_s16le -ac 1 -ar 16000 foo.wav```")
}
}
.padding(.vertical)
PhotosPicker(
selection: $subtitleViewModel.selectedItem,
matching: .videos
) {
Label("Open Audio from Photo Library", systemImage: "photo")
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.background(.blue, in: .rect(cornerRadius: 8.0))
.foregroundColor(.white)
}
Button(action: {
subtitleViewModel.importNow = true
}, label: {
Text("Open Audio from Files")
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.background(.blue, in: .rect(cornerRadius: 8.0))
})
.foregroundColor(.white)
switch subtitleViewModel.loadState {
case .initial, .loaded(_), .done:
EmptyView()
case .loading:
ProgressView()
case .failed:
Text("Gen SRT failed")
}
}
.fileImporter(isPresented: $subtitleViewModel.importNow, allowedContentTypes: [.movie, .audio], onCompletion: handleImportCompletion)
.onChange(of: subtitleViewModel.importNow) { importNow in
if !importNow {
subtitleViewModel.restoreState()
}
}
.fileExporter(isPresented: $subtitleViewModel.exportNow,
document: subtitleViewModel.srtDocument, contentType: .srt,
defaultFilename: subtitleViewModel.srtName,
onCompletion: handleExportCompletion)
.task(id: subtitleViewModel.selectedItem) {
do {
if !subtitleViewModel.hasAudio {
return
}
subtitleViewModel.loadState = .loading
if let movie = try await subtitleViewModel.selectedItem?.loadTransferable(type: Audio.self) {
subtitleViewModel.loadState = .loaded(movie)
subtitleViewModel.generateSRT(from: movie.url)
} else {
subtitleViewModel.loadState = .failed
}
} catch {
subtitleViewModel.loadState = .failed
}
}
.padding()
}
private func handleImportCompletion(result: Result<URL, Error>) {
print("file import...")
switch result {
case let .success(file):
let accessing = file.startAccessingSecurityScopedResource()
defer {
if accessing {
file.stopAccessingSecurityScopedResource()
}
}
subtitleViewModel.generateSRT(from: file)
case let .failure(error):
print(error.localizedDescription)
subtitleViewModel.loadState = .failed
}
}
private func handleExportCompletion(result: Result<URL, any Error>) {
switch result {
case let .success(url):
print("audio export to: \(url)")
subtitleViewModel.loadState = .done
case let .failure(error):
print("export audio error: \(error.localizedDescription)")
subtitleViewModel.loadState = .failed
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}