media/vlc/ VlcHttpInterface


Enabling

Enable via preferences (show all). When logging in from a browser, leave the user field blank and just enter the password. You can specify the port and password via the --http-port and --http-password arguments.

Default http port

Windows: in %appdata%/vlc edit vlcrc, if necessary uncomment the http-port line and set it accordingly:

http-port=7999

Convenience scripts

Php and Chrome

For convenience, I setup a 'search engine' in Chrome so that vl hostname/port expands to http://my-pi/vlc/host/port which, in turn, is serviced by the following php:

<?php
$a = $_SERVER['REQUEST_URI'];
function vlc_redirect($host,$port) {
  $url = "http://:CorrectHorseBatteryStaple@$host:$port/";
  #echo "Redirect: $url\n";
  #exit();
  header('Location: '.$url);
  exit();
}
function get_port($a) {
  $n = intval($a);
  if( $n < 1024 ) {
    $n += 8000;
  }
  return $n;
}
function get_host($host) {
  global $hosts;
  if( array_key_exists($host,$hosts) ) {
    return $hosts[$host];
  } else {
    return $host;
  }
}
$hostre = "[a-zA-Z0-9]+";
$hosts = [ "lh" => "localhost", "t" => "randomturnip", "b" => "bumblesnarf" ]; # shorthands for common host names
if( preg_match("@^/vlc/($hostre)/(\d+)$@",$a,$m) ) {
  $host = get_host($m[1]);
  $port = get_port($m[2]);
  vlc_redirect($host,$port);
} else if( preg_match("@^/vlc/(\d+)$@",$a,$m) ) {
  $host = "localhost";
  $port = get_port($m[1]);
  #echo "$host $port";
  vlc_redirect($host,$port);
} else if(preg_match("@^/($hostre)/(\d+)$@",$a,$m)) {
  $host = get_host($m[1]);
  $port = get_port($m[2]);
  vlc_redirect($host,$port);
} else {
  echo "invalid port: use http://my-pi/\$vlc_host/\$port_no<br/>
    or http://my-pi/vlc/\$vlc_host/\$port_no\n";
  exit();
}

and the following .htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^vlc/.* vlc_host_login.php [L]

Telnet and Expect

I have Cygwin installed, and expect installed. Then I have the following expect and Python scripts.

Use via tvlc hostname portoffset e.g. tvlc t410 3 for e.g. port 7003 (I put all my vlc instances in the range 7000...7199).

#!/usr/bin/expect
set timeout 5

set host "[lindex $argv 0]"
set tport "[lindex $argv 1]"
puts "tport '$tport'"
if { $tport == "" } {
  set tport 0
}
set port "[expr 7000 + $tport]"
set password "tree"

spawn telnet $host $port
expect "Password:"
send "$password\r"

expect "Welcome, Master"

interact

Use via tvlcmd hostname portoffset "command 1 with args" "command 2 with args" where each argument is one line of input for the telnet session.

#!/usr/bin/python
from subprocess import run, Popen, PIPE
import sys
import os
import time
import tempfile
import platform

if platform.system() == "Darwin":
  telnet = "gtelnet"
else:
  telnet = "telnet"

a = r"""set timeout 5

set host "[lindex $argv 0]"
set tport "[lindex $argv 1]"
puts "tport '$tport'"
if { $tport == "" } {
  set tport 0
}
set port "[expr 7000 + $tport]"
set password "tree"

spawn TELNET $host $port
expect {
  timeout { puts "timeout pw" ; exit 1 }
  "Password:"
}
send "$password\r"

expect {
  timeout { puts "timeout welcome" ; exit 1 }
  "Welcome, Master"
}

expect {
  timeout { puts "timeout prompt" ; exit 1 }
  "> "
}

""".replace("TELNET",telnet)
b="""
expect {
  timeout { puts "timeout prompt" ; exit 1 }
  "> "
}
"""

# shorthands so we can write e.g. "tvlcmd b 10 pause" rather than "tvlcmd boink 10 pause"
d = {
  ".": "localhost",
  "lh": "localhost",
  "b": "boink",
  "s": "snarf",
  "f2": "frodo2",
  "f3": "frodo3"
}

args = sys.argv[1:]
try:
  if len(args) == 1:
    host = os.getenv('vlc_host',os.getenv('host','localhost'))
    port = int(os.getenv('rp',os.getenv('port',0)))
  elif len(args) == 2:  
    host = os.getenv('vlc_host',os.getenv('host','localhost'))
    port, *args = args
  elif len(args) == 0:
    raise ValueError()
  else:
    host, port, *args = args
except ValueError:
  print(f"{sys.argv[0]} <host> <rport> [<cmds>...]")
  for k,v in d.items():
    print(f"  {k} => {v}")
  exit(1)

if host in d:
  host = d[host]

# shorthands so that we can type e.g. "tvlcmd . 10 p" instead of "tvlcmd localhost 10 play"
argm = """
p play
pa pause
pl play
pls playlist
s status
""".strip()
argd = { # shorthands
  k:v for k,v in [x.split(" ",1) for x in argm.splitlines()]
}
def sh(x):
  y=x
  if x[0] == 'v' and x[1:].isnumeric():
    x = 'volume '+x[1:]
  elif x[:3] == 'vol' and x[3:].isnumeric():
    x = 'volume '+x[3:]
  elif x[:2] == "vx" and x[2:].isnumeric():
    a = int(x[2:])*(2550/100.0)
    a = int(a)
    x = f'volume {a}'
  elif x[0] == 'g' and x[1:].strip().isnumeric():
    x = 'goto '+x[1:].strip()
  elif x in argd:
    x = argd[x]
  print(f"{y} => {x}")
  return x
args = [sh(arg.replace('"','\"')) for arg in args]
args = [(f"send \"{arg}\\r\"\n"+b if not arg.startswith("sleep ") else arg) for arg in args]
scr="\n".join([a]+args+['puts ""'])
with Popen(["expect","-f","-",host,str(port)],stdin=PIPE) as proc:
  proc.communicate(input=scr.encode())