mpv icon indicating copy to clipboard operation
mpv copied to clipboard

Hotkey to scale the video to "Fit the screen"

Open homever opened this issue 11 years ago • 27 comments

I know how to set the initial player widow size by autofit=100%x100% But, is there a way to create a hotkey in input.conf to achieve this? I am on a Mac. Hope this feature can be added using "CMD+3" to "Fit the screen size", but not in full screen mode.

Thanks.

homever avatar Sep 09 '15 13:09 homever

Use fullscreen mode?

ghost avatar Sep 09 '15 14:09 ghost

Or rather, what's the problem with fullscreen mode?

ghost avatar Sep 09 '15 14:09 ghost

hmm, consider it a low-priority feature request :dango:

homever avatar Sep 09 '15 14:09 homever

Well, I still don't understand the purpose. Fullscreen mode does this, as far as I understand.

ghost avatar Oct 01 '15 20:10 ghost

I will have to keep monitoring the servers tasking all time by getting a visual UL/DL speed on Menubar/Dock.

homever avatar Oct 05 '15 07:10 homever

I think he doesn't want the video to be enveloped when fullscreen if the aspect is longer than his display.

bitingsock avatar Oct 23 '15 23:10 bitingsock

I was trying to find this feature, too. For me, instead of binding set window-scale 1 with command+1, I would rather bind it with something which is same as autofit=50%x50% and bind command+2 with autofit=90%x90%, or some other ratio. It could be in pixels, too. Thanks for considering to add this feature.

wygsephi avatar Nov 02 '15 10:11 wygsephi

Try --panscan=1.0

chella1cm avatar Nov 25 '15 13:11 chella1cm

mpc-hc allows this using alt+4 (or alt+5) bindings, which can roughly be translated to --autofit=75%x75% and --autofit-larger=80%x80% (when considering entire window size and usually constrained by heigth) respectively.

I would love to have the ability to define commands for these "options" and thus hotkeys.

FichteFoll avatar May 20 '16 21:05 FichteFoll

I made a script which looks like this:

function fit_to_screen()
    scale = 1440.0 / mp.get_property("width")
    mp.set_property("window-scale", scale)
end

mp.add_key_binding("meta+9", "Fit_Screen", fit_to_screen)

You can change "1440.0" to your screen width -- if you are using a macbook it should be the scaled resolution, not the native resolution.

Two problem remaining:

  1. The screen width is hardcoded -- I can't figure out how to detect automatically. It's annoying if you sync your config over multiple devices.
  2. The window position is often incorrect -- I can't figure out how to move the window with script. You'll need to move the window after resized.

Hope it helps.

asd123cqp avatar Nov 12 '16 05:11 asd123cqp

Shouldn't the window manager be doing this? On xmonad I just hit meta+f and my current window will cover the entire display (sans the informational bar at the top).

haasn avatar Nov 12 '16 12:11 haasn

I am not so sure about xmonad on OSX...

asd123cqp avatar Nov 15 '16 06:11 asd123cqp

I think Cmd-Z (Window » Zoom) already does this. Testing on 10.12.1 / mpv-0.22.0, it will shrink or grow the window such that it is as large as possible without going off the edge of the screen. Or am I misunderstanding the feature request?

FrostedMint avatar Nov 29 '16 05:11 FrostedMint

@FrostedMint you are absolutely right, except that cmd+z won't work if you use the "border=no" option.

In fact, I think that the real problem is the native window manager in OSX will become invalid when using "border=no" option.

asd123cqp avatar Nov 29 '16 05:11 asd123cqp

@FrostedMint This is just brilliant 💃

homever avatar Dec 02 '16 04:12 homever

Edit: The final version of the script has been published on the wiki, along with a few other scripts:

https://github.com/mpv-player/mpv/wiki/User-Scripts

  • auto-keep-gui-open
    Intelligently switches mpv's "keep-open" behavior based on whether you are running in video-mode or audio-only mode.

  • cycle-video-rotate
    Allows you to perform video rotation which perfectly cycles through all 360 degrees without any glitches.

  • multi-command-if
    Very powerful conditional logic and multiple action engine for your keybindings, without having to write a single line of code!

  • quick-scale
    Quickly scale the video player to a target size, with full control over target scale and max scale. Helps you effortlessly resize a video to fit on your desktop, or any other video dimensions you need!


Old post:

@asd123cqp You're right... Cmd+Z does nothing in MPV for OS X if No-border mode is used...

I am therefore instead using the code you created. But I modified it to check the screen width AND height (to support VERTICAL videos) and to choose the smallest scale necessary to fit the video within the screen. There's no dock/menubar taken into account but whatever, this is better than having NO ability to "zoom to fit" when in no-border mode!

function fit_to_screen()
	-- supports both horizontal and vertical videos and picks smallest scale
	-- IMPORTANT: SET THIS TO YOUR NON-RETINA SCREEN RESOLUTION.
	widthscale = 1680.0 / mp.get_property("width")
	heightscale = 1050.0 / mp.get_property("height")
	scale = (widthscale < heightscale and widthscale or heightscale)
	mp.set_property_number("window-scale", scale)
end

mp.add_key_binding("meta+9", "Fit_Screen", fit_to_screen)

Save it to ~/.config/mpv/scripts/fit_to_screen.lua, so that it is auto-loaded every time mpv runs. Then just use Cmd+9 to "fit to screen".

PS: Another VERY worthwhile thing is to edit your mpv.conf and add these two lines:

geometry=50%:50%
autofit-larger=100%x100%

The first line ensures that the player starts in a centered position on the screen.

