drop audio on desync

If avdesync > leadin then we resume too late after the subtitle should have been shown. When mpv plays with no audio, subtitles are timed to video instead of audio. This change removes audio in this case to ensure we resume on time.
This commit is contained in:
bitingsock 2018-11-29 03:43:22 -08:00 committed by zenyd
parent 9eefa4ee4a
commit c7f88e0ec2
1 changed files with 17 additions and 0 deletions

View File

@ -17,11 +17,13 @@ function set_timeout()
return time_out return time_out
end end
local aid
function restore_normalspeed() function restore_normalspeed()
mp.set_property("speed", normalspeed) mp.set_property("speed", normalspeed)
if mp.get_property_native("video-sync") == "desync" then if mp.get_property_native("video-sync") == "desync" then
mp.set_property("video-sync", "audio") mp.set_property("video-sync", "audio")
end end
if aid~=mp.get_property("aid") then mp.set_property("aid", aid) end
end end
function check_should_speedup() function check_should_speedup()
@ -36,11 +38,23 @@ function check_should_speedup()
return nextsub, nextsub >= lookahead or nextsub == 0, mark return nextsub, nextsub >= lookahead or nextsub == 0, mark
end end
function check_audio(_,ds)
if state==0 then
return
end
if ds and tonumber(ds)>leadin then
aid = mp.get_property("aid")
mp.set_property("aid", "no")
print("avsync greater than leadin, dropping audio")
end
end
function check_position(_, position) function check_position(_, position)
if position then if position then
if nextsub ~= 0 and position >= (mark+nextsub-leadin) then if nextsub ~= 0 and position >= (mark+nextsub-leadin) then
restore_normalspeed() restore_normalspeed()
mp.unobserve_property(check_position) mp.unobserve_property(check_position)
mp.unobserve_property(check_audio)
elseif nextsub == 0 and position >= (mark+set_timeout()-leadin) then elseif nextsub == 0 and position >= (mark+set_timeout()-leadin) then
nextsub, _ , mark = check_should_speedup() nextsub, _ , mark = check_should_speedup()
end end
@ -85,12 +99,15 @@ function speed_transition(_, sub)
mp.set_property("speed", speedup) mp.set_property("speed", speedup)
mp.observe_property("time-pos", "native", check_position) mp.observe_property("time-pos", "native", check_position)
state = 1 state = 1
aid=mp.get_property("aid")
mp.observe_property("avsync", "native", check_audio)
end end
end end
end end
elseif state == 1 then elseif state == 1 then
if sub ~= "" and sub ~= nil or not mp.get_property_native("sid") then if sub ~= "" and sub ~= nil or not mp.get_property_native("sid") then
mp.unobserve_property(check_position) mp.unobserve_property(check_position)
mp.unobserve_property(check_audio)
restore_normalspeed() restore_normalspeed()
state = 0 state = 0
else else