This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
sub-export.lua
79 lines (62 loc) · 2.89 KB
/
sub-export.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
-- Usage:
-- Select subtitles and press Shift + X.
--
-- Note:
-- Requires FFmpeg in PATH environment variable or edit ffmpeg_path in the script options,
-- for example, by replacing [[ffmpeg]] with [[C:\Programs\ffmpeg\bin\ffmpeg.exe]]
-- Note:
-- A small circle at the top-right corner is a sign that export is happenning now.
-- Note:
-- The exported subtitles will be automatically selected with visibility set to true.
-- Note:
-- It could take ~1-5 minutes to export subtitles.
utils = require 'mp.utils'
---- Script Options ----
ffmpeg_path = [[ffmpeg]]
------------------------
function export_selected_subtitles()
local i = 0
local tracks_count = mp.get_property_number("track-list/count")
while i < tracks_count do
local track_type = mp.get_property(string.format("track-list/%d/type", i))
local track_index = mp.get_property_number(string.format("track-list/%d/ff-index", i))
local track_selected = mp.get_property(string.format("track-list/%d/selected", i))
local track_lang = mp.get_property(string.format("track-list/%d/lang", i))
local track_external = mp.get_property(string.format("track-list/%d/external", i))
local track_codec = mp.get_property(string.format("track-list/%d/codec", i))
if track_type == "sub" and track_selected == "yes" then
if track_external == "yes" then
mp.osd_message("Error: external subtitles have been selected", 2)
return
end
local video_file = mp.get_property("working-directory") .. "/" .. mp.get_property("filename")
local subtitles_ext = ".srt"
if track_codec == "ass" then
subtitles_ext = ".ass"
end
if track_lang ~= nil then
subtitles_ext = "." .. track_lang .. subtitles_ext
end
subtitles_file = mp.get_property("working-directory") .. "/" .. mp.get_property("filename/no-ext") .. subtitles_ext
mp.osd_message("Exporting selected subtitles")
args = {ffmpeg_path, '-y', '-hide_banner', '-loglevel', 'error', '-i', video_file, "-map", string.format("0:%d", track_index), subtitles_file}
mp.add_timeout(mp.get_property_number("osd-duration") * 0.001, process)
break
end
i = i + 1
end
end
function process()
local screenx, screeny, aspect = mp.get_osd_size()
mp.set_osd_ass(screenx, screeny, "{\\an9}● ")
local res = utils.subprocess({ args = args })
mp.set_osd_ass(screenx, screeny, "")
if res.status == 0 then
mp.osd_message("Finished exporting subtitles")
mp.commandv("sub-add", subtitles_file)
mp.set_property("sub-visibility", "yes")
else
mp.osd_message("Failed to export subtitles")
end
end
mp.add_key_binding("X", "export-selected-subtitles", export_selected_subtitles)