Skip to content

Instantly share code, notes, and snippets.

@AndyHoang
Forked from ekisu/peerflix-hook.lua
Last active June 3, 2018 04:24
Show Gist options
  • Save AndyHoang/e7dff6a33b852883842fcc1e39b3b7a3 to your computer and use it in GitHub Desktop.
Save AndyHoang/e7dff6a33b852883842fcc1e39b3b7a3 to your computer and use it in GitHub Desktop.
A Lua user script for mpv that allows playing of magnet links, similar to the youtube-dl hook.
local utils = require 'mp.utils'
local msg = require 'mp.msg'
-- OPTIONS
local keep_file_after_streaming = false
local peerflix = {
path = "peerflix",
searched = false
}
local wait_for_file_to_exist_script = [[
until [ -f /tmp/peerflix.lock ];
do
sleep 1;
done;
rm /tmp/peerflix.lock;
]]
local peerflix_is_running = false
mp.add_hook("on_load", 20, function ()
local url = mp.get_property("stream-open-filename")
msg.warn(url)
if (url:find("magnet:") == 1) or (url:find("peerflix://") == 1) then -- Support for .torrent files will come, someday
-- search for peerflix in mpv's config folder
if not (peerflix.searched) then
local peerflix_mcd = mp.find_config_file("peerflix")
if not (peerflix_mcd == nil) then
msg.verbose("found peerflix")
peerflix.path = peerflix_mcd
end
peerflix.searched = false
end
if (url:find("peerflix://") == 1) then
url = url:sub(12)
end
msg.warn("Starting peerflix")
-- utils.subprocess can't execute peerflix without blocking
if (keep_file_after_streaming) then
os.execute(peerflix.path .. " -q -p 8888 --on-listening 'touch /tmp/peerflix.lock' " .. url .. " &")
else
os.execute(peerflix.path .. " -q -r -p 8888 --on-listening 'touch /tmp/peerflix.lock' " .. url .. " &")
end
peerflix_is_running = true
msg.warn("Waiting for server")
-- There's probably a better way to do this...
os.execute("echo '" .. wait_for_file_to_exist_script .. "' | bash -")
msg.warn("Server is up")
mp.set_property("stream-open-filename", "http://localhost:8888")
end
end)
mp.add_hook("on_unload", 10, function ()
if (peerflix_is_running) then
os.execute("killall peerflix") -- Should get a way to get the PID, but using $! seems to fail.
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment