Skip to content

Instantly share code, notes, and snippets.

@varenc
Last active April 23, 2025 01:49
Show Gist options
  • Save varenc/49ba127037653f3cfb65aac47f52b707 to your computer and use it in GitHub Desktop.
Save varenc/49ba127037653f3cfb65aac47f52b707 to your computer and use it in GitHub Desktop.
Recovery a deleted file that you still have open in `mpv` or `iina` on macOS

Recover a deleted file from a running mpv process with an open file descriptor

If a macOS/linux process still has a deleted file open, you can recover it entirely using the open file descriptor — even if the file has been deleted from disk!

This guide shows how to do it using mpv’s IPC interface and embedded Lua scripting. Don't close the mpv process and hopefully it already has a IPC interface set. The general approach is generic, but you need a way to get the running process itself to perform the fd copy. We take advantage of mpv's ability to run arbitary lua code.


Find Open File Descriptor

lsof -p <PID_OF_MPV>;

Look for a line like:

mpv  25562 chris   12r   REG ... /private/tmp/my_deleted_file.mp4

Take note of the FD number (e.g. 12).


Create a Lua Recovery Script

Create the following file as /tmp/mpv_recovered_deleted.lua, and update the FD number if necessary:

-- Pure Lua recovery script for mpv
local in_fd = io.open("/dev/fd/12", "rb")  -- <-- update 12 if needed
local out = io.open("/tmp/recovered.mp4", "wb")
if in_fd and out then
  while true do
    local chunk = in_fd:read(65536)
    if not chunk then break end
    out:write(chunk)
  end
  in_fd:close()
  out:close()
  print("Recovery complete.")
else
  print("Failed to open one of the files.")
end

⚠️ Before running this, be sure to seek to the start of the file in mpv, or you'll only get a partial file.


Run the Lua Script via IPC

Send the script into the running mpv process using its IPC socket (/tmp/MPV_IPC.sock in this example):

echo "load-script '/tmp/mpv_recovered_deleted.lua'" | socat - /tmp/MPV_IPC.sock

You should now see /tmp/recovered.mp4 appear with the full contents of the deleted-but-open file!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment