Jump to content


Photo
- - - - -

Controlling Media Player Classic Remote with PHP


  • Please log in to reply
3 replies to this topic

#1 TerrorKalle

TerrorKalle

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 16 October 2006 - 03:58 AM

Controlling Media Player Classic Remote with PHP

This tutorial will guide you though how to control your Media Player remote with PHP, This tutorial requires you to have PHP & Media Player Classic installed, if you havnít got Media Player Classic then you can download it from here:



http://www.sourcefor...lease_id=403110


When you downloaded and installed Media Player Classic, press O or go to "View" > "Options" to open the option window. Select the menu "Web Interface" on the left.


Posted Image


Check the "Listen on port" checkbox, if you want to use another port than 13579 then enter it there. You can actually use this as webserver instead of Apache or Internet Information Services. But we don't want to here. If you want to install PHP then enter (if not already there) the index.php (make sure to remain index.html for Media Player Default pages) for php and this CGI handler:
.php=C:\Webserver\PHP\php-win.exe or .php=C:\Webserver\PHP\php-cgi.exe or where you have PHP Installed.

When you're done and click the Apply button, then Windows Firewall or whatever firewall program you have installed give you a notice and ask if Media Player Classic is allowed to port listen on TCP/UDP.


And now for the PHP! First let's make a simple function that checks if Media Player is running:


<?php
	// Try connect host
	$sock = @fsockopen('localhost', 13579, $errStr, $errNo, 10);

	if(!$sock)
	{
		die("Media Player is not running or web interface is turned off!");
	}
?>


Simply replace the 'localhost' and '13579' with your own values, these are just examples. The default file "command.html" sends a command that will be executed in Media Player. So we need to send some HTTP Post data to the script without loading our php page a couple of times, for that we need to connect via fsockopen and write some http code. So we can use the connect example above.

Letís code our function to execute a command:


<?php
	// Execute function
	function execute($host, $port, $cmd)
	{
		// Try connect host
		$sock = @fsockopen('localhost', 13579, $errStr, $errNo, 10);

		if(!$sock)
		{
			die("Unable to connect Remote server");
		}

		$data = "wm_command=" . urlencode($cmd);

		fwrite($sock, "POST /command.html HTTP/1.0\r\n");
		fwrite($sock, "HOST: localhost\r\n");
		fwrite($sock, "Content-Type: application/x-www-form-urlencoded\r\n");
		fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
		fwrite($sock, "\r\n");
		fwrite($sock, $data . "\r\n");
		fwrite($sock, "\r\n");

		// Close
		@fclose($sock);
	}
?>


The $cmd represents the command that can be executed, latest version of Media Player I'm running can handle these commands:


131 = Open File 
132 = Open DVD 
225 = Open Device 
313 = Save As 
32829 = Save Image 
32830 = Save Image (auto) 
134 = Load Subtitle 
239 = Save Subtitle 
32823 = Close 
283 = Properties 
135 = Exit 
157 = Play/Pause 
155 = Play 
156 = Pause 
158 = Stop 
32773 = Framestep 
32774 = Framestep back 
222 = Go To 
160 = Increase Rate 
159 = Decrease Rate 
32872 = Reset Rate 
32821 = Audio Delay +10ms 
32822 = Audio Delay -10ms 
32790 = Jump Forward (small) 
32789 = Jump Backward (small) 
32792 = Jump Forward (medium) 
32791 = Jump Backward (medium) 
32794 = Jump Forward (large) 
32793 = Jump Backward (large) 
32778 = Jump Forward (keyframe) 
32777 = Jump Backward (keyframe) 
165 = Next 
164 = Previous 
332 = Next Playlist Item 
331 = Previous Playlist Item 
267 = Toggle Caption&Menu 
136 = Toggle Seeker 
137 = Toggle Controls 
138 = Toggle Information 
139 = Toggle Statistics 
140 = Toggle Status 
236 = Toggle Subresync Bar 
242 = Toggle Playlist Bar 
244 = Toggle Capture Bar 
245 = Toggle Shader Editor Bar 
308 = View Minimal 
309 = View Compact 
310 = View Normal 
141 = Fullscreen 
32788 = Fullscreen (w/o res.change) 
142 = Zoom 50% 
143 = Zoom 100% 
144 = Zoom 200% 
271 = VidFrm Half 
272 = VidFrm Normal 
273 = VidFrm Double 
274 = VidFrm Stretch 
275 = VidFrm Inside 
276 = VidFrm Outside 
32827 = Always On Top 
152 = PnS Reset 
146 = PnS Inc Size 
148 = PnS Inc Width 
150 = PnS Inc Height 
147 = PnS Dec Size 
149 = PnS Dec Width 
151 = PnS Dec Height 
255 = PnS Center 
247 = PnS Left 
248 = PnS Right 
249 = PnS Up 
250 = PnS Down 
251 = PnS Up/Left 
252 = PnS Up/Right 
253 = PnS Down/Left 
254 = PnS Down/Right 
368 = PnS Rotate X+ 
369 = PnS Rotate X- 
370 = PnS Rotate Y+ 
371 = PnS Rotate Y- 
372 = PnS Rotate Z+ 
373 = PnS Rotate Z- 
161 = Volume Up 
162 = Volume Down 
163 = Volume Mute 
166 = DVD Title Menu 
167 = DVD Root Menu 
168 = DVD Subtitle Menu 
169 = DVD Audio Menu 
170 = DVD Angle Menu 
171 = DVD Chapter Menu 
32781 = DVD Menu Left 
32782 = DVD Menu Right 
32783 = DVD Menu Up 
32784 = DVD Menu Down 
32785 = DVD Menu Activate 
32786 = DVD Menu Back 
32787 = DVD Menu Leave 
32795 = Boss key 
32796 = Player Menu (short) 
32797 = Player Menu (long) 
32798 = Filters Menu 
154 = Options 
32799 = Next Audio 
32800 = Prev Audio 
32801 = Next Subtitle 
32802 = Prev Subtitle 
32817 = On/Off Subtitle 
33302 = Reload Subtitles 
32803 = Next Audio (OGM) 
32804 = Prev Audio (OGM) 
32805 = Next Subtitle (OGM) 
32806 = Prev Subtitle (OGM) 
32807 = Next Angle (DVD) 
32808 = Prev Angle (DVD) 
32809 = Next Audio (DVD) 
32810 = Prev Audio (DVD) 
32811 = Next Subtitle (DVD) 
32812 = Prev Subtitle (DVD) 
32818 = On/Off Subtitle (DVD)


