Last active
March 27, 2025 21:35
-
-
Save LaptopDev/263a3b2100d3ca6bba1e914486467fe6 to your computer and use it in GitHub Desktop.
Log MPV watch history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--https://chatgpt.com/c/67e44b59-292c-800b-80b6-3e6b1c609524 | |
local mp = require("mp") | |
local utils = require("mp.utils") | |
local log_file = utils.join_path(os.getenv("HOME"), ".config/mpv/last_watched.txt") | |
-- Function to log current playback | |
local function log_last_watched() | |
local path = mp.get_property("path") | |
if not path then return end | |
local date = os.date("%b %d, %Y %H:%M:%S") | |
local log_entry = "" | |
if path:match("^https?://") then | |
-- Online video: use yt-dlp to get metadata | |
local command = { | |
"yt-dlp", "--no-config", "--skip-download", | |
"--print", "%(title)s\t%(uploader)s\t%(id)s", | |
path | |
} | |
local result = utils.subprocess({ args = command }) | |
if result.status == 0 and result.stdout then | |
local title, uploader, video_id = result.stdout:match("([^\t]+)\t([^\t]+)\t([^\t]+)") | |
if title and uploader and video_id then | |
log_entry = string.format("%s '%s' youtube %s %s", date, title, uploader, video_id) | |
end | |
end | |
-- Fallback if yt-dlp fails | |
if log_entry == "" then | |
local fallback_title = mp.get_property("media-title") or "Unknown Title" | |
log_entry = string.format("%s '%s' youtube UnknownUploader UnknownID", date, fallback_title) | |
end | |
else | |
-- Local file | |
log_entry = string.format("%s %s", date, path) | |
end | |
-- Asynchronous write (prepend entry) | |
mp.add_timeout(0.1, function() | |
local existing = "" | |
local file = io.open(log_file, "r") | |
if file then existing = file:read("*all"); file:close() end | |
file = io.open(log_file, "w") | |
if file then | |
file:write(log_entry .. existing) | |
file:close() | |
end | |
end) | |
end | |
mp.register_event("file-loaded", log_last_watched) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The format of the output is:
Example:
Linux
log_last_watched.lua
assumesytdl
oryt-dlp
is installed and sourced in~/.config/mpv/mpv.conf
like:The below log file will be written to without adjustment, but requires being created first.
Put
log_last_watched.lua
in ~/.config/mpv/scripts/