-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutvid.lua
80 lines (71 loc) · 2.24 KB
/
cutvid.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
local status_ok, mp = pcall(require, "mp")
if not status_ok then
return
end
-- timestamps file
local tmp_file_path = "/tmp/mpv.timestamps"
-- temporary directory (generated by ffmpeg-cut-merge script)
local tmp_ffmpeg_dir = "/tmp/ffmpeg_cut_merge"
-- remove previous parasite files
local function cleanup()
os.remove(tmp_file_path)
os.execute("rm -rf " .. tmp_ffmpeg_dir)
end
local start_stamp = nil
local function file_exists()
local f = io.open(tmp_file_path, "r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end
local function format_time()
local time_pos = mp.get_property_number("time-pos")
local time_seg = time_pos % 60
time_pos = time_pos - time_seg
local time_hours = math.floor(time_pos / 3600)
time_pos = time_pos - (time_hours * 3600)
local time_minutes = time_pos / 60
local time_sec,time_ms = string.format("%.03f", time_seg):match("([^.]*).(.*)")
return string.format("%02d:%02d:%02d.%s", time_hours, time_minutes, time_sec, time_ms)
end
local function get_stamps()
if start_stamp then
local end_stamp = format_time()
mp.osd_message("Section ended " .. end_stamp)
-- write start timestamp and encoding duration to tmp file to be processed
-- by ffmpeg
local tmpfile = io.open(tmp_file_path, "a+")
io.output(tmpfile)
io.write(string.format("%s-%s\n", start_stamp, end_stamp))
io.close(tmpfile)
start_stamp = nil
else
start_stamp = format_time()
mp.osd_message("Section started " .. start_stamp)
end
end
local function clean_stamps()
start_stamp = nil
mp.osd_message("Stamp cleaned")
end
local function cutvid()
if start_stamp ~= nil then
mp.osd_message("Missing end section timestamp!")
return
elseif not file_exists() then
mp.osd_message("No section timestamps found!")
return
end
local video_path = string.format("%s/%s", mp.get_property("working-directory"), mp.get_property("path"))
-- if file exists then at least one section has been written to it
os.execute(string.format("/usr/local/bin/ffmpeg-cut-merge %s %s", tmp_file_path, video_path))
mp.osd_message("Video generated")
end
cleanup()
-- add mappings
mp.add_key_binding("Ctrl+t", "get_stamps", get_stamps)
mp.add_key_binding("Ctrl+Shift+t", "clean_stamps", clean_stamps)
mp.add_key_binding("Ctrl+p", "cutvid", cutvid)