user input in command line NOT batch scripts


lavinnyv

New member
Local time
9:29 AM
Posts
15
OS
all
i am trying to convert this code from linux/mac to windows:

Batch:
echo -e ""
echo -e "Enter Contrast/Brightness/Gamma number (-100 to 100, 0 default)"
echo -e "CONTRAST: "
read c_num
echo -e "BRIGHTNESS: "
read b_num
echo -e "GAMMA: "
read g_num
cnum_command="echo { \"command\": [\"set_property\", \"contrast\", $c_num] } >\\.\pipe\mpvsocket
bnum_command="echo { \"command\": [\"set_property\", \"brightness\", $b_num] } >\\.\pipe\mpvsocket
gnum_command="echo { \"command\": [\"set_property\", \"gamma\", $g_num] } >\\.\pipe\mpvsocket
eval $cnum_command
eval $bnum_command
eval $gnum_command
echo show-text "Brightness: ${brightness} Contrast: ${contrast} Gamma: ${gamma}" 4000 >\\.\pipe\mpvsocket


- -

i know that in windows it's set /p to set the variable, but when i change things above to do that, it doesn't run properly from the cmd prompt.


to run the above command in linux/mac, i had to use the eval to execute the command. i don't know what the evivelant cmd is in windows or whether that's the right way to set this up in windows. maybe just running the variable for the 3 above commands will work.



in summary, i'm just trying to get a prompt so i can input the level of contrast/brightness/gamma and then once i pick them, send the commands to change those settings.

the above commands for set property and show text are the correct commands for this, i send them by themselves putting the number in where the variable is in the above setup. so i know the codes are fine, i just can't get this to run like i'm used to in other os'.

i don't need to hear that i should put it into a batch script. i run snippets from a terminal command app while logged into multiple hosts, so i want to double click the snippet and then input the levels i need to change locally on all the hosts. so batch scripting this is not what i'm trying to figure out. i'm trying to get this to run from the cmd prompt.

thanks for the help!
 
Last edited by a moderator:

My Computer

System One

  • OS
    all
There's no equivalent of 'eval' in Windows. PowerShell has script blocks, where you can provide arbitrary code lines for in-line execution. But CMD has nothing similar.

I don't run MPV myself, but I peeked at the online docs.
Code:
@echo off
echo:
echo Enter Contrast/Brightness/Gamma number (-100 to 100, 0 default)
echo:

set /p "c_num=Enter CONTRAST: "
set /p "b_num=Enter BRIGHTNESS: "
set /p "g_num=Enter GAMMA: "

(echo { "command": ["set_property", "contrast", %c_num%] }
echo { "command": ["set_property", "brightness", %b_num%] }
echo { "command": ["set_property", "gamma", %g_num%] }
echo show-text "Brightness: %b_num% Contrast: %c_num% Gamma: %g_num%" 4000
) > \\.\pipe\mpvsocket

Here's the output (without the redirection).
Code:
C:\Users\GARLIN\Downloads\LAVI>NEW.bat

Enter Contrast/Brightness/Gamma number (-100 to 100, 0 default)

Enter CONTRAST: 33
Enter BRIGHTNESS: 44
Enter GAMMA: 55

{ "command": ["set_property", "contrast", 33] }
{ "command": ["set_property", "brightness", 44] }
{ "command": ["set_property", "gamma", 55] }
show-text "Brightness: 44 Contrast: 33 Gamma: 55" 4000
 

My Computer

System One

  • OS
    Windows 7
this is where it gets tripped up and why i needed eval on mac/linux.

