This avoids having to rewind the video for a second telecine detection pass.
Unified · Split
| @@ -2,21 +2,16 @@ · 1 unmodified line | |||
| 2 | -- appropriate deinterlacing filter based on a short section of the | 2 | -- appropriate deinterlacing filter based on a short section of the |
| 3 | -- currently playing video. | 3 | -- currently playing video. |
| 4 | -- | 4 | -- |
| 5 | -- It registers the key-binding ctrl+d, which when pressed, inserts | 5 | -- It registers the key-binding ctrl+d, which when pressed, inserts the filters |
| 6 | -- the filter vf=lavfi=idet. After 4 second, it examines the results | 6 | -- ``vf=lavfi=idet,pullup,vf=lavfi=idet``. After 4 seconds, it removes these |
| 7 | -- to determine whether the content progressive or interlaced and the | 7 | -- filters and decides whether the content is progressive, interlaced, or |
| 8 | -- interlacing field dominance. It immediately sets the latter within | 8 | -- telecined and the interlacing field dominance. |
| 9 | -- mpv. | ||
| 10 | -- | 9 | -- |
| 11 | -- If the content is judged not to be progressive, it may be either | 10 | -- Based on this information, it may set mpv's ``deinterlace`` property (which |
| 12 | -- interlaced or telecined. To determine which one, the video is | 11 | -- usually inserts the yadif filter), or insert the ``pullup`` filter if the |
| 13 | -- seeked back to the detection starting point with the "pullup" | 12 | -- content is telecined. It also sets mpv's ``field-dominance`` property. |
| 14 | -- filter inserted. After another four seconds, if idet thinks that | ||
| 15 | -- the pulled up viedo is "progressive", then pullup is the correct | ||
| 16 | -- filter and this script leaves it in places while removing the idet | ||
| 17 | -- filter. Otherwise, the content plain interlaced and mpv's deinterlace | ||
| 18 | -- property is set. This will usually insert the yadif deinterlacing filter. | ||
| 19 | -- | 13 | -- |
| 14 | -- OPTIONS: | ||
| 20 | -- The default detection time may be overridden by adding | 15 | -- The default detection time may be overridden by adding |
| 21 | -- | 16 | -- |
| 22 | -- --script-opts=autodeint.detect_seconds=<number of seconds> | 17 | -- --script-opts=autodeint.detect_seconds=<number of seconds> |
| @@ -37,6 +32,7 @@ require "mp.msg" · 14 unmodified lines | |||
| 37 | script_name = mp.get_script_name() | 32 | script_name = mp.get_script_name() |
| 38 | detect_label = string.format("%s-detect", script_name) | 33 | detect_label = string.format("%s-detect", script_name) |
| 39 | pullup_label = string.format("%s", script_name) | 34 | pullup_label = string.format("%s", script_name) |
| 35 | ivtc_detect_label = string.format("%s-ivtc-detect", script_name) | ||
| 40 | 36 | ||
| 41 | -- number of seconds to gather cropdetect data | 37 | -- number of seconds to gather cropdetect data |
| 42 | detect_seconds = tonumber(mp.get_opt(string.format("%s.detect_seconds", script_name))) | 38 | detect_seconds = tonumber(mp.get_opt(string.format("%s.detect_seconds", script_name))) |
| @@ -69,30 +65,29 @@ function start_detect() · 26 unmodified lines | |||
| 69 | mp.set_property("deinterlace","no") | 65 | mp.set_property("deinterlace","no") |
| 70 | del_filter_if_present(pullup_label) | 66 | del_filter_if_present(pullup_label) |
| 71 | 67 | ||
| 72 | percent_pos = mp.get_property("percent-pos") | ||
| 73 | |||
| 74 | -- insert the detection filter | 68 | -- insert the detection filter |
| 75 | local cmd = string.format('vf add @%s:lavfi=graph="idet"', detect_label) | 69 | local cmd = string.format('vf add @%s:lavfi=graph="idet",@%s:pullup,@%s:lavfi=graph="idet"', |
| 70 | detect_label, pullup_label, ivtc_detect_label) | ||
| 76 | if not mp.command(cmd) then | 71 | if not mp.command(cmd) then |
| 77 | mp.msg.error("failed to insert detection filter") | 72 | mp.msg.error("failed to insert detection filters") |
| 78 | return | 73 | return |
| 79 | end | 74 | end |
| 80 | 75 | ||
| 81 | -- wait to gather data | 76 | -- wait to gather data |
| 82 | timer = mp.add_timeout(detect_seconds, judge_field_dominance) | 77 | timer = mp.add_timeout(detect_seconds, select_filter) |
| 83 | end | 78 | end |
| 84 | 79 | ||
| 85 | function stop_detect() | 80 | function stop_detect() |
| 86 | del_filter_if_present(detect_label) | 81 | del_filter_if_present(detect_label) |
| 82 | del_filter_if_present(ivtc_detect_label) | ||
| 87 | timer = nil | 83 | timer = nil |
| 88 | mp.set_property("playback-pos", percent_pos) | ||
| 89 | end | 84 | end |
| 90 | 85 | ||
| 91 | progressive, interlaced_tff, interlaced_bff, interlaced = 0, 1, 2, 3, 4 | 86 | progressive, interlaced_tff, interlaced_bff, interlaced = 0, 1, 2, 3, 4 |
| 92 | 87 | ||
| 93 | function judge() | 88 | function judge(label) |
| 94 | -- get the metadata | 89 | -- get the metadata |
| 95 | local result = mp.get_property_native(string.format("vf-metadata/%s", detect_label)) | 90 | local result = mp.get_property_native(string.format("vf-metadata/%s", label)) |
| 96 | num_tff = tonumber(result["lavfi.idet.multiple.tff"]) | 91 | num_tff = tonumber(result["lavfi.idet.multiple.tff"]) |
| 97 | num_bff = tonumber(result["lavfi.idet.multiple.bff"]) | 92 | num_bff = tonumber(result["lavfi.idet.multiple.bff"]) |
| 98 | num_progressive = tonumber(result["lavfi.idet.multiple.progressive"]) | 93 | num_progressive = tonumber(result["lavfi.idet.multiple.progressive"]) |
| @@ -100,10 +95,10 @@ function judge() · 1 unmodified line | |||
| 100 | num_interlaced = num_tff + num_bff | 95 | num_interlaced = num_tff + num_bff |
| 101 | num_determined = num_interlaced + num_progressive | 96 | num_determined = num_interlaced + num_progressive |
| 102 | 97 | ||
| 103 | mp.msg.verbose("progressive = "..num_progressive) | 98 | mp.msg.verbose(label.." progressive = "..num_progressive) |
| 104 | mp.msg.verbose("interlaced-tff = "..num_tff) | 99 | mp.msg.verbose(label.." interlaced-tff = "..num_tff) |
| 105 | mp.msg.verbose("interlaced-bff = "..num_bff) | 100 | mp.msg.verbose(label.." interlaced-bff = "..num_bff) |
| 106 | mp.msg.verbose("undetermined = "..num_undetermined) | 101 | mp.msg.verbose(label.." undetermined = "..num_undetermined) |
| 107 | 102 | ||
| 108 | if num_determined < num_undetermined then | 103 | if num_determined < num_undetermined then |
| 109 | mp.msg.warn("majority undetermined frames") | 104 | mp.msg.warn("majority undetermined frames") |
| @@ -119,11 +114,12 @@ function judge() · 9 unmodified lines | |||
| 119 | end | 114 | end |
| 120 | end | 115 | end |
| 121 | 116 | ||
| 122 | function judge_field_dominance() | 117 | function select_filter() |
| 123 | verdict = judge() | 118 | -- handle the first detection filter results |
| 119 | verdict = judge(detect_label) | ||
| 124 | if verdict == progressive then | 120 | if verdict == progressive then |
| 125 | stop_detect() | ||
| 126 | mp.msg.info("progressive: doing nothing") | 121 | mp.msg.info("progressive: doing nothing") |
| 122 | stop_detect() | ||
| 127 | return | 123 | return |
| 128 | elseif verdict == interlaced_tff then | 124 | elseif verdict == interlaced_tff then |
| 129 | mp.set_property("field-dominance", "top") | 125 | mp.set_property("field-dominance", "top") |
| @@ -133,21 +129,8 @@ function judge_field_dominance() · 3 unmodified lines | |||
| 133 | mp.set_property("field-dominance", "auto") | 129 | mp.set_property("field-dominance", "auto") |
| 134 | end | 130 | end |
| 135 | 131 | ||
| 136 | local cmd = string.format("vf pre @%s:pullup", pullup_label) | 132 | -- handle the ivtc detection filter results |
| 137 | if not mp.command(cmd) then | 133 | verdict = judge(ivtc_detect_label) |
| 138 | mp.msg.error("failed to insert pullup filter") | ||
| 139 | stop_detect() | ||
| 140 | return | ||
| 141 | end | ||
| 142 | |||
| 143 | -- redo the detection with the pullup filter to see if it's telecined | ||
| 144 | mp.set_property("percent-pos", percent_pos) | ||
| 145 | timer = mp.add_timeout(detect_seconds, judge_ivtc) | ||
| 146 | end | ||
| 147 | |||
| 148 | function judge_ivtc() | ||
| 149 | verdict = judge() | ||
| 150 | |||
| 151 | if verdict == progressive then | 134 | if verdict == progressive then |
| 152 | mp.msg.info(string.format("telecinied with %s field dominance: using pullup", mp.get_property("field-dominance"))) | 135 | mp.msg.info(string.format("telecinied with %s field dominance: using pullup", mp.get_property("field-dominance"))) |
| 153 | stop_detect() | 136 | stop_detect() |
| @@ -157,8 +140,6 @@ function judge_ivtc() · 3 unmodified lines | |||
| 157 | mp.set_property("deinterlace","yes") | 140 | mp.set_property("deinterlace","yes") |
| 158 | stop_detect() | 141 | stop_detect() |
| 159 | end | 142 | end |
| 160 | |||
| 161 | return | ||
| 162 | end | 143 | end |
| 163 | 144 | ||
| 164 | mp.add_key_binding("ctrl+d", script_name, start_detect) | 145 | mp.add_key_binding("ctrl+d", script_name, start_detect) |