Skip to content

Instantly share code, notes, and snippets.

@xarblu
Last active January 16, 2025 20:33
Show Gist options
  • Select an option

  • Save xarblu/3cad5763ab699f331559d736c2462db0 to your computer and use it in GitHub Desktop.

Select an option

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)
@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