Share part of my personal project for the YTZF youtube searching tool (from terminal in Mabox when you look for youtube vids/music by typing: ytfzf -t your query) so as to have youtube or other similar music/video streams display playing in mpv player.
Problem: whenever you use from terminal ytfzf (using also ytdlp) to search for & play youtube videos… you can’t get a specific resolution quality or size simply, cause you need to tweak or be savy of tricks to modify mpv settings or ytdlp settings after a long google search trip.
So what if you could add to your mpv’s folder named as scripts (/home/mabox/.config/mpv/scripts) a simply 81 lines lua script to preset the resolution quality & sizing to be displayed by mpv output whenever you use ytfzf as youtube searches.
You don’t need extra packages to add to Mabox, all dependencies are installed by default.
You just need to add this 85 lines lua script in the mpv’s scripts folder, or add this scripts folder in case you don’t have it created in /home/mabox/.config/mpv/.
Maybe you already created this folder for my previous lua script (notify-lua), if so just add this new one accompanying notify-lua script as shown in this picture: https://i.imgur.com/QNiP1Ew.png
I named this new script as ytfzfmpvsetting.lua if you want to keep the same name.
There are important information 13 lines to check at the beginning of this new script with sections which can be modified by the user at one’s taste. Please read them.
I have incorporated some new stream sources between lines 23-24 after a month testing the script as requested by some testing guys team ( youtu.be, twitch.tv, yewtu.be) so as to get more choices to find your musical video queries.
https://i.imgur.com/hJ06uFm.png
I have preset this new script with the smallest quality resolution (240) set as default. In case you want to modify it , follow the instructions inside the info script file lines after Options.
Finally, if you don’t know much of lua scripting I recommend that you keep the rest of this script as it is to have it functional for your system.
Copy all lines from the script below and paste it in a new file named ytfzfmpvsettings.lua inside /home/mabox/.config/mpv/scripts in you Mabox OS
Have fun & good luck.
--[[
- Part of personal video project in November-December 2022, Nick0bre Chile shared for Maboxlinux users. Maboxlinux.org Forum.
Mpv lua script to automatically change ytdl-format (for yt-dlp) when using/running ytfzf script from terminal (e.g. ytfzf -t The Beatles / e.g. ytfzf
-m The Rolling Stones, so as to watch/listen Youtube tunes/channels from Mpv player & presetting your best resolution manually).
Options:
- To add more domains, simply add them to the StreamSource set between 'xxx.com' & separated by a comma excepting for the last domain added in line 24.
- To adjust quality, edit local changedQuality value in line 28 from the options stated above in line 27 (240, 360, 480, 720, 1080, 1440, or 2160).
- It is recommended to set VP9 codec as false to keep video framerate solid & running most of the time. But if you want to enable VP9 codec,
change enableVP9 to true in line 31.
- To change frame rate, adjust FPSLimit in line 32 at your own risk, default is 30 & works great.
- I recommend to keep the rest of this script as it is so as to have it 100% functional, in case you have not used/ or worked lua scripting before.
--]]
local function Set (t)
local set = {}
for _, v in pairs(t) do set[v] = true end
return set
end
-- Domains list for custom quality
local StreamSource = Set {
'youtu.be', 'youtube.com', 'www.youtube.com',
'twitch.tv', 'yewtu.be', 'www.twitch.tv'
}
-- Accepts: (240, 360, 480, 720, 1080, 1440, 2160) ( The smallest quality is set as deafult now)
local changedQuality = 240
-- Affects matched and non-matched domains
local enableVP9 = false
local FPSLimit = 30
-- Do not edit from here on
local msg = require 'mp.msg'
local utils = require 'mp.utils'
local VP9value = ""
if enableVP9 == false then
VP9value = "[vcodec!=?vp9]"
end
local ytdlChange = "bestvideo[height<=?"..changedQuality.."][fps<=?"..FPSLimit.."]"..VP9value.."+bestaudio/best[height<="..changedQuality.."]"
local ytdlDefault = "bestvideo[fps<=?"..FPSLimit.."]"..VP9value.."+bestaudio/best"
local function getStreamSource(path)
local hostname = path:match '^%a+://([^/]+)/' or ''
return hostname:match '([%w%.]+%w+)$'
end
local function ytdlAutoChange(name, value)
local path = value
if StreamSource[getStreamSource(string.lower(path))] then
mp.set_property("ytdl-format", ytdlChange)
msg.info("Domain match found, ytdl-format has been changed.")
msg.info("Changed ytdl-format: "..mp.get_property("ytdl-format"))
else
msg.info("No domain match, ytdl-format unchanged.")
end
mp.unobserve_property(ytdlAutoChange)
msg.info("Finished check, script no longer running.")
end
local function ytdlCheck()
local path = mp.get_property("path", "")
if string.match(string.lower(path), "^(%a+://)") then
mp.set_property("ytdl-format", ytdlDefault)
msg.info("Current ytdl-format: "..mp.get_property("ytdl-format"))
mp.observe_property("path", "string", ytdlAutoChange)
msg.info("Observing path to determine ytdlAutoChange status...")
else
msg.info("Not a URL/Stream, script did not run.")
end
end
mp.register_event("start-file", ytdlCheck)