So here a couple of examples for you to build or base your own scripts on :P


<?php
	// Execute function
	function execute($host, $port, $cmd)
	{
		// Try connect host
		$sock = @fsockopen($host, $port, $errStr, $errNo, 10);

		if(!$sock)
		{
			die("Unable to connect Remote server");
		}

		$data = "wm_command=" . urlencode($cmd);

		fwrite($sock, "POST /command.html HTTP/1.0\r\n");
		fwrite($sock, "HOST: localhost\r\n");
		fwrite($sock, "Content-Type: application/x-www-form-urlencoded\r\n");
		fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
		fwrite($sock, "\r\n");
		fwrite($sock, $data . "\r\n");
		fwrite($sock, "\r\n");

		// Close
		@fclose($sock);
	}

	$host = 'localhost';
	$port = 13579;

	// We know that thereís a movie on pause at the server, so lets start it and stop
	// after 10 seconds.

	// Play movie
	execute($host, $port, 155);

	// Wait...
	sleep(10);

	// Stop movie
	execute($host, $port, 156);
?>


Close Running Media Player:


<?php
	// Execute function
	function execute($host, $port, $cmd)
	{
		// Try connect host
		$sock = @fsockopen($host, $port, $errStr, $errNo, 10);

		if(!$sock)
		{
			die("Unable to connect Remote server");
		}

		$data = "wm_command=" . urlencode($cmd);

		fwrite($sock, "POST /command.html HTTP/1.0\r\n");
		fwrite($sock, "HOST: localhost\r\n");
		fwrite($sock, "Content-Type: application/x-www-form-urlencoded\r\n");
		fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
		fwrite($sock, "\r\n");
		fwrite($sock, $data . "\r\n");
		fwrite($sock, "\r\n");

		// Close
		@fclose($sock);
	}

	$host = 'localhost';
	$port = 13579;

	// Close Media Player
	execute($host, $port, 135);
?>


Getting status codes:


<?php
	function getStatus($host, $port)
	{
		// Try connect host
		$sock = @fsockopen($host, $port, $errStr, $errNo, 10);

		if(!$sock)
		{
			die("Unable to connect Remote server");
		}

		// Close
		@fclose($sock);

		function onStatus($winTitle, $status, $currLen, $currTime, $totalLen, $totalTime, $muted, $volume)
		{
			$muted = (($muted) ? true : false);

			if($volume > 100)
			{
				$volume = 100;
			}
			elseif($volume < 0)
			{
				$volume = 0;
			}

			$title = str_replace(' - Media Player Classic', '', $winTitle);

			return(Array(
					'windowtitle'		=> $winTitle, 
					'title'			=> $title, 
					'status'		=> $status, 
					'currentLength'		=> $currLen, 
					'currentTime'		=> $currTime, 
					'TotalLength'		=> $totalLen, 
					'TotalTime'		=> $totalTime, 
					'muted'			=> $muted, 
					'volume'		=> $volume
					));
			}

			$statusdata = @file_get_contents('http://' . $host . ':' . $port . '/status.html') or die('Unable to open status file!');
			eval("\$status = " . $statusdata . "; ");

		return($status);
	}

	$host = 'localhost';
	$port = 13579;

	// Var_dump() it :P
	echo("<pre>");
	var_dump(getStatus($host, $port));
	echo("</pre>");
