mirror of
https://github.com/cranci1/Sora.git
synced 2026-05-19 16:21:49 +00:00
should work with subtitles now
This commit is contained in:
parent
4c6cabca59
commit
79bd797240
1 changed files with 25 additions and 11 deletions
|
|
@ -13,13 +13,13 @@ class DownloadManager {
|
||||||
|
|
||||||
private init() {}
|
private init() {}
|
||||||
|
|
||||||
/// Downloads and converts an HLS stream to MP4, or downloads an MP4 stream normally.
|
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - url: The stream URL (either .m3u8 or .mp4).
|
/// - url: The stream URL (either .m3u8 or .mp4).
|
||||||
/// - title: The title used for creating the folder.
|
/// - title: The title used for creating the folder.
|
||||||
/// - episode: The episode number used for naming the output file.
|
/// - episode: The episode number used for naming the output file.
|
||||||
|
/// - subtitleURL: An optional URL for the subtitle file (expects a .srt or .vtt file). (should work but not sure tbh).
|
||||||
/// - completion: Completion handler with a Bool indicating success and the URL of the output file.
|
/// - completion: Completion handler with a Bool indicating success and the URL of the output file.
|
||||||
func downloadAndConvertHLS(from url: URL, title: String, episode: Int, completion: @escaping (Bool, URL?) -> Void) {
|
func downloadAndConvertHLS(from url: URL, title: String, episode: Int, subtitleURL: URL? = nil, completion: @escaping (Bool, URL?) -> Void) {
|
||||||
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
|
guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
|
||||||
completion(false, nil)
|
completion(false, nil)
|
||||||
return
|
return
|
||||||
|
|
@ -65,18 +65,32 @@ class DownloadManager {
|
||||||
}
|
}
|
||||||
task.resume()
|
task.resume()
|
||||||
} else if fileExtension == "m3u8" {
|
} else if fileExtension == "m3u8" {
|
||||||
let ffmpegCommand = [
|
|
||||||
"ffmpeg",
|
|
||||||
"-threads", "0",
|
|
||||||
"-i", url.absoluteString,
|
|
||||||
"-c", "copy",
|
|
||||||
outputFileURL.path
|
|
||||||
]
|
|
||||||
|
|
||||||
DispatchQueue.global(qos: .background).async {
|
DispatchQueue.global(qos: .background).async {
|
||||||
|
var ffmpegCommand = ["ffmpeg", "-threads", "0", "-i", url.absoluteString]
|
||||||
|
|
||||||
|
if let subtitleURL = subtitleURL {
|
||||||
|
do {
|
||||||
|
let subtitleData = try Data(contentsOf: subtitleURL)
|
||||||
|
let subtitleFileExtension = subtitleURL.pathExtension.lowercased()
|
||||||
|
if subtitleFileExtension != "srt" && subtitleFileExtension != "vtt" {
|
||||||
|
Logger.shared.log("❌ Unsupported subtitle format: \(subtitleFileExtension)")
|
||||||
|
}
|
||||||
|
let subtitleFileName = "\(title)_Episode\(episode).\(subtitleFileExtension)"
|
||||||
|
let subtitleLocalURL = folderURL.appendingPathComponent(subtitleFileName)
|
||||||
|
try subtitleData.write(to: subtitleLocalURL)
|
||||||
|
ffmpegCommand.append(contentsOf: ["-i", subtitleLocalURL.path])
|
||||||
|
ffmpegCommand.append(contentsOf: ["-c", "copy", "-c:s", "mov_text", outputFileURL.path])
|
||||||
|
} catch {
|
||||||
|
Logger.shared.log("❌ Subtitle download failed: \(error)")
|
||||||
|
ffmpegCommand.append(contentsOf: ["-c", "copy", outputFileURL.path])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ffmpegCommand.append(contentsOf: ["-c", "copy", outputFileURL.path])
|
||||||
|
}
|
||||||
|
|
||||||
let success = ffmpeg(ffmpegCommand)
|
let success = ffmpeg(ffmpegCommand)
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
if (success == 0) {
|
if success == 0 {
|
||||||
Logger.shared.log("✅ Conversion successful: \(outputFileURL)")
|
Logger.shared.log("✅ Conversion successful: \(outputFileURL)")
|
||||||
completion(true, outputFileURL)
|
completion(true, outputFileURL)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue