add script censor

This commit is contained in:
zenyd 2021-03-13 16:18:52 +01:00
parent 54a74c4441
commit 85f9964f30
3 changed files with 125 additions and 0 deletions

View File

@ -73,3 +73,9 @@ Key Bind|Effect
## copy-paste-URL
Like its name suggests - copy and paste links into mpv with `ctrl + v` to start playback of a video. This needs an open mpv window like `mpv --idle --force-window` or a window already playing a video. Also the script utilizes powershell, so that needs to be installed as well.
## censor
Skip over parts of videos you don't want (others) to view.
**Usage:** Put the exact filename (w/o extension) of the video you wan't this script to affect inside the names folder. The file has to have an extension itself. Example filename "video.mkv" -> "names/video.txt". Timestamps follow the format "X+:XX:XX(.X+) X+:XX:XX(.X+)". First timestamp denotes beginning and second end of skip window. Sample is included in names directory.

115
censor/main.lua Normal file
View File

@ -0,0 +1,115 @@
local utils = require 'mp.utils'
SkipIndex = 1
SkipWindow = nil
names_directory = utils.join_path(mp.get_script_directory(), 'names')
--read-in filenames for files to be censored
names = utils.readdir(names_directory)
--execute a skip when in skip window and set the next skip window
function skip(time_pos, tpos)
if SkipWindow == nil or tpos == nil then return end
if withinSkipWindow(SkipWindow, tpos) then
local skipTo = SkipWindow[2]
SkipIndex = SkipIndex + 1
SkipWindow = TimeStamps[SkipIndex]
mp.set_property_number('time-pos', skipTo)
end
end
function withinSkipWindow(skipwindow, tpos)
return tpos >= skipwindow[1] and tpos < skipwindow[2]
end
function isNextSkipWindow(skipwindow, tpos)
return skipwindow[1] > tpos
end
--when seeking forward/backward we need to find the correct skip window related to current time position
function searchSkipWindow(seeking)
SkipWindow = nil
local curPos = mp.get_property_number('time-pos')
if TimeStamps and curPos then
--iterate over all our timestamps and find the correct window
for i, window in ipairs(TimeStamps) do
if withinSkipWindow(window, curPos) then
SkipIndex = i
SkipWindow = window
break
elseif isNextSkipWindow(window, curPos) then
SkipIndex = i
SkipWindow = window
break
end
end
end
end
--return timeformat in seconds
function tryParseTimeFormat(line)
--parse line
local h1, m1, s1, h2, m2, s2 = line:match('(%d+):(%d%d):(%d%d%.?%d*)%s+(%d+):(%d%d):(%d%d%.?%d*)')
if not h1 then
print('invalid line/time format: ' .. line)
return nil
end
h1 = tonumber(h1)
h2 = tonumber(h2)
m1 = tonumber(m1)
m2 = tonumber(m2)
s1 = tonumber(s1)
s2 = tonumber(s2)
--check ranges for time parts
if m1 >= 60 or m2 >= 60 or s1 >= 60 or s2 >= 60 then
print('invalid time format - there are time parts >= 60')
return nil
end
--convert into seconds
local t1 = h1*60*60 + m1*60 + s1
local t2 = h2*60*60 + m2*60 + s2
return t1, t2
end
function handler()
local filename = mp.get_property('filename/no-ext', nil)
print('filename: ' .. filename)
if filename ~= nil and names ~= nil then
for _, name in ipairs(names) do
--extract filename (containing timestamps) w/o extension
name_ = string.gsub(name, '%.[^.]+$', '')
if name_ == filename then
--found a timestamp file (name stores path to file)
TimeStamps = {}
--parse file and get timestamps into table TimeStamps
for line in io.lines(utils.join_path(names_directory, name)) do
local t1, t2 = tryParseTimeFormat(line)
if t1 ~= nil then
--valid timestamps found - insert into table
table.insert(TimeStamps, { tonumber(t1), tonumber(t2) })
end
end
break
end
end
if TimeStamps and #TimeStamps > 0 then
print('TimeStamps: '..utils.to_string(TimeStamps))
print('Active')
mp.observe_property('seeking', nil, searchSkipWindow)
mp.observe_property('time-pos', 'number', skip)
end
end
end
mp.register_event('file-loaded', handler)

View File

@ -0,0 +1,4 @@
#Files containing timestamps have to have an extension like .txt
#Timestamps follow format X+:XX:XX(.X+)
0:00:02.09 0:05:00
1:46:00 1:47:32.125