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