Skip to content

Commit

Permalink
Support mpv 0.29.0
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
po5 committed Oct 31, 2022
1 parent 11764cd commit b097b77
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Linux: None, works out of the box

Mac: None, works out of the box

This script makes an effort to run on mpv versions as old as 0.29.0.
Note that most custom UIs will not support vintage mpv builds, consider updating if you're having issues.

## Usage
Once the lua file is in your scripts directory, and you are using a UI that supports thumbfast, you are done.
Hover on the timeline for nice thumbnails.
Expand Down
51 changes: 35 additions & 16 deletions thumbfast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ mp.utils = require "mp.utils"
mp.options = require "mp.options"
mp.options.read_options(options, "thumbfast")

local pre_0_30_0 = mp.command_native_async == nil

function subprocess(args, async, callback)
callback = callback or function() end

if not pre_0_30_0 then
if async then
return mp.command_native_async({name = "subprocess", playback_only = true, args = args}, callback)
else
return mp.command_native({name = "subprocess", playback_only = false, capture_stdout = true, args = args})
end
else
if async then
return mp.utils.subprocess_detached({args = args}, callback)
else
return mp.utils.subprocess({args = args})
end
end
end

local winapi = {}
if options.use_lua_io then
local ffi_loaded, ffi = pcall(require, "ffi")
Expand Down Expand Up @@ -133,7 +153,7 @@ local client_script = [=[
MPV_IPC_FD=0; MPV_IPC_PATH="%s"
trap "kill 0" EXIT
while [[ $# -ne 0 ]]; do case $1 in --mpv-ipc-fd=*) MPV_IPC_FD=${1/--mpv-ipc-fd=/} ;; esac; shift; done
if echo "print-text test" >&"$MPV_IPC_FD"; then echo -n > "$MPV_IPC_PATH"; tail -f "$MPV_IPC_PATH" >&"$MPV_IPC_FD" & while read -r -u "$MPV_IPC_FD"; do :; done; fi
if echo "print-text thumbfast" >&"$MPV_IPC_FD"; then echo -n > "$MPV_IPC_PATH"; tail -f "$MPV_IPC_PATH" >&"$MPV_IPC_FD" & while read -r -u "$MPV_IPC_FD"; do :; done; fi
]=]

local function get_os()
Expand All @@ -149,7 +169,7 @@ local function get_os()
raw_os_name = env_OS
end
else
raw_os_name = mp.command_native({name = "subprocess", playback_only = false, capture_stdout = true, args = {"uname", "-s"}}).stdout
raw_os_name = subprocess({"uname", "-s"}).stdout
end
end

Expand Down Expand Up @@ -201,7 +221,7 @@ if options.thumbnail == "" then
end
end

local unique = mp.get_property_native("pid")
local unique = mp.utils.getpid()

options.socket = options.socket .. unique
options.thumbnail = options.thumbnail .. unique
Expand All @@ -219,7 +239,7 @@ end
local mpv_path = "mpv"

if os_name == "Mac" and unique then
mpv_path = string.gsub(mp.command_native({name = "subprocess", playback_only = false, capture_stdout = true, args = {"ps", "-o", "comm=", "-p", tostring(unique)}}).stdout, "[\n\r]", "")
mpv_path = string.gsub(subprocess({"ps", "-o", "comm=", "-p", tostring(unique)}).stdout, "[\n\r]", "")
end

local function vf_string(filters, full)
Expand Down Expand Up @@ -307,34 +327,37 @@ local function spawn(time)
"--ytdl-format=worst", "--demuxer-readahead-secs=0", "--demuxer-max-bytes=128KiB",
"--vd-lavc-skiploopfilter=all", "--vd-lavc-software-fallback=1", "--vd-lavc-fast", "--vd-lavc-threads=2", "--hwdec="..(options.hwdec and "auto" or "no"),
"--vf="..vf_string(filters_all, true),
"--sws-allow-zimg=no", "--sws-fast=yes", "--sws-scaler=fast-bilinear",
"--sws-scaler=fast-bilinear",
"--video-rotate="..last_rotate,
"--ovc=rawvideo", "--of=image2", "--ofopts=update=1", "--o="..options.thumbnail
}

if not pre_0_30_0 then
table.insert(args, "--sws-allow-zimg=no")
end

if os_name == "Windows" then
table.insert(args, "--input-ipc-server="..options.socket)
else
local client_script_path = options.socket..".run"
local file = io.open(client_script_path, "w+")
if file == nil then
mp.msg.error("client script write failed.")
mp.msg.error("client script write failed")
return
else
file:write(string.format(client_script, options.socket))
file:close()
mp.command_native_async({name = "subprocess", playback_only = true, args = {"chmod", "+x", client_script_path}}, function() end)
subprocess({"chmod", "+x", client_script_path}, true)
table.insert(args, "--script="..client_script_path)
end
end

spawned = true

mp.command_native_async(
{name = "subprocess", playback_only = true, args = args},
subprocess(args, true,
function(success, result)
if success == false or result.status ~= 0 then
mp.msg.error("mpv subprocess create failed.")
mp.msg.error("mpv subprocess create failed")
end
spawned = false
end
Expand Down Expand Up @@ -377,9 +400,7 @@ local function draw(w, h, script)
end

if x ~= nil then
mp.command_native(
{name = "overlay-add", id=options.overlay_id, x=x, y=y, file=options.thumbnail..".bgra", offset=0, fmt="bgra", w=display_w, h=display_h, stride=(4*display_w)}
)
mp.command_native({"overlay-add", options.overlay_id, x, y, options.thumbnail..".bgra", 0, "bgra", display_w, display_h, (4*display_w)})
elseif script then
local json, err = mp.utils.format_json({width=display_w, height=display_h, x=x, y=y, socket=options.socket, thumbnail=options.thumbnail, overlay_id=options.overlay_id})
mp.commandv("script-message-to", script, "thumbfast-render", json)
Expand Down Expand Up @@ -516,9 +537,7 @@ local function clear()
last_x = nil
last_y = nil
if script_name then return end
mp.command_native(
{name = "overlay-remove", id=options.overlay_id}
)
mp.command_native({"overlay-remove", options.overlay_id})
end

local function watch_changes()
Expand Down

2 comments on commit b097b77

@hooke007
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally broken here(windows10 & latest mpv-git)

Snipaste_2022-10-31_19-32-53

@po5
Copy link
Owner Author

@po5 po5 commented on b097b77 Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this commit.

Please sign in to comment.