Skip to content

Instantly share code, notes, and snippets.

@xarblu
Last active March 26, 2024 13:49
Show Gist options
  • Save xarblu/3cad5763ab699f331559d736c2462db0 to your computer and use it in GitHub Desktop.
Save xarblu/3cad5763ab699f331559d736c2462db0 to your computer and use it in GitHub Desktop.
A very simple script for mpv that allows playing YouTube audio while displaying the videos thumbnail.
--Very simple mpv script that disables video on YouTube videos
--and instead shows the video thumbnail.
--Because there is no toggle for this I recommend only calling
--the script via the "--scripts-append" flag unless you always
--want to just see video thumbnails.
--Set this to 1 if you use Windows
windows = 0
function thumbnail_as_cover()
--check if the video is from YouTube
path = mp.get_property("path")
is_yt = string.find(path, "www.youtube.com")
if ( is_yt == nil )
then
return
else
print("YouTube video detected! Using thumbnail as cover!")
--generate a url to the thumbnail file
vid_id = mp.get_property("filename")
vid_id = string.gsub(vid_id, "watch%?v=", "") -- Strip possible prefix.
vid_id = string.sub(vid_id, 1, 11) -- Strip possible suffix.
thumb_url_maxres1 = string.format("https://i.ytimg.com/vi_webp/%s/maxresdefault.webp", vid_id)
thumb_url_maxres2 = string.format("https://i.ytimg.com/vi/%s/maxresdefault.jpg", vid_id)
thumb_url_sd = string.format("https://i.ytimg.com/vi_webp/%s/sddefault.webp", vid_id)
thumb_url_hq = string.format("https://i.ytimg.com/vi_webp/%s/hqdefault.webp", vid_id)
--ensuring we only grab the audio
mp.set_property("ytdl-format", "bestaudio")
if ( windows == 1 )
then
--set the yt thumbnail as cover art (Windows)
thumb_urls = thumb_url_maxres1 .. ";" .. thumb_url_maxres2 .. ";" .. thumb_url_sd .. ";" .. thumb_url_hq
mp.set_property("cover-art-files", thumb_urls)
else
--set the yt thumbnail as cover art (Linux)
thumb_urls = thumb_url_maxres1 .. ":" .. thumb_url_maxres2 .. ":" .. thumb_url_sd .. ":" .. thumb_url_hq
thumb_urls = string.gsub(thumb_urls, "://", [[\://]])
mp.set_property("cover-art-files", thumb_urls)
end
end
end
--start on every new file in a playlist
mp.register_event("start-file", thumbnail_as_cover)
@xarblu
Copy link
Author

xarblu commented Dec 23, 2021

If you get some 404 not found errors that's totally fine. Just means that the video you're trying to see doesn't have any high res thumbnails available and thus skips to the next best option.

@zaza42
Copy link

zaza42 commented Dec 24, 2021

Here is a template for detecting os:

local platform = nil --set to 'linux', 'windows' or 'macos' to override automatic assign

if not platform then
  local o = {}
  if mp.get_property_native('options/vo-mmcss-profile', o) ~= o then
    platform = 'windows'
  elseif mp.get_property_native('options/input-app-events', o) ~= o then
    platform = 'macos'
  else
    platform = 'linux'
  end
end

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