mirror of
https://github.com/yattee/yattee.git
synced 2024-12-22 13:33:42 +00:00
improved subtitle handling
- fix subtitle disabling not working - make subtitle adding/removing async - make subtitle menu non blocking Signed-off-by: Toni Förster <toni.foerster@gmail.com>
This commit is contained in:
parent
8596ee8811
commit
3339e8cb1f
@ -23,15 +23,14 @@ final class MPVBackend: PlayerBackend {
|
|||||||
|
|
||||||
var stream: Stream?
|
var stream: Stream?
|
||||||
var video: Video?
|
var video: Video?
|
||||||
var captions: Captions? { didSet {
|
var captions: Captions? {
|
||||||
guard let captions else {
|
didSet {
|
||||||
if client?.areSubtitlesAdded == true {
|
Task {
|
||||||
client?.removeSubs()
|
await handleCaptionsChange()
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
addSubTrack(captions.url)
|
}
|
||||||
}}
|
|
||||||
var currentTime: CMTime?
|
var currentTime: CMTime?
|
||||||
|
|
||||||
var loadedVideo = false
|
var loadedVideo = false
|
||||||
@ -617,10 +616,14 @@ final class MPVBackend: PlayerBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addSubTrack(_ url: URL) {
|
func addSubTrack(_ url: URL) {
|
||||||
if client?.areSubtitlesAdded == true {
|
Task {
|
||||||
client?.removeSubs()
|
if let areSubtitlesAdded = client?.areSubtitlesAdded {
|
||||||
|
if await areSubtitlesAdded() {
|
||||||
|
await client?.removeSubs()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await client?.addSubTrack(url)
|
||||||
}
|
}
|
||||||
client?.addSubTrack(url)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setVideoToAuto() {
|
func setVideoToAuto() {
|
||||||
@ -684,6 +687,17 @@ final class MPVBackend: PlayerBackend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func handleCaptionsChange() async {
|
||||||
|
guard let captions else {
|
||||||
|
if let isSubtitlesAdded = client?.areSubtitlesAdded, await isSubtitlesAdded() {
|
||||||
|
await client?.removeSubs()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
addSubTrack(captions.url)
|
||||||
|
}
|
||||||
|
|
||||||
private func handlePropertyChange(_ name: String, _ property: mpv_event_property) {
|
private func handlePropertyChange(_ name: String, _ property: mpv_event_property) {
|
||||||
switch name {
|
switch name {
|
||||||
case "pause":
|
case "pause":
|
||||||
|
@ -349,21 +349,15 @@ final class MPVClient: ObservableObject {
|
|||||||
return Int(fps.rounded())
|
return Int(fps.rounded())
|
||||||
}
|
}
|
||||||
|
|
||||||
var areSubtitlesAdded: Bool {
|
func areSubtitlesAdded() async -> Bool {
|
||||||
guard !mpv.isNil else { return false }
|
guard !mpv.isNil else { return false }
|
||||||
|
|
||||||
// Retrieve the number of tracks
|
let trackCount = await Task(operation: { getInt("track-list/count") }).value
|
||||||
let trackCount = getInt("track-list/count")
|
|
||||||
guard trackCount > 0 else { return false }
|
guard trackCount > 0 else { return false }
|
||||||
|
|
||||||
for index in 0 ..< trackCount {
|
for index in 0 ..< trackCount {
|
||||||
// Get the type of each track
|
if let trackType = await Task(operation: { getString("track-list/\(index)/type") }).value, trackType == "sub" {
|
||||||
if let trackType = getString("track-list/\(index)/type"), trackType == "sub" {
|
return true
|
||||||
// Check if the subtitle track is currently selected
|
|
||||||
let selected = getInt("track-list/\(index)/selected")
|
|
||||||
if selected == 1 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -539,12 +533,16 @@ final class MPVClient: ObservableObject {
|
|||||||
command("video-add", args: [url.absoluteString])
|
command("video-add", args: [url.absoluteString])
|
||||||
}
|
}
|
||||||
|
|
||||||
func addSubTrack(_ url: URL) {
|
func addSubTrack(_ url: URL) async {
|
||||||
command("sub-add", args: [url.absoluteString])
|
await Task {
|
||||||
|
command("sub-add", args: [url.absoluteString])
|
||||||
|
}.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeSubs() {
|
func removeSubs() async {
|
||||||
command("sub-remove")
|
await Task {
|
||||||
|
command("sub-remove")
|
||||||
|
}.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func setVideoToAuto() {
|
func setVideoToAuto() {
|
||||||
|
@ -5,6 +5,8 @@ struct ControlsOverlay: View {
|
|||||||
@ObservedObject private var player = PlayerModel.shared
|
@ObservedObject private var player = PlayerModel.shared
|
||||||
private var model = PlayerControlsModel.shared
|
private var model = PlayerControlsModel.shared
|
||||||
|
|
||||||
|
@State private var availableCaptions: [Captions] = []
|
||||||
|
@State private var isLoadingCaptions = true
|
||||||
@State private var contentSize: CGSize = .zero
|
@State private var contentSize: CGSize = .zero
|
||||||
|
|
||||||
@Default(.showMPVPlaybackStats) private var showMPVPlaybackStats
|
@Default(.showMPVPlaybackStats) private var showMPVPlaybackStats
|
||||||
@ -335,7 +337,6 @@ struct ControlsOverlay: View {
|
|||||||
Image(systemName: "text.bubble")
|
Image(systemName: "text.bubble")
|
||||||
if let captions = captionsBinding.wrappedValue,
|
if let captions = captionsBinding.wrappedValue,
|
||||||
let language = LanguageCodes(rawValue: captions.code)
|
let language = LanguageCodes(rawValue: captions.code)
|
||||||
|
|
||||||
{
|
{
|
||||||
Text("\(language.description.capitalized) (\(language.rawValue))")
|
Text("\(language.description.capitalized) (\(language.rawValue))")
|
||||||
.foregroundColor(.accentColor)
|
.foregroundColor(.accentColor)
|
||||||
@ -380,17 +381,16 @@ struct ControlsOverlay: View {
|
|||||||
.contextMenu {
|
.contextMenu {
|
||||||
Button("Disabled") { captionsBinding.wrappedValue = nil }
|
Button("Disabled") { captionsBinding.wrappedValue = nil }
|
||||||
|
|
||||||
ForEach(player.currentVideo?.captions ?? []) { caption in
|
ForEach(availableCaptions) { caption in
|
||||||
Button(caption.description) { captionsBinding.wrappedValue = caption }
|
Button(caption.description) { captionsBinding.wrappedValue = caption }
|
||||||
}
|
}
|
||||||
Button("Cancel", role: .cancel) {}
|
Button("Cancel", role: .cancel) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ViewBuilder private var captionsPicker: some View {
|
@ViewBuilder private var captionsPicker: some View {
|
||||||
let captions = player.currentVideo?.captions ?? []
|
let captions = availableCaptions
|
||||||
Picker("Captions", selection: captionsBinding) {
|
Picker("Captions", selection: captionsBinding) {
|
||||||
if captions.isEmpty {
|
if captions.isEmpty {
|
||||||
Text("Not available").tag(Captions?.none)
|
Text("Not available").tag(Captions?.none)
|
||||||
@ -402,6 +402,31 @@ struct ControlsOverlay: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.disabled(captions.isEmpty)
|
.disabled(captions.isEmpty)
|
||||||
|
.onAppear {
|
||||||
|
loadCaptions()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func loadCaptions() {
|
||||||
|
isLoadingCaptions = true
|
||||||
|
|
||||||
|
// Fetch captions asynchronously
|
||||||
|
Task {
|
||||||
|
let fetchedCaptions = await fetchCaptions()
|
||||||
|
await MainActor.run {
|
||||||
|
// Update state on the main thread
|
||||||
|
self.availableCaptions = fetchedCaptions
|
||||||
|
self.isLoadingCaptions = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func fetchCaptions() async -> [Captions] {
|
||||||
|
// Access currentVideo from the main actor context
|
||||||
|
await MainActor.run {
|
||||||
|
// Safely access the main actor-isolated currentVideo property
|
||||||
|
player.currentVideo?.captions ?? []
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var captionsBinding: Binding<Captions?> {
|
private var captionsBinding: Binding<Captions?> {
|
||||||
|
Loading…
Reference in New Issue
Block a user