?>


Exampe output of above:


array(9) {
  ["windowtitle"]=>
  string(37) "Tusind ben.mp3 - Media Player Classic"
  ["title"]=>
  string(14) "Tusind ben.mp3"
  ["status"]=>
  string(7) "Playing"
  ["currentLength"]=>
  int(1126)
  ["currentTime"]=>
  string(8) "00:00:01"
  ["TotalLength"]=>
  int(179737)
  ["TotalTime"]=>
  string(8) "00:02:59"
  ["muted"]=>
  bool(false)
  ["volume"]=>
  int(54)
}


Putting player status on a image using GD:


<?php
	function getStatus($host, $port)
	{
		// Try connect host
		$sock = @fsockopen($host, $port, $errStr, $errNo, 10);

		if(!$sock)
		{
			die("Unable to connect Remote server");
		}

		// Close
		@fclose($sock);

		function onStatus($winTitle, $status, $currLen, $currTime, $totalLen, $totalTime, $muted, $volume)
		{
			$muted = (($muted) ? true : false);

			if($volume > 100)
			{
				$volume = 100;
			}
			elseif($volume < 0)
			{
				$volume = 0;
			}

			$title = str_replace(' - Media Player Classic', '', $winTitle);

			// Note the empty key and value here
			return(Array(
					'windowtitle'		=> $winTitle, 
					''			=> '', 
					'title'			=> $title, 
					'status'		=> $status, 
					'currentLength'		=> $currLen, 
					'currentTime'		=> $currTime, 
					'TotalLength'		=> $totalLen, 
					'TotalTime'		=> $totalTime, 
					'muted'			=> $muted, 
					'volume'		=> $volume
					));
			}

			$statusdata = @file_get_contents('http://' . $host . ':' . $port . '/status.html') or die('Unable to open status file!');
			eval("\$status = " . $statusdata . "; ");

		return($status);
	}

	$host = 'localhost';
	$port = 13579;

	// Get Status codes
	$status = getStatus($host, $port);

	// Create image resource, change the functions to 
	// whatever format you wish to use.
	//
	// Also note that this requires GD Library to work!
	$im = ImageCreateTrueColor(300, 100);

	$black = ImageColorAllocate($im, 255, 255, 255);

	$start_x = 5;
	$start_y = 5;

	$x_increment = 0;
	$y_increment = 11;

	$not_increment = false;

	foreach($status as $key => $val)
	{
		switch($key)
		{
			case('windowtitle'):
				$response = 'Media Player Classic Status';
			break;
			case('title'):
				$response = "File: " . $val;
			break;
			case('status'):
				if(empty($val))
				{
					$val = 'Not playing';
				}

				$response = "Status: " . $val;
			break;
			case('currentLength'):
			case('currentTime'):
				$currentTime = $val;
			case('TotalLength'):
				$not_increment = true;
			break;
			case('TotalTime'):
				if($currentTime != '00:00:00' && $val != '00:00:00')
				{
					$response = "Time: " . $currentTime . " / " . $val;
					$not_increment = false;
				}
			break;
			case('muted'):
				if($not_increment != false)
				{
					$not_increment = false;
				}

				$response = "Muted: " . (($val) ? 'Yes' : 'No');
			break;
			case('volume'):
				$response = "Volume: " . $val . "%";
			break;
		}

		if(!empty($response))
		{
			ImageString($im, 2, $start_x, $start_y, $response, $black);
			$response = '';
		}

		if(!$not_increment)
		{
			($start_x += $x_increment);
			($start_y += $y_increment);
		}
	}

	@Header('Content-Type: Image/JPEG');

	ImageJpeg($im);
	ImageDestroy($im);
?>


The above example is using GD Library's JPEG support, read the inline comments for more information.

Example signature:
Posted Image

Generating XML Output:


