Skip to content

Instantly share code, notes, and snippets.

@LaptopDev
Last active March 27, 2025 21:35
Show Gist options
  • Save LaptopDev/263a3b2100d3ca6bba1e914486467fe6 to your computer and use it in GitHub Desktop.
Save LaptopDev/263a3b2100d3ca6bba1e914486467fe6 to your computer and use it in GitHub Desktop.
Log MPV watch history
--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)
@LaptopDev
Copy link
Author

LaptopDev commented Mar 15, 2025

The format of the output is:

Month day, year 24-hour-time   'Title'   domain   channel   video_id
  or, if a local video:
Month day, year 24-hour-time   system_local_path

Example:

Mar 13, 2025 21:27:54  'JEREMIAH WATKINS 2nd visit and things get Cajun CRAZY'   youtube   Harland Highway Podcast   OOZYiCn7eWE
Mar 13, 2025 19:54:25  'Jay Dyer on The Cold War'   youtube   Dyer Clips   UaRHhES2SuY
Mar 13, 2025 19:41:09  /home/user/Downloads/video_only_48.mp4
Mar 13, 2025 19:07:48  'Jimbob Karate Chops Secular Atheist'   youtube   MadeByJimbob   aCeUle87b5A
Mar 13, 2025 19:06:10  'SHARK WHEEL PRO hidecomply'   youtube   Shark Wheel   CO4VIy8aKyk
Mar 13, 2025 19:04:18  'The Hardest Tricks In Andy Andersons New Skate Video Hawk vs Wolf skateboarding tonyhawk'   youtube   Hawk vs Wolf   3qoJUbudUvc



Linux

log_last_watched.lua assumes ytdl or yt-dlp is installed and sourced in ~/.config/mpv/mpv.conf like:

# Ensure mpv knows where yt-dlp is
script-opts=ytdl_hook-ytdl_path=/home/user/.local/bin/yt-dlp

The below log file will be written to without adjustment, but requires being created first.

~/.config/mpv/last_watched.txt

Put log_last_watched.lua in ~/.config/mpv/scripts/

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