the command for sending the contrast to mpv is: echo { \"command\": [\"set_property\", \"contrast\", $c_num] } >\\.\pipe\mpvsocket

but when the code gets run -
(echo { "command": ["set_property", "contrast", %c_num%] }
echo { "command": ["set_property", "brightness", %b_num%] }
echo { "command": ["set_property", "gamma", %g_num%] }
echo show-text "Brightness: %b_num% Contrast: %c_num% Gamma: %g_num%" 4000
) > \\.\pipe\mpvsocket

it just echos - { "command": ["set_property", "contrast", %c_num%] etc.

i need to send the command "echo { \"command\": [\"set_property\", \"contrast\", $c_num] } >\\.\pipe\mpvsocket"

that's why i had to create the 2nd variable in my original code -
cnum_command="echo { \"command\": [\"set_property\", \"contrast\", $c_num] } >\\.\pipe\mpvsocket

and then i would "run" the variable cnum_command with eval. but i just need to run/send/execute the code:
"echo { \"command\": [\"set_property\", \"contrast\", $c_num] } >\\.\pipe\mpvsocket"

i can't find a ton of information on how to do this in the cmd.exe. when i search there are a lot of ways to do this on a batch script. but i'm running this from a central control computer while logged in to multiple hosts. and my snippets send command to 1 or all remote hosts. so i could just send the command "echo { \"command\": [\"set_property\", \"contrast\", 50] } >\\.\pipe\mpvsocket and send contrast of 50 to all hosts, but i want to be able to input that into the command vs having to type/copy into terminal. i'm under time constraints to accomplish commands, so things need to be quick for me.

thanks
 

My Computer

System One

  • OS
    all
The CMD (...) construct is like the sub-shell construct in most Linux shells. Everything is piped all at once to the named pipe.

If read the MPV docs, you're supposed to send JSON-formatted text:

Therefore, this JSON would be valid:
Code:
{ "command": ["set_property", "contrast", 33] }
{ "command": ["set_property", "brightness", 44] }
{ "command": ["set_property", "gamma", 55] }
show-text "Brightness: 44 Contrast: 33 Gamma: 55" 4000

Well, except for the show-txt since I couldn't find an online example of the correct form.

I don't understand why you want c_num to be evaluated and stuck inside another echo which wraps the evaluated string with more JSON. That seems wrong to me, and creates incorrect JSON. Since I do read Linux shell script, why not show me how you did this outside of Windows?


UPDATE: Found a correct example for show-text.

Code:
@echo off
echo:
echo Enter Contrast/Brightness/Gamma number (-100 to 100, 0 default)
echo:

set /p "c_num=Enter CONTRAST: "
set /p "b_num=Enter BRIGHTNESS: "
set /p "g_num=Enter GAMMA: "

(echo { "command": ["set_property", "contrast", %c_num%] }
 echo { "command": ["set_property", "brightness", %b_num%] }
 echo { "command": ["set_property", "gamma", %g_num%] }
 echo { "command": ["expand-properties", "show-text", "Contrast: %c_num% Brightness: %b_num% Gamma: %g_num% 4000"] }
) > \\.\pipe\mpvsocket
 
Last edited:

My Computer

System One

  • OS
    Windows 7
Screenshot 2024-05-17 at 1.24.55 PM.png
this is my mac/linux user input command. the connection was refused because i didn't have mpv open on the mac. but it would have just moved to a new prompt.

-

this is what i call my "user input command that can be cut and pasted into mac:

echo -e ""
echo -e "Enter Contrast/Brightness/Gamma number (-100 to 100, 0 default"
echo -e "CONTRAST: "
read c_num
echo -e "BRIGHTNESS: "
read b_num
echo -e "GAMMA: "
read g_num
cnum_command="echo '{ \"command\": [\"set_property\", \"contrast\", $c_num] }' | socat - /tmp/mpvsocket"
bnum_command="echo '{ \"command\": [\"set_property\", \"brightness\", $b_num] }' | socat - /tmp/mpvsocket"
gnum_command="echo '{ \"command\": [\"set_property\", \"gamma\", $g_num] }' | socat - /tmp/mpvsocket"
eval $cnum_command
eval $bnum_command
eval $gnum_command
echo 'show-text "Brightness: ${brightness} Contrast: ${contrast} Gamma: ${gamma}" 4000' | socat - /tmp/mpvsocket

-

this is the snippet i create in my terminal app to send to mac and linux:

echo -e "" && echo -e "\e[1mEnter Contrast/Brightness/Gamma number\e[0m (\e[44m-100 to 100, 0 default\e[0m)" && echo -e "\e[1mCONTRAST:\e[0m " && read c_num && echo -e "\e[1mBRIGHTNESS:\e[0m " && read b_num && echo -e "\e[1mGAMMA:\e[0m " && read g_num && cnum_command="echo '{ \"command\": [\"set_property\", \"contrast\", $c_num] }' | socat - /tmp/mpvsocket" && bnum_command="echo '{ \"command\": [\"set_property\", \"brightness\", $b_num] }' | socat - /tmp/mpvsocket" && gnum_command="echo '{ \"command\": [\"set_property\", \"gamma\", $g_num] }' | socat - /tmp/mpvsocket" && eval $cnum_command && eval $bnum_command && eval $gnum_command && echo 'show-text "Brightness: ${brightness} Contrast: ${contrast} Gamma: ${gamma}" 4000' | socat - /tmp/mpvsocket

-

when i was building this user input command, without the eval command, when i would run "echo '{ \"command\": [\"set_property\", \"contrast\", $c_num] }' | socat - /tmp/mpvsocket" it would just be echo'd and not run. so that's why i created a second variable - num_command and that would pull the echo command together with the number variable, then i just eval'd the num_command so it was cleaner.

-

here is what i use to just send a command when if i already have a number in mind and don't need to use the variable input command:

echo '{ "command": ["set_property", "contrast", -5] }' | socat - /tmp/mpvsocket && echo 'show-text "Contrast: ${contrast}" 3000' | socat - /tmp/mpvsocket

*show text is just for me to confirm i made the changes

this is the base command that i send while logged in to remote hosts, whether mac or linux:

echo '{ "command": ["set_property", "contrast", -5] }' | socat - /tmp/mpvsocket

thanks
 

My Computer

System One

  • OS
    all
If you spend a few minutes researching mpv-player examples on Windows:

1. There is no acceptable socat equivalent for Windows. The normal solution is echoing to the named pipe "\\.\pipe\mpvsocket". You can send commands, but not read responses (not bi-directional).
Someone proposed a PowerShell solution: IPC: socat equivalent on Windows #10116

2. Someone else mentioned it might be possible to use UNC \\hostname or \\192.168.0.10\ notation to reach remote Windows hosts, assuming the firewall doesn't block this traffic.

3. CMD echo doesn't support the same character escaping as Linux. Keep it simple.

4. If you're not allowed to provide multiple JSON commands in a single pipe session, the script reverts to:
Code:
@echo off
echo:
echo Enter Contrast/Brightness/Gamma number (-100 to 100, 0 default)
echo:

set /p "c_num=Enter CONTRAST: "
set /p "b_num=Enter BRIGHTNESS: "
set /p "g_num=Enter GAMMA: "

echo { "command": ["set_property", "contrast", %c_num%] } > \\.\pipe\mpvsocket
echo { "command": ["set_property", "brightness", %b_num%] } > \\.\pipe\mpvsocket
echo { "command": ["set_property", "gamma", %g_num%] } > \\.\pipe\mpvsocket
echo { "command": ["expand-properties", "show-text", "Contrast: %c_num% Brightness: %b_num% Gamma: %g_num% 4000"] }> \\.\pipe\mpvsocket

I haven't heard you mention if any of this has been tested on Windows.
 

My Computer

System One

  • OS
    Windows 7
i understand that mpv on windows is not bi directional. i have spent probably 100 hours researching how to send mpv commands to a windows host. and i have compiled 100s of commands for mpv to send and they all work. i just send the command - echo { "command": ["set_property", "contrast", -50] } > \\.\pipe\mpvsocket and i get mpv on the host to come down 50% on contrast.

i'm not trying to figure out how to send commands to windows hosts over ssh. what i'm having trouble with is building a command that asks for user input for the variable of contrast and then sends the command. windows is not my primary OS for working with remote hosts. i did not test the most recent example you sent. but the first one you sent last week, i did test and it didn't work for me.

so maybe i should have started with a more simplified example, then i could attempt to build it out:

@echo off
echo:
echo Enter Contrast number (-100 to 100, 0 default)
echo:

set /p "c_num=Enter CONTRAST: "

echo { "command": ["set_property", "contrast", %c_num%] } > \\.\pipe\mpvsocket

my windows computer is disconnected at the moment - i had to do some linux work. but i hope to test out your latest example later today.

using code that i have to connect to using hostname/ip isn't an option as i have to sync my commands to many host, so i'm using terminals that have a "broadcast" option. so i log into many hosts with ssh and then broadcast and mpv command to all of them. if i just use a terminal with something like ssh host1.local cmd, ssh host2.local cmd, the commands run in order, so they do not sync up.

thanks for the help on this.
 

My Computer

System One

  • OS
    all
This sounds like your own dev project, which I hope you find success.
 

My Computer

System One

  • OS
    Windows 7
not python. but i have considered powershell. i'm just try to keep everything in cmd, because that's how i'm sending most of the other commands that i'm using.
 

My Computer

System One

  • OS
    all
so this works to somewhat complete the task. i had to combine it with && because it was jumping past my user prompts:

echo : && echo Enter Contrast/Brightness/Gamma number (-100 to 100, 0 default) && echo : && set /p "c_num=Enter CONTRAST: " && set /p "b_num=Enter BRIGHTNESS: " && set /p "g_num=Enter GAMMA: " && echo "%c_num%", "%b_num%", "%g_num%" && echo { "command": ["set_property", "contrast", %c_num%] } > \\.\pipe\mpvsocket && echo { "command": ["set_property", "brightness", %b_num%] } > \\.\pipe\mpvsocket && echo { "command": ["set_property", "gamma", %g_num%] } > \\.\pipe\mpvsocket && echo { "command": ["expand-properties", "show-text", "Contrast: %c_num% Brightness: %b_num% Gamma: %g_num% 4000"] }> \\.\pipe\mpvsocket


the only problem i'm running into now is that it's holding the variable from my previous commands. so if i run it the first time and put -20 on all, it changes the settings by -20. if i run it right after and use 0 to bring everything back to normal (or use any other number) it's still holding the -20 as my variable. if i close the terminal, then restart it clears the variable. but when i'm doing this to 20+ hosts, that's not going to cut it for me.

i looked into setlocal EnableDelayedExpansion but i couldn't get that to work. i'm still researching how to solve this issue of the variable holding the previous setting.

thanks for the help, this is progressing.
 

My Computer

System One

  • OS
    all
not python. but i have considered powershell. i'm just try to keep everything in cmd, because that's how i'm sending most of the other commands that i'm using.
Converting the commands to PowerShell should be relatively easy. Managing your variables will be a lot simpler in PowerShell. I think it's worth making the change.
 

My Computer

System One

  • OS
    Windows 10/11
    Computer type
    Laptop
    Manufacturer/Model
    Acer
lavinnyv
For delayed expansion on command prompt, does something like this work? Although this works for me on a single running instance of mpv.exe, I don't know how it might affect any other commands you are using for connecting to multiple instances of mpv.
Code:
cmd /E:ON /V:ON /c "echo Enter Contrast/Brightness/Gamma number (-100 to 100, 0 default) && echo : && set /p "c_num=Enter CONTRAST: " && set /p "b_num=Enter BRIGHTNESS: " && set /p "g_num=Enter GAMMA: " && echo "!c_num!", "!b_num!", "!g_num!" && echo { "command": ["set_property", "contrast", !c_num!] } > \\.\pipe\mpvsocket && echo { "command": ["set_property", "brightness", !b_num!] } > \\.\pipe\mpvsocket && echo { "command": ["set_property", "gamma", !g_num!] } > \\.\pipe\mpvsocket && echo { "command": ["expand-properties", "show-text", "Contrast: !c_num! Brightness: !b_num! Gamma: !g_num! 4000"] }> \\.\pipe\mpvsocket"

ref:

pn: what does the "4000" do in the above command?
 

My Computer

System One

  • OS
    Windows 11 23H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    custom
    CPU
    intel i7-8700 (non-K)
    Motherboard
    Asus Z370 TUF Gaming
    Memory
    32Gb
    Graphics Card(s)
    On-board Intel iGPU
    Sound Card
    On-board Realtek
    Hard Drives
    Samsung_SSD_850_EVO
    PSU
    Corsair Rm850X
    Cooling
    All air
thanks i will see if that works.

this was the original code:
echo show-text "Brightness: ${brightness} Contrast: ${contrast} Gamma: ${gamma}" 4000 >\\.\pipe\mpvsocket

=4000 is for how long to display on the OSD

this code got a little changed in this forum to this:
echo { "command": ["expand-properties", "show-text", "Contrast: %c_num% Brightness: %b_num% Gamma: %g_num% 4000"] }> \\.\pipe\mpvsocket

=and now the 4000 is just displayed as text after the other numbers. i haven't figured out to get it back to being the OSD time yet in the second command here. i'm just leaving it as is for now and i'm going to work on that part when i get the variable part working properly.
 

My Computer

System One

  • OS
    all
YES! that worked. thank you!

i tested out using setlocal EnableDelayedExpansion at the top of the command. i hadn't found the stack overflow page yet with the cmd /E:ON /V:ON solution.
 

My Computer

System One

  • OS
    all

LesFerch - could you give me an example of switching this cmd code to powershell? switching all my commands to powershell will be a lot of work, but it has been on my future list to look in to.​

 

My Computer

System One

  • OS
    all
could you give me an example of switching this cmd code to powershell?

Example 1:
Powershell:
cmd.exe /c start mpv "C:\Movies\Test.mkv" --input-ipc-server=/mpvsocket
$brightness = Read-Host "Enter brightness"
$contrast = Read-Host "Enter contrast"
$gamma = Read-Host "Enter gamma"
$cmd = 'echo { "command": ["set_property", "brightness","' + $brightness + '"] }> \\.\pipe\mpvsocket'
cmd.exe /c $cmd
$cmd = 'echo { "command": ["set_property", "contrast","' + $contrast + '"] }> \\.\pipe\mpvsocket'
cmd.exe /c $cmd
$cmd = 'echo { "command": ["set_property", "gamma","' + $gamma + '"] }> \\.\pipe\mpvsocket'
cmd.exe /c $cmd

Example 2:
Powershell:
cmd.exe /c start mpv "C:\Movies\Test.mkv" --input-ipc-server=/mpvsocket
$brightness = Read-Host "Enter brightness"
$contrast = Read-Host "Enter contrast"
$gamma = Read-Host "Enter gamma"
$c1 = 'echo { "command": ["set_property", "'
$c2 = '","'
$c3 = '"] }> \\.\pipe\mpvsocket'
$cmd = $c1 + 'brightness' + $c2 + $brightness + $c3
cmd.exe /c $cmd
$cmd = $c1 + 'contrast' + $c2 + $contrast + $c3
cmd.exe /c $cmd
$cmd = $c1 + 'gamma' + $c2 + $gamma + $c3
cmd.exe /c $cmd

Example 3:
Powershell:
cmd.exe /c start mpv "C:\Movies\Test.mkv" --input-ipc-server=/mpvsocket
$brightness = Read-Host "Enter brightness"
$contrast = Read-Host "Enter contrast"
$gamma = Read-Host "Enter gamma"
cmd.exe /c "echo { `"command`": [`"set_property`", `"brightness`",`"$brightness`"] }> \\.\pipe\mpvsocket"
cmd.exe /c "echo { `"command`": [`"set_property`", `"contrast`",`"$contrast`"] }> \\.\pipe\mpvsocket"
cmd.exe /c "echo { `"command`": [`"set_property`", `"gamma`",`"$gamma`"] }> \\.\pipe\mpvsocket"
 

My Computer

System One

  • OS
    Windows 10/11
    Computer type
    Laptop
    Manufacturer/Model
    Acer
thank you. i'm going to dig into this soon.

i'm actually doing this over ssh (and broadcasting to many remote hosts), so i have to start all .exe's with psexec.

*unless there is a better way to launch an .exe on a remote host with powershell. i haven't got to that research yet.

-

if anyone is interested:

#to launch in my path:
psexec64 -d -i -nobanner 1 "mpv.exe" "C:/Users/home/PB_Show/PB_Folder/Playback/PB_Playlists/win_playlists/default_playlist.m3u8"

launches my playlist, but could just be the .mkv/.mp4 file

#to use the powershell commands, mostly likely would just need to put -

psexec64 -d -i -nobanner 1

in front of all the cmd.exe's. but i haven't tested this yet, i will soon.


** and for ease of use, i put the ipc in my mpv.conf file so it's piped every time mpv opens.

#start with pipe
input-ipc-server=\\.\pipe\mpvsocket
 

My Computer

System One

  • OS
    all
Back
Top Bottom