The second line means that if you open a video that would have been LARGER than your current screen's resolution, MPV will automatically scale it to be no wider/taller than 100% of your screen width/height (without changing aspect ratio). "autofit-larger" doesn't do anything at all if the video is smaller than the desktop. So this is a great solution for ensuring that massive videos (like 4K videos) don't play larger than your desktop size whenever you open MPV.

ghost avatar Dec 04 '16 03:12 ghost

I tried to extend the script to also center the player. I think I may have found a bug.

I won't open a separate ticket about this, until I know whether this is a BUG on OS X:

mp.set_property("geometry", "50%:50%")

Does nothing. It's supposed to center the player. But maybe it doesn't work on ANY OS after the playback has already begun? Or does it work on Windows/Linux and this is just an OS X bug?

Edit: Also tried with input.conf. Fails there too. Does nothing except show "geometry: +50%+50%" on the OSD:

Meta+5 set geometry 50%:50%

ghost avatar Dec 04 '16 05:12 ghost

geometry is normally only read when the window is first shown, on all platforms.

ghost avatar Dec 04 '16 11:12 ghost

@wm4 Thanks for clarifying that it isn't a bug.

ghost avatar Dec 04 '16 19:12 ghost

Edit: The final version of the script has been published on the wiki, along with a few other scripts:

https://github.com/mpv-player/mpv/wiki/User-Scripts

  • auto-keep-gui-open
    Intelligently switches mpv's "keep-open" behavior based on whether you are running in video-mode or audio-only mode.

  • cycle-video-rotate
    Allows you to perform video rotation which perfectly cycles through all 360 degrees without any glitches.

  • multi-command-if
    Very powerful conditional logic and multiple action engine for your keybindings, without having to write a single line of code!

  • quick-scale
    Quickly scale the video player to a target size, with full control over target scale and max scale. Helps you effortlessly resize a video to fit on your desktop, or any other video dimensions you need!


Old post:

@asd123cqp Here's the final code I came up with. Lets me scale the window to any bounds, automatically. Fullscreen. Small box. Etc. It is all easy.

--- Quickly scale the video player to a target size.
--
-- Parameters:
-- targetwidth = How wide you want the target area to be.
-- targetheight = How tall you want the target area to be.
-- targetscale = If this is 1, we use your target width/height
--   as-is, but if it's another value then we scale your provided
--   target size by that amount. This parameter is great if you want
--   a video to be a certain percentage of your desktop resolution.
--   In that case, just set targetwidth/targetheight to your
--   desktop resolution, and set this targetscale to the percentage
--   of your desktop that you want to use for the video, such as
--   "0.25" to resize the video to 25% of your desktop resolution.
-- maxvideoscale = If this is a positive number (anything above 0),
--   then the final video scale cannot exceed this number.
--   This is useful if you for example set the target to 25%
--   of your desktop resolution. If the video is smaller than that,
--   then it would be scaled up (enlarged) to the size of the target.
--   To control that behavior, simply set this parameter.
--   Here are some examples:
--    -1, 0, or any other non-positive number: We'll enlarge
--      too-small videos and shrink too-large videos. Small videos
--      will be enlarged as much as needed to match target size.
--    1: Video will only be allowed to enlarge to 100% of its natural size.
--      This means that small videos won't become big and blurry.
--    1.5: Video will only be allowed to enlarge to 150% of its natural size.
function quick_scale(targetwidth, targetheight, targetscale, maxvideoscale)
    -- Check parameter existence.
    if (targetwidth == nil or targetheight == nil
            or targetscale == nil or maxvideoscale == nil)
    then
        mp.osd_message("Quick_Scale: Missing parameters")
        return nil -- abort
    end

    -- Ensure that the incoming strings are valid numbers.
    targetwidth = tonumber(targetwidth)
    targetheight = tonumber(targetheight)
    targetscale = tonumber(targetscale)
    maxvideoscale = tonumber(maxvideoscale)
    if (targetwidth == nil or targetheight == nil
            or targetscale == nil or maxvideoscale == nil)
    then
        mp.osd_message("Quick_Scale: Non-numeric parameters")
        return nil -- abort
    end

    -- If the target scale isn't 1 (100%), we'll re-calculate target size.
    if (targetscale ~= 1) then
        targetwidth = targetwidth * targetscale
        targetheight = targetheight * targetscale
    end

    -- Find smallest video scale that fits target size in both width and height.
    -- This only looks at video and doesn't take window borders into account!
    widthscale = targetwidth / mp.get_property("width")
    heightscale = targetheight / mp.get_property("height")
    local scale = (widthscale < heightscale and widthscale or heightscale)

    -- If we arrived at a target width/height that is larger than the video's
    -- natural "100%" scale, then we may want to limit it to a maximum amount.
    if (maxvideoscale > 0 and scale > maxvideoscale) then
        scale = maxvideoscale
    end

    -- Apply the new video scale.
    mp.set_property_number("window-scale", scale)
end

-- Bind this via input.conf. Examples:
--   To fit a video to 100% of a 1680x1050 desktop size, with unlimited video enlarging:
--     Alt+9 script-message Quick_Scale "1680" "1050" "1" "-1"
--   To fit a video to 80% of a 1680x1050 desktop size, but disallowing the
--     video from becoming larger than 150% of its natural size:
--     Alt+9 script-message Quick_Scale "1680" "1050" "0.8" "1.5"
--   To fit a video to a 200x200 box, with unlimited video enlarging:
--     Alt+9 script-message Quick_Scale "200" "200" "1" "-1"
mp.register_script_message("Quick_Scale", quick_scale)

ghost avatar Dec 14 '16 19:12 ghost