Generate SRT from audio (#341)

This commit is contained in:
yujinqiu
2023-09-25 16:36:16 +08:00
committed by GitHub
parent 552a267c23
commit 9091917eab
19 changed files with 984 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
//
// Audio.swift
// SherpaOnnxSubtitle
//
// Created by knight on 2023/9/23.
//
import SwiftUI
struct Audio: Transferable {
let url: URL
static var transferRepresentation: some TransferRepresentation {
FileRepresentation(contentType: .movie) { movie in
SentTransferredFile(movie.url)
} importing: { received in
let copy = URL.documentsDirectory.appending(path: "audio.wav")
if FileManager.default.fileExists(atPath: copy.path()) {
try FileManager.default.removeItem(at: copy)
}
try FileManager.default.copyItem(at: received.file, to: copy)
return Self(url: copy)
}
}
}

View File

@@ -0,0 +1,32 @@
//
// Document.swift
// YPlayer
//
// Created by knight on 2023/6/5.
//
import SwiftUI
import UniformTypeIdentifiers
struct Document: FileDocument {
static var readableContentTypes = [UTType.srt]
static var writableContentTypes = [UTType.srt]
var data: Data?
init(data: Data?) {
self.data = data
}
init(configuration: ReadConfiguration) throws {
if let data = configuration.file.regularFileContents {
self.data = data
}
}
func fileWrapper(configuration _: WriteConfiguration) throws -> FileWrapper {
guard let data = data else {
throw ExportError.fileNotFound
}
return FileWrapper(regularFileWithContents: data)
}
}

View File

@@ -0,0 +1,12 @@
//
// Errors.swift
// YPlayer
//
// Created by knight on 2023/8/26.
//
import Foundation
enum ExportError: String, Error {
case fileNotFound = "export file not found"
}

View File

@@ -0,0 +1,31 @@
//
// SpeechSegment.swift
// SherpaOnnxSubtitle
//
// Created by knight on 2023/9/23.
//
import Foundation
class SpeechSegment: CustomStringConvertible {
let start: Float
let end: Float
let text: String
init(start: Float, duration: Float, text: String) {
self.start = start
end = start + duration
self.text = text
}
public var description: String {
var s: String
s = TimeInterval(start).hourMinuteSecondMS
s += " --> "
s += TimeInterval(end).hourMinuteSecondMS
s += "\n"
s += text
return s
}
}