mirror of
https://github.com/yattee/yattee.git
synced 2025-12-16 13:08:14 +00:00
Compare commits
1 Commits
fix-subtit
...
mpv-tweake
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c52b4e1007 |
@@ -23,14 +23,15 @@ final class MPVBackend: PlayerBackend {
|
|||||||
|
|
||||||
var stream: Stream?
|
var stream: Stream?
|
||||||
var video: Video?
|
var video: Video?
|
||||||
var captions: Captions? {
|
var captions: Captions? { didSet {
|
||||||
didSet {
|
guard let captions else {
|
||||||
Task {
|
if client?.areSubtitlesAdded == true {
|
||||||
await handleCaptionsChange()
|
client?.removeSubs()
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
addSubTrack(captions.url)
|
||||||
|
}}
|
||||||
var currentTime: CMTime?
|
var currentTime: CMTime?
|
||||||
|
|
||||||
var loadedVideo = false
|
var loadedVideo = false
|
||||||
@@ -616,14 +617,10 @@ final class MPVBackend: PlayerBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addSubTrack(_ url: URL) {
|
func addSubTrack(_ url: URL) {
|
||||||
Task {
|
if client?.areSubtitlesAdded == true {
|
||||||
if let areSubtitlesAdded = client?.areSubtitlesAdded {
|
client?.removeSubs()
|
||||||
if await areSubtitlesAdded() {
|
|
||||||
await client?.removeSubs()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await client?.addSubTrack(url)
|
|
||||||
}
|
}
|
||||||
|
client?.addSubTrack(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setVideoToAuto() {
|
func setVideoToAuto() {
|
||||||
@@ -687,17 +684,6 @@ 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":
|
||||||
|
|||||||
@@ -79,14 +79,21 @@ final class MPVClient: ObservableObject {
|
|||||||
checkError(mpv_set_option_string(mpv, "user-agent", UserAgentManager.shared.userAgent))
|
checkError(mpv_set_option_string(mpv, "user-agent", UserAgentManager.shared.userAgent))
|
||||||
checkError(mpv_set_option_string(mpv, "initial-audio-sync", Defaults[.mpvInitialAudioSync] ? "yes" : "no"))
|
checkError(mpv_set_option_string(mpv, "initial-audio-sync", Defaults[.mpvInitialAudioSync] ? "yes" : "no"))
|
||||||
|
|
||||||
|
// A/V-SYNC //
|
||||||
|
|
||||||
// Enable VSYNC – needed for `video-sync`
|
// Enable VSYNC – needed for `video-sync`
|
||||||
checkError(mpv_set_option_string(mpv, "opengl-swapinterval", "1"))
|
checkError(mpv_set_option_string(mpv, "opengl-swapinterval", "1"))
|
||||||
checkError(mpv_set_option_string(mpv, "video-sync", "display-resample"))
|
checkError(mpv_set_option_string(mpv, "video-sync", "display-resample"))
|
||||||
checkError(mpv_set_option_string(mpv, "interpolation", "yes"))
|
checkError(mpv_set_option_string(mpv, "interpolation", "yes"))
|
||||||
checkError(mpv_set_option_string(mpv, "tscale", "mitchell"))
|
checkError(mpv_set_option_string(mpv, "tscale", "mitchell"))
|
||||||
checkError(mpv_set_option_string(mpv, "tscale-window", "blackman"))
|
checkError(mpv_set_option_string(mpv, "tscale-window", "blackman"))
|
||||||
checkError(mpv_set_option_string(mpv, "vd-lavc-framedrop", "nonref"))
|
checkError(mpv_set_option_string(mpv, "vd-lavc-framedrop", "no"))
|
||||||
checkError(mpv_set_option_string(mpv, "display-fps-override", "\(String(getScreenRefreshRate()))"))
|
checkError(mpv_set_option_string(mpv, "display-fps-override", "\(String(getScreenRefreshRate()))"))
|
||||||
|
checkError(mpv_set_option_string(mpv, "video-sync-max-factor", "1.1"))
|
||||||
|
checkError(mpv_set_option_string(mpv, "video-sync-max-video-change", "10"))
|
||||||
|
checkError(mpv_set_option_string(mpv, "audio-buffer", "0.2"))
|
||||||
|
checkError(mpv_set_option_string(mpv, "audio-wait-open", "no"))
|
||||||
|
checkError(mpv_set_option_string(mpv, "force-window", "yes"))
|
||||||
|
|
||||||
// CPU //
|
// CPU //
|
||||||
|
|
||||||
@@ -349,15 +356,21 @@ final class MPVClient: ObservableObject {
|
|||||||
return Int(fps.rounded())
|
return Int(fps.rounded())
|
||||||
}
|
}
|
||||||
|
|
||||||
func areSubtitlesAdded() async -> Bool {
|
var areSubtitlesAdded: Bool {
|
||||||
guard !mpv.isNil else { return false }
|
guard !mpv.isNil else { return false }
|
||||||
|
|
||||||
let trackCount = await Task(operation: { getInt("track-list/count") }).value
|
// Retrieve the number of tracks
|
||||||
|
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 {
|
||||||
if let trackType = await Task(operation: { getString("track-list/\(index)/type") }).value, trackType == "sub" {
|
// Get the type of each track
|
||||||
return true
|
if let trackType = getString("track-list/\(index)/type"), trackType == "sub" {
|
||||||
|
// Check if the subtitle track is currently selected
|
||||||
|
let selected = getInt("track-list/\(index)/selected")
|
||||||
|
if selected == 1 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@@ -533,16 +546,12 @@ final class MPVClient: ObservableObject {
|
|||||||
command("video-add", args: [url.absoluteString])
|
command("video-add", args: [url.absoluteString])
|
||||||
}
|
}
|
||||||
|
|
||||||
func addSubTrack(_ url: URL) async {
|
func addSubTrack(_ url: URL) {
|
||||||
await Task {
|
command("sub-add", args: [url.absoluteString])
|
||||||
command("sub-add", args: [url.absoluteString])
|
|
||||||
}.value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeSubs() async {
|
func removeSubs() {
|
||||||
await Task {
|
command("sub-remove")
|
||||||
command("sub-remove")
|
|
||||||
}.value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setVideoToAuto() {
|
func setVideoToAuto() {
|
||||||
|
|||||||
@@ -269,6 +269,15 @@ final class PlayerModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
|
||||||
|
guard let self else { return }
|
||||||
|
|
||||||
|
if !self.musicMode, self.activeBackend == .mpv {
|
||||||
|
self.mpvBackend.setVideoToAuto()
|
||||||
|
self.mpvBackend.controls.resetTimer()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
presentingPlayer = true
|
presentingPlayer = true
|
||||||
|
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
@@ -290,6 +299,9 @@ final class PlayerModel: ObservableObject {
|
|||||||
DispatchQueue.main.async { [weak self] in
|
DispatchQueue.main.async { [weak self] in
|
||||||
Delay.by(0.3) {
|
Delay.by(0.3) {
|
||||||
self?.exitFullScreen(showControls: false)
|
self?.exitFullScreen(showControls: false)
|
||||||
|
if self?.activeBackend == .mpv, !(self?.musicMode ?? false) {
|
||||||
|
self?.mpvBackend.setVideoToNo()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1013,10 +1025,9 @@ final class PlayerModel: ObservableObject {
|
|||||||
#else
|
#else
|
||||||
func handleEnterForeground() {
|
func handleEnterForeground() {
|
||||||
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
|
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
|
||||||
guard let self = self else { return }
|
guard let self else { return }
|
||||||
|
|
||||||
if !self.musicMode, self.activeBackend == .mpv {
|
if !self.musicMode, self.activeBackend == .mpv, presentingPlayer {
|
||||||
self.mpvBackend.addVideoTrackFromStream()
|
|
||||||
self.mpvBackend.setVideoToAuto()
|
self.mpvBackend.setVideoToAuto()
|
||||||
self.mpvBackend.controls.resetTimer()
|
self.mpvBackend.controls.resetTimer()
|
||||||
} else if !self.musicMode, self.activeBackend == .appleAVPlayer {
|
} else if !self.musicMode, self.activeBackend == .appleAVPlayer {
|
||||||
@@ -1047,7 +1058,7 @@ final class PlayerModel: ObservableObject {
|
|||||||
pause()
|
pause()
|
||||||
} else if !playingInPictureInPicture, activeBackend == .appleAVPlayer {
|
} else if !playingInPictureInPicture, activeBackend == .appleAVPlayer {
|
||||||
avPlayerBackend.removePlayerFromLayer()
|
avPlayerBackend.removePlayerFromLayer()
|
||||||
} else if activeBackend == .mpv, !musicMode {
|
} else if activeBackend == .mpv, !musicMode, presentingPlayer {
|
||||||
mpvBackend.setVideoToNo()
|
mpvBackend.setVideoToNo()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ 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
|
||||||
@@ -337,6 +335,7 @@ 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)
|
||||||
@@ -381,16 +380,17 @@ struct ControlsOverlay: View {
|
|||||||
.contextMenu {
|
.contextMenu {
|
||||||
Button("Disabled") { captionsBinding.wrappedValue = nil }
|
Button("Disabled") { captionsBinding.wrappedValue = nil }
|
||||||
|
|
||||||
ForEach(availableCaptions) { caption in
|
ForEach(player.currentVideo?.captions ?? []) { 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 = availableCaptions
|
let captions = player.currentVideo?.captions ?? []
|
||||||
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,31 +402,6 @@ 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?> {
|
||||||
|
|||||||
Reference in New Issue
Block a user