<?php
	function getStatus($host, $port)
	{
		// Try connect host
		$sock = @fsockopen($host, $port, $errStr, $errNo, 10);

		if(!$sock)
		{
			die("Unable to connect Remote server");
		}

		// Close
		@fclose($sock);

		function onStatus($winTitle, $status, $currLen, $currTime, $totalLen, $totalTime, $muted, $volume)
		{
			$muted = (($muted) ? true : false);

			if($volume > 100)
			{
				$volume = 100;
			}
			elseif($volume < 0)
			{
				$volume = 0;
			}

			$title = str_replace(' - Media Player Classic', '', $winTitle);

			// Tiny change here, changed key values to bigger letters 
			// so the XML dump will be abit more human read-able
			return(Array(
					'WindowTitle'		=> $winTitle, 
					'Title'			=> $title, 
					'Status'		=> $status, 
					'CurrentLength'		=> $currLen, 
					'CurrentTime'		=> $currTime, 
					'TotalLength'		=> $totalLen, 
					'TotalTime'		=> $totalTime, 
					'Muted'			=> $muted, 
					'volume'		=> $volume
					));
			}

			$statusdata = @file_get_contents('http://' . $host . ':' . $port . '/status.html') or die('Unable to open status file!');
			eval("\$status = " . $statusdata . "; ");

		return($status);
	}

	$host = 'localhost';
	$port = 13579;

	// Get Status codes
	$status = getStatus($host, $port);

	// Format into our own little XML format, so we
	// later can load it into PHP via simpleXML

	@Header('Content-Type: text/xml');

	echo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
	echo("<MediaPlayerDump>\n");

	foreach($status as $key => $val)
	{
		if($key == 'muted')
		{
			$val = ($val) ? 1 : 0;
		}

		echo("\t<" . $key . ">" . $val . "</" . $key . ">\n");
	}

	echo("</MediaPlayerDump>");
?>


Loading it into PHP via SimpleXML:


<?php
	$xmldump = simplexml_load_file('http://localhost/wmpl.php') or die('Unable to Load XML dump!');

	$array = Array();
	$array['windowtitle'] = $xmldump->WindowTitle;
	$array['Title'] = $xmldump->Title;
	$array['Status'] = $xmldump->Status;
	$array['CurrentLength'] = $xmldump->CurrentLength;
	$array['CurrentTime'] = $xmldump->CurrentTime;
	$array['TotalLength'] = $xmldump->TotalLength;
	$array['TotalTime'] = $xmldump->TotalTime;
	$array['Muted'] = $xmldump->Muted;
	$array['Volume'] = $xmldump->volume;

	foreach($array as $key => $val)
	{
		// UGLY Hack to force objects into strings =/
		ob_start();
		echo($val);
		$val = ob_get_clean();
		ob_end_clean();
		ob_end_flush();

		$array[$key] = $val;
	}

	// Print_r() it =o
	echo("<pre>");
	print_r($array);
	echo("</pre>");
?>


Example output of above example:


Array
(
	[windowtitle] => Safri duo vs Prima Donna and Linda Scott - Why havnt i told Baya(1).mp3 - Media Player Classic
	[Title] => Safri duo vs Prima Donna and Linda Scott - Why havnt i told Baya(1).mp3
	[Status] => Paused
	[CurrentLength] => 0
	[CurrentTime] => 00:00:00
	[TotalLength] => 302654
	[TotalTime] => 00:05:02
	[Muted] => 0
	[Volume] => 64
)


Bonus Stuff
Another thing that I havnít yet got time to but I properly will be working on later is a get method so you can make a function to play a movie, yet what I've found out is if you send a valid local path to browser.html (eg. browser.html?path=C:\Local\Path\To\Movie.divx) then Media Player will play that movie, so its really just a matter of sending the right HTTP Code to the server.

Changing volume, send the post variable 'volume' with a value of 1-100 and the command code -2 to change the volume.

To seek, then send the post variable 'percent' with a value of how many % of the movie that you wanna seek into, use the command code -1

Commands can also be sent via GET format instead of POST as the examples :)

Not only movies are supported via this, but also music.

Command line execution:
You can use Media Player from command line too, for a overview of commands and their parameters, then go to "Help" > "Command Line Switches".

Use one of the PHP Program Execution functions that fits your needs at:
http://www.php.net/ref.exec


A little minus:
A personal little minus for me was that the snapshot image isnít working well. Since I was excited to test that out with some PHP / Javascript integration. I can always hope that it will get fixed in one of the upcoming versions.

This file in a Word Document:
Attached File  Controlling_Media_Player_Classic_Remote_with_PHP.doc   116K   1506 downloads


First post and first tutorial on Pixel2Life enjoy :P

Kalle Sommer Nielsen
Zesix Interactive / TK Web Technologies



#2 Adαm

Adαm

    Young Padawan

  • Members
  • Pip
  • 189 posts
  • Gender:Male
  • Location:United Kingdom

Posted 17 October 2006 - 03:03 PM

Awesome!

I only had a quick scim read but it looks really good, nice one :love:

#3 TerrorKalle

TerrorKalle

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 27 October 2006 - 09:26 PM

ey, Thanks alot =)

#4 Patrick

Patrick

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 27 October 2006 - 09:49 PM

wow this looks really good, and it really explains what each part does, and even shows some examples and possibilities.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users