Archive for the ‘PHP’ Category

Chromecast: Playing Local Media

February 6th, 2014

The Chromecast SDK just became publicly available yesterday and I wasted no time figuring out how to cast my local media (finally).

This is a hack at the moment while I work on writing a Chrome extension to act as the server (or someone else finishes it first). It works well enough for me for the time being. If you’re going to set this up, you’ll need a web server with PHP running on the computer that holds your media. Again, this is just a hack.

After cursing JavaScript again since my last adventure with Google Maps, I finally got the most basic code I could come up with to cast video. The HTML sucks, the JavaScript sucks, and the error handling is non-existent. But it works (barely). There is no seeking. The only thing it can do is play and pause. With a Chrome extension, you can write a very basic server in JavaScript to serve the movie file which removes the needs for a web server on your host.

Until then, I set up Apache with mod_php, set my DocumentRoot to my movie directory, stuck jquery.js in there and created a PHP script that does the HTML/Chromecast stuff.

First, stick jquery in your movie directory:

curl > ~/Movies/jquery.js

And then create index.php with this code:

<script type="text/javascript"
<script type="text/javascript"

<script type="text/javascript">

session = null;
currentMedia = null;
if (!chrome.cast || !chrome.cast.isAvailable) {
  setTimeout(initializeCastApi, 1000);

function initializeCastApi() {
var applicationID =;
var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
chrome.cast.initialize(apiConfig, onInitSuccess, onError);
chrome.cast.requestSession(onRequestSessionSuccess, onLaunchError);

function onMediaDiscovered(how, media) {
       currentMedia = media;

function receiverListener(e) {
if( e === 'available' ) {

function onInitSuccess() {
      console.log("init success");

function onError() {

function onRequestSessionSuccess(e) {
    console.log('got it');
          session = e;

function sessionListener(e) {
      session = e;

function onMediaError(e) {
    console.log('media error ' + e.code);

function onLaunchError() {
    console.log('launch error');


function setmedia(e) {
var mediaInfo = new
  '' + e, 'video/mp4');
var request = new;
  request.autoplay = true;
       onMediaDiscovered.bind(this, 'loadMedia'),

function onMediaDiscovered(how, media) {
       window.currentMedia = media;
       console.log('got currentmedia');
function mediaCommandSuccessCallback(e) {
    console.log('command success');
function onError() {

function play() {,
function pause() {
<font face=verdana>
echo "<a href=\"#\" onclick=\"javascript:play()\">Play</a>&nbsp;&nbsp;|";
echo "&nbsp;&nbsp;<a href=\"#\" onclick=\"javascript:pause()\">Pause</a><br><hr>\n";
foreach (scandir('.') as $f) {
    if ($f[0] == '.' || $f == '..') {
    if (! preg_match('/(.*)\.(mp4|m4v|avi)$/', $f, $regs)) {
    $name = $regs[1];
    echo "<a href=\"#\" onclick=\"javascript:window.setmedia('$f');\">$name</a><br>\n";


Chromecast, PHP, Video

Patch for the VastHTML WordPress Forum Server

March 3rd, 2010

So, I’ve made a number of fixes to the VastHTML WordPress forum server plugin. It has some pretty big bugs, and I don’t know if the project is being maintained anymore or not. At any rate, the fixes I’ve made should have been considered critical and should have been fixed long ago by whoever is maintaining it, but I digress…

I’m not going to support people trying to apply this patch. If you don’t know what a diff is and you don’t know what the patch command does, you’re probably out of luck. If you want me to fix all of the problems in this code and release it, pay me a bunch of money…

Also, the security problems in their code makes babies cry… but that’s for another day.

Lastly, to make the search actually work, you need to connect to your wordpress mysql database and issue this SQL statement:

alter table wp_forum_posts add fulltext key `text` (`text`);

Here's the patch: vasthtml-forum-server.diff

Here's what it fixes (in no particular order):

  • RSS feeds now contain the username of the poster instead of ""
  • All &amp; characters in the links have been properly changed to & as they should be
  • Page 2+ of your forums will work
  • Page 2+ of posts will work
  • The number of replies shown in the topic list is properly set to number of posts - 1
  • The title delimeter is changed from » to "|" (don't remember why i did this, but there ya go)
  • The search form/box uses HTTP GET instead of POST so your back button works without complaining about having to resubmit your request
  • You can press enter in the search box to submit
  • A $ followed by a number doesn't get filtered out
  • Apostrophes in posts/titles get their slashes properly stripped

I may have fixed other things in this patch and forgot about it. This works for me... your mileage may vary.

General, PHP , ,

Got a PS3? Want Hulu Back? Easy enough…

June 29th, 2009

Update Jan 6, 2010: Some users are reporting that they’re getting an error telling them they need to update Flash. I imagine we’re going to have to wait for Sony to update the PS3 firmware with a new Flash version.

Over the weekend Hulu stopped working for PS3 users. How did they block the PS3 users? With the dumbest method they could find. They test the User-Agent string in the HTTP request. Well, luckily, we can use a proxy server and just rewrite it. Windows users may want to look here

1. Install squid (“sudo apt-get install squid” for ubuntu users…. for Windows users, google for: squid windows)

2. Edit squid’s default config (/etc/squid/squid.conf on ubuntu) with these changes (or download my configuration: squid-hulu.conf):

Search for “acl localnet src” and set it to your internal network. You can remove the other localnet definitions if you’re not using them as shown here:

#acl localnet src
#acl localnet src
acl localnet src

Note: For 99.9999% of you, the correct line will be “acl localnet src″

After the “acl localnet src” line, add this:

acl hulu url_regex -i ^http://.**

Search for “http_access allow localhost” and add “http_access allow localnet” as such:

http_access allow localnet
http_access allow localhost

Add the following two lines pretty much anywhere in the file (the end of the file works just fine):

header_access User-Agent deny hulu
header_replace User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)

3. Start squid (sudo /etc/init.d/squid start)

4. Go to PS3 Internet settings. Do manual configure. Go through the settings as normal (the defaults should be fine) until you get to proxy server. Set it to be your PC’s IP address port 3128.

Your PS3 is now a Windows machine running Firefox (as far as Hulu is concerned) and you can use Hulu again. What a dumb method of restricting access.

UPDATE – Jul 30 2009: To all of you guys out there saying “Why don’t you just use PlayOn or TVersity?” My response is:

from eric to MediaMall Support
date Mon, Jun 29, 2009 at 10:00 AM

I assume you're saying that there's no plan for a Linux version then?

from MediaMall Support to eric
date Mon, Jun 29, 2009 at 10:03 AM


That was the response I received. TVersity is quite obviously intertwined with Windows and there is no hope there either. The users that support the invasive DRM (by using it) are responsible for open protocols not being available. If you want to know why DRM is successful, you need only look in the mirror.

UPDATED – Aug 10 2009: As requested in the comments, the instructions now make it so that only hulu will get the header replaced. Also, the comments section has tips and hints for Windows users and squid version 3.0+. See:

Some more Windows Info

Squid 3.0+ Info

General, GTFS, PHP, PlayStation3 , , ,

Categorizing Your Movie Collection with IMDB

June 17th, 2009

If you have a ton of movies like I do, scrolling through the full list on the PS3 is painful. After about 100 movies or so, you realize that you need a better method of doing it.

Well if you’re on Linux, you are in luck! As I started categorizing stuff by hand, I ran into another unrelated problem. While I was searching for the solution, I came across IMDB-to-MPEG. I had to hack around a bit in the PHP code to get it to work the way I wanted, but man did it save a lot of time. I fixed a number of things and submitted it back to the author, so hopefully we’ll see a new version soon.

Basically, you give it a movie name and it queries IMDB for the movie. Based on the IMDB Genres and Ratings, it creates your symlink tree for you. So in the PS3 XMB, when you go t your movie server you have all of your movies categorized by Genre so you can get a list of all Action movies, for example. Of course, since it’s all just symlinks, you can have the same movie covering multiple genres. So for a military crime drama — such as A Few Good Men — it would be listed under all 3 genres of Military, Crime, and Drama.

But it also had another interesting feature. It creates a MPEG video file that you can play from your uPnP client that gives you IMDB info, like the plot, the year, the ratings, etc.

This is what the original looked like: About Army of Darkness.m4v

This is what my latest version looks like: About Army of Darkness.m4v

And if you have an HTML 5 browser, here it is with the video tag (in Ogg Video format):

I wish I knew a good algorithm for making motion more fluid. The animation frames look jerky. I’m sure I need to blur or leave trails or something, but my attempts have all been failures.

General, PHP , ,