Sign up here and you can log into the forum!

UMSP Plugin: TroNik Player

UMSP Plugin Depot, the place for sharing plugins & info about them. No requests, just plugins/code.

Re: UMSP Plugin: TroNik Player   

Postby recliq » Tue Jan 24, 2012 10:08 am

Note: Your problem probably was that you won't notice a missing folder.jpg in OSD as long as you have a valid file thumb, since dmaosd will search for folder.jpg and some other filenames as a folder thumb (See WDTV User Manual for an explanation of the search algorithm).
­WDLXTV Project Maintainer
-:] If you like my contributions feel free to donate for a beer or a new flash drive. ...and always remember: RTFM! (README, FAQ, WIKI) [:-
User avatar
recliq
WDLXTV Team
 
Posts: 5024
Joined: Thu Apr 15, 2010 8:09 am
Location: Kiel, Germany

Re: UMSP Plugin: TroNik Player   

Postby hobbitjack » Wed Jan 25, 2012 5:44 am

Only issue i currently get now, is when browsing my movies, via moviesheets, it displays the first couple, then skips to the 5th or 6th moviesheet and then goes back to the ones it misses.

Im guessing its to do with the way it sorts the movie/moviesheet files?
hobbitjack
Donor
 
Posts: 54
Joined: Thu Apr 21, 2011 9:35 am

Re: UMSP Plugin: TroNik Player   

Postby jmaraujo » Mon Feb 20, 2012 12:54 am

First of all, thank you for the plugin! ;)

I'm having trouble downloading subtitles. Every time I try it says "Failed to save {subtitle-filename}.srt".

Probably a problem with permissions but I couldn't fix it myself. :oops: Can you please guide me here?

Thank you.
User avatar
jmaraujo
n00b
 
Posts: 17
Joined: Sun Feb 19, 2012 3:01 pm
Location: Rivera, Uruguay

Re: UMSP Plugin: TroNik Player   

Postby mad_ady » Mon Feb 20, 2012 5:15 am

If you are using xmounts or network shares, you will need to set them to be mounted read-write before you can download subtitles (see the xmount or cifs-interceptor wiki pages for details). If you are browsing data from an attached USB drive, then the plugin will work automagically.
User avatar
mad_ady
Developer
 
Posts: 3002
Joined: Fri Nov 05, 2010 9:08 am
Location: Bucharest, Romania

Re: UMSP Plugin: TroNik Player   

Postby jmaraujo » Tue Feb 21, 2012 11:53 pm

Thank you very much, mad_ady. 8-) I'm using xmount and I set them as read-write and now problem solved! :D
User avatar
jmaraujo
n00b
 
Posts: 17
Joined: Sun Feb 19, 2012 3:01 pm
Location: Rivera, Uruguay

Re: UMSP Plugin: TroNik Player   

Postby sonofdibnah » Mon Mar 05, 2012 9:13 am

After updating to 0.1.5, I no longer see moviesheets which are working if I go through Video>Local Drives. I'm using $M/$F/$N$E_sheet.std.jpg which worked in your earlier version. Have I missed something or is there a fault in the latest version? Thanks.
sonofdibnah
Donor
 
Posts: 17
Joined: Mon Jun 13, 2011 7:40 am

Re: UMSP Plugin: TroNik Player   

Postby TroNik » Tue Mar 06, 2012 1:43 am

Changelog:
- Convert MP4 videos with AAC audio to MKV with AC3 at 448k
- Higher quality check mark for watched videos
- Create directory if it doesn't exist when generating movie sheets
- Fix listing videos in directories containing square bracket [
- Sort videos by name
- Support folder.png in addition to folder.jpg
- If a directory has only one directory or video auto select it and advance to its menu
- Show download all subtitles option only if there are at least 2 subtitles available
- Download a smaller size of poster from IMDb
TroNik
Developer
 
Posts: 46
Joined: Wed Dec 21, 2011 2:56 am

Re: UMSP Plugin: TroNik Player   

Postby TroNik » Tue Mar 06, 2012 1:49 am

hobbitjack wrote:Only issue i currently get now, is when browsing my movies, via moviesheets, it displays the first couple, then skips to the 5th or 6th moviesheet and then goes back to the ones it misses.
Im guessing its to do with the way it sorts the movie/moviesheet files?

Before the videos were sorted by extension but I changed the code to sort them by filename.

sonofdibnah wrote:After updating to 0.1.5, I no longer see moviesheets which are working if I go through Video>Local Drives. I'm using $M/$F/$N$E_sheet.std.jpg which worked in your earlier version. Have I missed something or is there a fault in the latest version? Thanks.

I hope not :) I just tried under UMSPX and the moviesheets are displayed. I'll try on my WDTV over the weekend. Edit: Tried it and I didn't have any problems.
Last edited by TroNik on Wed Mar 14, 2012 1:50 am, edited 1 time in total.
TroNik
Developer
 
Posts: 46
Joined: Wed Dec 21, 2011 2:56 am

Re: UMSP Plugin: TroNik Player   

Postby rokr » Sat Mar 10, 2012 9:28 am

Is it possible to add another subtitle site to this plugin?
http://www.subs4free.com/ (it redirects to http://www.freeprojectx.com when you search subtitles)
this has english and greek lanquage subtitles only, but it the most used subtitle site here in Greece.
I don't think there's an api for it though.
Thanks
rokr
Beta Tester
 
Posts: 121
Joined: Wed May 12, 2010 11:10 pm

Re: UMSP Plugin: TroNik Player   

Postby TroNik » Wed Mar 14, 2012 2:04 am

rokr,
The SVN is down right now but I'll upload it as soon as it's up. In the meantime you could add/change the following files:

subs4free.php
Code: Select all
<?php

class subs4free {
  public static function search($query) {
    $results = array();
    $url = 'http://www.freeprojectx.com/search_report.php?search=' . urlencode($query);
    $data = file_get_contents($url);
    if (preg_match_all('@href="(?P<href>link.php\?p=download-(?P<id>[A-Za-z0-9]*).html)" ' .
        'title="(?P<title>[^"]*)">@', $data, $matches, PREG_SET_ORDER)) {
      foreach ($matches as $match) {
        $results[] = array(
          'id' => $match['id'],
          'href' => $match['href'],
          'title' => $match['title'],
        );
      }
    }
    return $results;
  }

  public static function download($href, $saveToFile) {
    $cookie = 'PHPSESSID=' . rand();
    $data = subs4free::fetch($href, '', $cookie);
    if (preg_match('@<link href="./(?P<css>styleSheets/css/[A-Za-z0-9]*\.css)"@', $data, $match)) {
      $styles = subs4free::fetch($match['css'], $href, $cookie);
      if (preg_match_all('@<tr class="stylepps (?P<style>style_[A-Za-z0-9]*)">\s*' .
          '<td><div align="left"><a rel="nofollow" ' .
          'href="(?P<href>getSub-[A-Za-z0-9=]*.html)"@s', $data, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $match) {
          $style = $match['style'];
          if (preg_match('@.' . $style . ' {display:block}@', $styles)) {
            $rar_contents = subs4free::fetch($match['href'], $href, $cookie);
            $tmp_dir = function_exists('_getUMSPTmpPath') ? _getUMSPTmpPath() : '/tmp';
            $rar_file = $tmp_dir . '/' . 'subs4free_' . rand() . '.rar';
            $rar_fp = fopen($rar_file, 'w');
            if ($rar_fp) {
              fwrite($rar_fp, $rar_contents);
              fclose($rar_fp);
              $subtitle_contents = shell_exec('unrar p -inul -y "' . $rar_file . '" *.srt');
              unlink($rar_file);
              if ($subtitle_contents != '') {
                $fp = fopen($saveToFile, 'w');
                if ($fp) {
                  fwrite($fp, $subtitle_contents);
                  fclose($fp);
                  return true;
                }
              }
            }
          }
        }
      }
    }
    return false;
  }

  private static function fetch($href, $referer = '', $cookie = '') {
    $url = "http://www.freeprojectx.com/$href";
    $opts = array(
      'http' => array(
        'method' => 'GET',
        'header' => "Referer: http://www.freeprojectx.com/$referer\r\nCookie: $cookie\r\n",
      )
    );
    $context = stream_context_create($opts);
    return file_get_contents($url, false, $context);
  }
}

?>


tronik-player.php
Code: Select all
<?php

include_once('imdb.php');
include_once('tmdb.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/umsp/funcs-config.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/umsp/funcs-misc.php');

function _pluginMain($prmQuery) {
  if (strpos($prmQuery, '&amp;') !== false) $prmQuery = str_replace('&amp;', '&', $prmQuery);
  parse_str($prmQuery, $params);
  if (isset($params['f']) && $params['f'] == 'search_button') {
    exec("sudo chmod 666 /tmp/ir_injection && sudo echo E > /tmp/ir_injection &");
    return call_user_func_array(_readTmpVar('lastFunction'), _readTmpVar('lastArgs'));
  }
  if (isset($params['f']) && function_exists($params['f'])) {
    _writeTmpVar('lastFunction', $params['f']);
    _writeTmpVar('lastArgs', $params['args']);
    if (isset($params['args'])) {
      return call_user_func_array($params['f'], $params['args']);
    } else {
      return call_user_func($params['f']);
    }
  }
  $dir = getConfigValueFiles('INITIAL_DIR', array('/tmp/media/usb'));
  if (!file_exists($dir)) {
    $dir = '/';
  }
  return dir_menu($dir);
}

function _pluginSearch($prmQuery) {
  if (preg_match('/and dc:(?:title|album|genre) contains "(.*?)"/', $prmQuery, $query)) {
    $query = $query[1];
    $lastFunction = _readTmpVar('lastFunction');
    $lastArgs = _readTmpVar('lastArgs');
    if ($lastFunction == 'associate') {
      return associate_imdb($lastArgs[0], $query, $lastArgs[1], $lastArgs[2]);
    } else if ($lastFunction == 'subtitle_menu') {
      return subtitle_search_by_text($lastArgs[0], $query);
    }
    return array(
      array(
        'id' => build_umsp_url($lastFunction, $lastArgs),
        'dc:title' => 'Search is supported only in IMDb association and subtitle menu',
        'upnp:class' => 'object.container',
      ),
    );
  }
}

function build_query($f, $args = array()) {
  return http_build_query(array('f' => $f, 'args' => $args), '', '&amp;');
}

function build_umsp_url($f, $args = array()) {
  return 'umsp://plugins/tronik-player/tronik-player?' . build_query($f, $args);
}

////////////////////////////////////////////////////////////////////////////////

function dir_menu($dir) {
  $Items = array();
  $dir_count = 0;
  $file_count = 0;
  $supported_files = get_supported_files();
  foreach (dir_glob($dir, '*', GLOB_ONLYDIR) as $subdir) {
    if (is_video_dir($subdir, $supported_files)) {
      $videosubdir = $subdir;
      $dir_thumb = $subdir . '/folder.jpg';
      if (!file_exists($dir_thumb)) $dir_thumb = $subdir . '/folder.png';
      $Items[] = array(
        'id' => build_umsp_url('dir_menu', array($subdir)),
        'dc:title' => '[' . basename($subdir) . ']',
        'upnp:album_art' => proxy_local_image($dir_thumb),
        'upnp:class' => 'object.container',
      );
      $dir_count++;
    }
  }
  foreach (list_videos($dir, $supported_files) as $file) {
    $Items[] = array(
      'id' => build_umsp_url('video_menu', array($file)),
      'dc:title' => basename($file),
      'upnp:album_art' => get_thumbnail($file),
      'upnp:class' => 'object.container',
    );
    $file_count++;
  }
  if ($dir_count == 1 && $file_count == 0) {
    return dir_menu($videosubdir);
  }
  if ($dir_count == 0 && $file_count == 1) {
    return video_menu($file);
  }
  if (empty($Items)) {
    $Items[] = array(
      'id' => build_umsp_url('dir_menu', array(dirname($dir))),
      'dc:title' => 'No video files in: "' . $dir . '"',
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

////////////////////////////////////////////////////////////////////////////////

function video_menu($file) {
  // TODO: automatically mark as watched
  $Items[] = array(
    'id' => build_umsp_url('play', array($file)),
    'res' => 'file://' . $file,
    'dc:title' => 'Play "' . basename($file) . '"',
    'upnp:album_art' => get_thumbnail($file),
    'upnp:class' => 'object.item.videoItem',
    'protocolInfo' => 'http-get:*:*:*',
  );
  $Items[] = array(
    'id' => build_umsp_url('trailer_menu', array($file)),
    'dc:title' => 'Trailers',
    'upnp:album_art' => 'http://lh6.googleusercontent.com/-lLFiRG9jHT4/TxPfINh-tlI/AAAAAAAAAa8/Ej87r-G0BBI/s220/video_icon.png',
    'upnp:class' => 'object.container',
  );
  $Items[] = array(
    'id' => build_umsp_url('subtitle_menu', array($file)),
    'dc:title' => 'Download subtitles',
    'upnp:album_art' => 'http://lh3.googleusercontent.com/-xe_EaL64jU8/Twl6Vy5WxUI/AAAAAAAAAaM/lK4oVVStEmg/s128/subtitles.png',
    'upnp:class' => 'object.container',
  );
  $Items[] = array(
    'id' => build_umsp_url('poster_menu', array($file)),
    'dc:title' => 'Download poster',
    'upnp:album_art' => 'http://lh4.googleusercontent.com/-xaW2z6YpyE8/TxPiPMqsX9I/AAAAAAAAAbU/EaVf6muwiR4/s220/poster.png',
    'upnp:class' => 'object.container',
  );
  $Items[] = array(
    'id' => build_umsp_url('moviesheet_menu', array($file)),
    'dc:title' => 'View/Generate moviesheet',
    'upnp:album_art' => 'http://lh3.googleusercontent.com/-qkxtGSjGVnU/TxPhWcxOoNI/AAAAAAAAAbM/JjtswgFSAGA/s220/thumbgen.jpg',
    'upnp:class' => 'object.container',
  );
  $Items[] = array(
    'id' => build_umsp_url('advanced_menu', array($file)),
    'dc:title' => 'Advanced menu',
    'upnp:album_art' => 'http://lh5.googleusercontent.com/-xsH3IJAYXd0/TvwfRdc7DMI/AAAAAAAAAFk/NmvkjuqP_eo/s220/Settings.png',
    'upnp:class' => 'object.container',
  );
  return $Items;
}

////////////////////////////////////////////////////////////////////////////////

function trailer_menu($file) {
  $imdbid = retrieve_imdbid($file);
  if ($imdbid) {
    $movieInfo = Imdb::getInfo($imdbid);
    $title = $movieInfo['title'];
    $title_and_year = Imdb::formatTitle($movieInfo);
    foreach ($movieInfo['trailers'] as $trailer) {
      $thumb = Imdb::formatThumbUrl($trailer['thumb']);
      $Items[] = array(
        'id' => build_umsp_url('imdb_trailer', array($file)),
        'res' => $trailer['url'],
        'dc:title' => $title_and_year . ' - IMDb trailer ' . $trailer['format'],
        'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/tronik-player/img-proxy.php?url=' . urlencode($thumb),
        'upnp:class' => 'object.item.videoitem',
        'protocolInfo' => 'http-get:*:video/*:*',
      );
    }
    if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/umsp/plugins/youtube-subscriptions/youtube-subscriptions-proxy.php')) {
      $tmdbMovieInfo = Tmdb::imdbLookup($imdbid);
      $youtube_trailer = $tmdbMovieInfo['trailer'];
      if ($youtube_trailer) {
        parse_str(parse_url($youtube_trailer, PHP_URL_QUERY), $params);
        $video_id = $params['v'];
        if ($video_id) {
          $Items[] = array(
            'id' => build_umsp_url('youtube_trailer', array($file)),
            'res' => 'http://127.0.0.1/umsp/plugins/youtube-subscriptions/youtube-subscriptions-proxy.php?video_id=' . $video_id,
            'dc:title' => $title_and_year . ' - YouTube trailer',
            'upnp:album_art' => 'http://i.ytimg.com/vi/' . $video_id . '/default.jpg',
            'upnp:class' => 'object.item.videoitem',
            'protocolInfo' => 'http-get:*:video/*:*',
          );
        }
      }
    } else {
      $Items[] = array(
        'id' => build_umsp_url('video_menu', array($file)),
        'dc:title' => 'Requires youtube-subscriptions plugin',
        'upnp:class' => 'object.container',
      );
    }
    if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/umsp/plugins/traileraddict/traileraddict.php')) {
      $Items[] = array(
        'id' => 'umsp://plugins/traileraddict/traileraddict?' . build_query('imdb_lookup', array($imdbid, $title)),
        'dc:title' => 'Search TrailerAddict',
        'upnp:album_art' => 'http://lh3.googleusercontent.com/_xJcSFBlLg_Y/TXENZwVCxJI/AAAAAAAAAcs/jPVtwm60uR4/s220/ta2.png',
        'upnp:class' => 'object.container',
      );
    }
    if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/umsp/plugins/hd-trailers/hd-trailers.php')) {
      $Items[] = array(
        'id' => 'umsp://plugins/hd-trailers/hd-trailers?' . build_query('search', array($title)),
        'dc:title' => 'Search HD-Trailers',
        'upnp:album_art' => 'http://lh6.googleusercontent.com/-pVguvOG7-AQ/Tvf6DDCGJHI/AAAAAAAAAE8/YxtuKqmt3do/s220/hd-trailers.png',
        'upnp:class' => 'object.container',
      );
    }
    if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/umsp/plugins/fandango/fandango.php')) {
      $Items[] = array(
        'id' => 'umsp://plugins/fandango/fandango?' . build_query('search', array($title)),
        'dc:title' => 'Search Fandango',
        'upnp:album_art' => 'http://lh5.googleusercontent.com/-V45S-ZOQtps/TwaKKNoy73I/AAAAAAAAAGY/YlHVrqjv9Y0/s220/f.png',
        'upnp:class' => 'object.container',
      );
    }
    if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/umsp/plugins/rottentomatoes/rottentomatoes.php')) {
      $Items[] = array(
        'id' => 'umsp://plugins/rottentomatoes/rottentomatoes?' . build_query('search', array($title)),
        'dc:title' => 'Search Rotten Tomatoes',
        'upnp:album_art' => 'http://lh5.googleusercontent.com/-rCCouY9gLJ4/TwQU9zc1AgI/AAAAAAAAAGM/56wLDGXHwpA/s200/rottentomatoes.png',
        'upnp:class' => 'object.container',
      );
    }
  } else {
    $Items[] = array(
      'id' => build_umsp_url('associate', array($file, 'trailer_menu', array($file))),
      'dc:title' => 'Associate with IMDb ID first',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

////////////////////////////////////////////////////////////////////////////////

function subtitle_menu($file) {
  $Items[] = array(
    'id' => build_umsp_url('subtitle_search_by_hash', array($file)),
    'dc:title' => 'Search by movie hash of "' . basename($file) . '"',
    'upnp:album_art' => 'http://lh6.googleusercontent.com/-JELgj24qhkU/Tw19nmeylbI/AAAAAAAAAas/oVUHt59Ppbc/s250/opensubtitles-logo.png',
    'upnp:class' => 'object.container',
  );
  if (getConfigValue('SUBTITLES_MPC_METHOD', '0')) {
    $Items[] = array(
      'id' => build_umsp_url('subtitle_search_by_hash_mpc', array($file)),
      'dc:title' => 'Download using movie hash of "' . basename($file) . '"',
      'upnp:album_art' => 'http://lh6.googleusercontent.com/-JELgj24qhkU/Tw19nmeylbI/AAAAAAAAAas/oVUHt59Ppbc/s250/opensubtitles-logo.png',
      'upnp:class' => 'object.container',
    );
  }
  $Items[] = array(
    'id' => build_umsp_url('subtitle_search_by_imdbid', array($file)),
    'dc:title' => 'Search by IMDb ID',
    'upnp:album_art' => 'http://lh6.googleusercontent.com/-JELgj24qhkU/Tw19nmeylbI/AAAAAAAAAas/oVUHt59Ppbc/s250/opensubtitles-logo.png',
    'upnp:class' => 'object.container',
  );
  $movie_name_year = movie_name_year_from_filename($file);
  $query = trim($movie_name_year['name'] . ' ' . $movie_name_year['year']);
  $Items[] = array(
    'id' => build_umsp_url('subtitle_search_by_text', array($file, $query)),
    'dc:title' => 'Search by text (' . $query . ')',
    'upnp:album_art' => 'http://lh6.googleusercontent.com/-JELgj24qhkU/Tw19nmeylbI/AAAAAAAAAas/oVUHt59Ppbc/s250/opensubtitles-logo.png',
    'upnp:class' => 'object.container',
  );
  if (is_single_video_dir(dirname($file))) {
    $movie_name_year_from_dir = movie_name_year_from_filename(dirname($file));
    if ($movie_name_year_from_dir != $movie_name_year) {
      $query = trim($movie_name_year_from_dir['name'] . ' ' . $movie_name_year_from_dir['year']);
      $Items[] = array(
        'id' => build_umsp_url('subtitle_search_by_text', array($file, $query)),
        'dc:title' => 'Search by text (' . $query . ')',
        'upnp:album_art' => 'http://lh6.googleusercontent.com/-JELgj24qhkU/Tw19nmeylbI/AAAAAAAAAas/oVUHt59Ppbc/s250/opensubtitles-logo.png',
        'upnp:class' => 'object.container',
      );
    }
  }
  $Items[] = array (
    'id' => build_umsp_url('search_button'),
    'dc:title' => 'Type search query',
    'upnp:album_art' => 'http://lh3.googleusercontent.com/-F9qS4ys3A6M/TvGzha58eYI/AAAAAAAAAEw/2b1JcrEneXA/s220/search_icon.png',
    'upnp:class' => 'object.container',
  );
  $Items[] = array(
    'id' => build_umsp_url('select_subtitle_language', array($file)),
    'dc:title' => 'Select language for subtitle searches',
    'upnp:album_art' => 'http://lh6.googleusercontent.com/-FERXWgnwD4w/Twl4tnUe44I/AAAAAAAAAZ0/tXtCT5j-674/s220/worldmap.jpg',
    'upnp:class' => 'object.container',
  );
  $Items[] = array(
    'id' => build_umsp_url('addic7ed_subtitle_search_by_text', array($file, basename($file))),
    'dc:title' => 'Search addic7ed by text (' . basename($file) . ')',
    'upnp:album_art' => 'http://lh4.googleusercontent.com/-pkNtJQloGx8/TxkW6v80pGI/AAAAAAAAAbc/zChsZ5-_i24/s150/addic7ed.png',
    'upnp:class' => 'object.container',
  );
  $query = trim($movie_name_year['name'] . ' ' . $movie_name_year['year']);
  $Items[] = array(
    'id' => build_umsp_url('subs4free_subtitle_search_by_text', array($file, $query)),
    'dc:title' => 'Search subs4free by text (' . $query . ')',
    'upnp:album_art' => 'http://lh4.googleusercontent.com/-A9ObXh_MOGY/T18mTEgAPzI/AAAAAAAAAcE/Usg9B3oHggA/s220/s4f.png',
    'upnp:class' => 'object.container',
  );
  return $Items;
}

function subtitle_search_by_hash_mpc($file) {
  include_once('opensubtitles.php');
  $count = OpenSubtitles::DownloadAllUsingHash($file, getConfigValue('SUBTITLES_ISO639', 'en'));
  $Items[] = array(
    'id' => build_umsp_url('play', array($file)),
    'res' => 'file://' . $file,
    'dc:title' => 'Downloaded ' . $count . ' subtitles using movie hash of "' . basename($file) . '"',
    'upnp:album_art' => get_thumbnail($file),
    'upnp:class' => 'object.item.videoItem',
    'protocolInfo' => 'http-get:*:*:*',
  );
  return $Items;
}

function subtitle_search_by_hash($file) {
  include_once('opensubtitles.php');
  $sublanguageid = getConfigValue('SUBTITLES_LANG', 'eng');
  $openSubtitles = new OpenSubtitles();
  $subtitles = $openSubtitles->SearchByHash($sublanguageid, $file);
  if (empty($subtitles)) {
    $Items[] = array(
      'id' => build_umsp_url('subtitle_menu', array($file)),
      'dc:title' => 'No ' . $sublanguageid . ' subtitles found for "' . basename($file) . '" using movie hash',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-QPLkp80BR2U/Twl0_91OJkI/AAAAAAAAAZI/AyiL5wgsJJk/s220/error.png',
      'upnp:class' => 'object.container',
    );
  } else {
    populate_subtitle_items($Items, $subtitles, $file);
  }
  return $Items;
}

function subtitle_search_by_text($file, $query) {
  include_once('opensubtitles.php');
  $sublanguageid = getConfigValue('SUBTITLES_LANG', 'eng');
  $openSubtitles = new OpenSubtitles();
  $subtitles = $openSubtitles->SearchByText($sublanguageid, $query);
  if (empty($subtitles)) {
    $Items[] = array(
      'id' => build_umsp_url('subtitle_menu', array($file)),
      'dc:title' => 'No ' . $sublanguageid . ' subtitles found for "' . $query . '"',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-QPLkp80BR2U/Twl0_91OJkI/AAAAAAAAAZI/AyiL5wgsJJk/s220/error.png',
      'upnp:class' => 'object.container',
    );
  } else {
    populate_subtitle_items($Items, $subtitles, $file);
  }
  return $Items;
}

function subtitle_search_by_imdbid($file) {
  $imdbid = retrieve_imdbid($file);
  if ($imdbid) {
    include_once('opensubtitles.php');
    $sublanguageid = getConfigValue('SUBTITLES_LANG', 'eng');
    $openSubtitles = new OpenSubtitles();
    $subtitles = $openSubtitles->SearchByImdbId($sublanguageid, $imdbid);
    if (empty($subtitles)) {
      $Items[] = array(
        'id' => build_umsp_url('subtitle_menu', array($file)),
        'dc:title' => 'No ' . $sublanguageid . ' subtitles found for IMDb id: ' . $imdbid,
        'upnp:album_art' => 'http://lh3.googleusercontent.com/-QPLkp80BR2U/Twl0_91OJkI/AAAAAAAAAZI/AyiL5wgsJJk/s220/error.png',
        'upnp:class' => 'object.container',
      );
    } else {
      populate_subtitle_items($Items, $subtitles, $file);
    }
  } else {
    $Items[] = array(
      'id' => build_umsp_url('associate', array($file, 'subtitle_search_by_imdbid', array($file))),
      'dc:title' => 'Associate with IMDb ID first',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

function populate_subtitle_items(&$Items, $subtitles, $file) {
  $path_parts = pathinfo($file);
  $path_prefix = $path_parts['dirname'] . '/' . basename($file, '.' . $path_parts['extension']);
  foreach ($subtitles as $subtitle) {
    $SaveToFile = $path_prefix . '.' . $subtitle['SubLanguageID'] . '_' . $subtitle['IDSubtitleFile'] . '.' . $subtitle['SubFormat'];
    $Items[] = array(
      'id' => build_umsp_url('download_subtitle', array($subtitle['SubDownloadLink'], $SaveToFile, $file)),
      'dc:title' => $subtitle['SubFileName'] . ' (' . $subtitle['SubLanguageID'] . ', ' . $subtitle['IDSubtitleFile'] . ', ' . $subtitle['SubDownloadsCnt'] . 'x)',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-1xBuycFB6wc/Twl1A2Z62NI/AAAAAAAAAZQ/ubbNJYJnnHs/s220/download.png',
      'upnp:class' => 'object.container',
    );
  }
  if (count($subtitles) > 1 && count($subtitles) < 10) {
    foreach ($subtitles as $subtitle) {
      $all_subtitles[] = array(
        'i' => $subtitle['IDSubtitleFile'],
        'l' => $subtitle['SubLanguageID'],
        'f' => $subtitle['SubFormat'],
        'd' => $subtitle['SubDownloadLink'],
      );
    }
    $Items[] = array(
      'id' => build_umsp_url('download_all_subtitles', array($all_subtitles, $file)),
      'dc:title' => 'Download all',
      'upnp:album_art' => 'http://lh6.googleusercontent.com/-_yXBZBqodJU/Twl2zRyDaDI/AAAAAAAAAZc/yflhus5Cv0w/s220/download-icon.png',
      'upnp:class' => 'object.container',
    );
  }
}

function download_subtitle($subDownloadLink, $saveToFile, $file) {
  include_once('opensubtitles.php');
  $downloaded = OpenSubtitles::download($subDownloadLink, $saveToFile);
  $Items[] = array(
    'id' => build_umsp_url('subtitle_menu', array($file)),
    'dc:title' => ($downloaded ? 'Saved to: ' : 'Failed to save to: ') . basename($saveToFile),
    'upnp:album_art' => $downloaded ?
        'http://lh4.googleusercontent.com/-_TvE_KoXL2U/Twl0_VtZoeI/AAAAAAAAAZA/snBUZ9GWg0Q/s220/success.png' :
        'http://lh3.googleusercontent.com/-QPLkp80BR2U/Twl0_91OJkI/AAAAAAAAAZI/AyiL5wgsJJk/s220/error.png',
    'upnp:class' => 'object.container',
  );
  return $Items;
}

function download_all_subtitles($subtitles, $file) {
  include_once('opensubtitles.php');
  $path_parts = pathinfo($file);
  $path_prefix = $path_parts['dirname'] . '/' . basename($file, '.' . $path_parts['extension']);
  foreach ($subtitles as $subtitle) {
    $saveToFile = $path_prefix . '.' . $subtitle['l'] . '_' . $subtitle['i'] . '.' . $subtitle['f'];
    OpenSubtitles::download($subtitle['d'], $saveToFile);
  }
  return video_menu($file);
}

function select_subtitle_language($file, $language = '') {
  include_once('opensubtitles.php');
  if ($language == '') {
    $languages = OpenSubtitles::languages();
    foreach ($languages as $IdSubLanguage => $LanguageName) {
      $Items[] = array(
        'id' => build_umsp_url('select_subtitle_language', array($file, $IdSubLanguage)),
        'dc:title' => $LanguageName,
        'upnp:class' => 'object.container',
      );
    }
    return $Items;
  } else {
    putConfigValue('SUBTITLES_LANG', $language);
    return subtitle_menu($file);
  }
}

function addic7ed_subtitle_search_by_text($file, $query) {
  include_once('addic7ed.php');
  $results = addic7ed::search($query);
  if (empty($results)) {
    $Items[] = array(
      'id' => build_umsp_url('subtitle_menu', array($file)),
      'dc:title' => 'No results for "' . $query . '"',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-QPLkp80BR2U/Twl0_91OJkI/AAAAAAAAAZI/AyiL5wgsJJk/s220/error.png',
      'upnp:class' => 'object.container',
    );
  } else {
    foreach ($results as $result) {
      $Items[] = array(
        'id' => build_umsp_url('addic7ed_subtitles', array($file, $result['href'])),
        'dc:title' => $result['title'],
        'upnp:class' => 'object.container',
      );
    }
  }
  return $Items;
}

function addic7ed_subtitles($file, $href) {
  include_once('addic7ed.php');
  $subtitles = addic7ed::subtitles($href);
  foreach ($subtitles as $subtitle) {
    $Items[] = array(
      'id' => build_umsp_url('addic7ed_download_subtitle', array($file, $subtitle['href'])),
      'dc:title' => $subtitle['title'],
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

function addic7ed_download_subtitle($file, $href) {
  include_once('addic7ed.php');
  $saveToFile = replace_extension($file, str_replace('/', '.', substr($href, 1)) . ".srt");
  $downloaded = addic7ed::download($href, $saveToFile);
  $Items[] = array(
    'id' => build_umsp_url('subtitle_menu', array($file)),
    'dc:title' => ($downloaded ? 'Saved to: ' : 'Failed to save to: ') . basename($saveToFile),
    'upnp:album_art' => $downloaded ?
        'http://lh4.googleusercontent.com/-_TvE_KoXL2U/Twl0_VtZoeI/AAAAAAAAAZA/snBUZ9GWg0Q/s220/success.png' :
        'http://lh3.googleusercontent.com/-QPLkp80BR2U/Twl0_91OJkI/AAAAAAAAAZI/AyiL5wgsJJk/s220/error.png',
    'upnp:class' => 'object.container',
  );
  return $Items;
}

function subs4free_subtitle_search_by_text($file, $query) {
  include_once('subs4free.php');
  $results = subs4free::search($query);
  if (empty($results)) {
    $Items[] = array(
      'id' => build_umsp_url('subtitle_menu', array($file)),
      'dc:title' => 'No results for "' . $query . '"',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-QPLkp80BR2U/Twl0_91OJkI/AAAAAAAAAZI/AyiL5wgsJJk/s220/error.png',
      'upnp:class' => 'object.container',
    );
  } else {
    foreach ($results as $result) {
      $Items[] = array(
        'id' => build_umsp_url('subs4free_download_subtitle', array($file, $result['href'], $result['id'])),
        'dc:title' => $result['title'],
        'upnp:class' => 'object.container',
      );
    }
  }
  return $Items;
}

function subs4free_download_subtitle($file, $href, $id) {
  include_once('subs4free.php');
  $saveToFile = replace_extension($file, $id . ".srt");
  $downloaded = subs4free::download($href, $saveToFile);
  $Items[] = array(
    'id' => build_umsp_url('subtitle_menu', array($file)),
    'dc:title' => ($downloaded ? 'Saved to: ' : 'Failed to save to: ') . basename($saveToFile),
    'upnp:album_art' => $downloaded ?
        'http://lh4.googleusercontent.com/-_TvE_KoXL2U/Twl0_VtZoeI/AAAAAAAAAZA/snBUZ9GWg0Q/s220/success.png' :
        'http://lh3.googleusercontent.com/-QPLkp80BR2U/Twl0_91OJkI/AAAAAAAAAZI/AyiL5wgsJJk/s220/error.png',
    'upnp:class' => 'object.container',
  );
  return $Items;
}

////////////////////////////////////////////////////////////////////////////////

function poster_menu($file) {
  $imdbid = retrieve_imdbid($file);
  if ($imdbid) {
    $movieInfo = Imdb::getInfo($imdbid);
    $title = Imdb::formatTitle($movieInfo);
    foreach ($movieInfo['cover'] as $cover) {
      $thumb = Imdb::formatThumbUrl($cover);
      $Items[] = array(
        'id' => build_umsp_url('download_poster', array($thumb, $file)),
        'dc:title' => $title,
        'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/tronik-player/img-proxy.php?url=' . urlencode($thumb),
        'upnp:class' => 'object.container',
      );
    }
    $movieInfo = Tmdb::imdbLookup($imdbid, false);
    $title = Imdb::formatTitle($movieInfo);
    foreach ($movieInfo['cover'] as $cover) {
      $Items[] = array(
        'id' => build_umsp_url('download_poster', array($cover, $file)),
        'dc:title' => $title,
        'upnp:album_art' => $cover,
        'upnp:class' => 'object.container',
      );
    }
  } else {
    $Items[] = array(
      'id' => build_umsp_url('associate', array($file, 'poster_menu', array($file))),
      'dc:title' => 'Associate with IMDb ID first',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

function download_poster($image, $file) {
  $image_contents = file_get_contents($image);
  if ($image_contents != '') {
    $fp = fopen(replace_extension($file, 'jpg'), 'w');
    fwrite($fp, $image_contents);
    fclose($fp);
    if (is_single_video_dir(dirname($file))) {
      $fp = fopen(dirname($file) . '/folder.jpg', 'w');
      fwrite($fp, $image_contents);
      fclose($fp);
    }
  }
  return video_menu($file);
}

////////////////////////////////////////////////////////////////////////////////

function moviesheet_menu($file) {
  $moviesheet = expand_moviesheet($file, getConfigValue('MOVIESHEET_FILENAME', '$M/$F_sheet.jpg'));
  if (file_exists($moviesheet)) {
    $Items[] = array(
      'id' => build_umsp_url('moviesheet', array($moviesheet)),
      'res' => proxy_local_image_res($moviesheet),
      'dc:title' => 'View existing moviesheet',
      'upnp:album_art' => proxy_local_image($moviesheet),
      'upnp:class' => 'object.item.imageItem',
      'protocolInfo' => 'http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_LRG',
    );
  }
  $imdbid = retrieve_imdbid($file);
  if ($imdbid) {
    $Items[] = array(
      'id' => build_umsp_url('moviesheet_generate', array($file, $imdbid, false, $moviesheet)),
      'dc:title' => 'Generate moviesheet using IMDb data',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
      'upnp:class' => 'object.container',
    );
    $Items[] = array(
      'id' => build_umsp_url('moviesheet_generate', array($file, $imdbid, true, $moviesheet)),
      'dc:title' => 'Generate moviesheet using themoviedb data',
      'upnp:album_art' => 'http://lh6.googleusercontent.com/-Ih1Z0O0stWQ/Twl3WdnvUGI/AAAAAAAAAZk/PQLbqmtKoXM/s220/tmdb-logo.png',
      'upnp:class' => 'object.container',
    );
    /*$res = 'http://127.0.0.1/umsp/plugins/tronik-player/tronik-player.php?file=' . urlencode($file) . '&moviesheet';
    $Items[] = array(
      'id' => build_umsp_url('moviesheet', array($imdbid, false)),
      'res' => $res  . '&imdbid=' . urlencode($imdbid) . '&tmdb=0',
      'dc:title' => 'View moviesheet using IMDb data',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
      'upnp:class' => 'object.item.imageItem',
      'protocolInfo' => 'http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_LRG',
    );
    $Items[] = array(
      'id' => build_umsp_url('moviesheet', array($imdbid, true)),
      'res' => $res  . '&imdbid=' . urlencode($imdbid) . '&tmdb=1',
      'dc:title' => 'View moviesheet using themoviedb data',
      'upnp:album_art' => 'http://lh6.googleusercontent.com/-Ih1Z0O0stWQ/Twl3WdnvUGI/AAAAAAAAAZk/PQLbqmtKoXM/s220/tmdb-logo.png',
      'upnp:class' => 'object.item.imageItem',
      'protocolInfo' => 'http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_LRG',
    );*/
    $Items[] = array(
      'id' => build_umsp_url('select_moviesheet_language', array($file)),
      'dc:title' => 'Select moviesheet language',
      'upnp:album_art' => 'http://lh5.googleusercontent.com/-98LS3YvT2cc/TxJhQZstA2I/AAAAAAAAAa0/xx2uBNTOQ8E/s150/google-translate.png',
      'upnp:class' => 'object.container',
    );
  } else {
    $Items[] = array(
      'id' => build_umsp_url('associate', array($file, 'moviesheet_menu', array($file))),
      'dc:title' => 'Associate with IMDb ID first',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

function moviesheet_generate($file, $imdbid, $tmdb, $saveToFile = null) {
  include_once('googletranslate.php');
  include_once('thumbgen.php');

  $hl = getConfigValue('MOVIESHEET_LANGUAGE', 'en');
  if ($tmdb) {
    $movieInfo = Tmdb::imdbLookup($imdbid, true, $hl);
  } else {
    $movieInfo = Imdb::getInfo($imdbid, $hl);
  }
  if (!$movieInfo['translated'] && $movieInfo['plot']) {
    $movieInfo['plot'] = GoogleTranslate::translate($movieInfo['plot'], 'en', $hl);
  }

  $template = getConfigValueFiles('MOVIESHEET_TEMPLATE', array(
      $_SERVER['DOCUMENT_ROOT'] . '/umsp/plugins/tronik-player/templates/default.xml'));
  $font = getConfigValueFiles('MOVIESHEET_FONT', array(
      $_SERVER['DOCUMENT_ROOT'] . '/umsp/plugins/reader/fonts/arial.ttf',
      '/lib/qt/fonts/Arial Regular.ttf',
      'C:/WINDOWS/Fonts/arial.ttf'));

  $tg = new ThumbGen($template, $font);
  $img = $tg->generate($file, $movieInfo);
  if (!file_exists(dirname($saveToFile))) {
    mkdir(dirname($saveToFile), 0777, true);
  }
  imagejpeg($img, $saveToFile);
  imagedestroy($img);
  if ($saveToFile) {
    $Items[] = array(
      'id' => build_umsp_url('moviesheet', array($saveToFile)),
      'res' => proxy_local_image_res($saveToFile),
      'dc:title' => 'View generated moviesheet',
      'upnp:album_art' => proxy_local_image($saveToFile),
      'upnp:class' => 'object.item.imageItem',
      'protocolInfo' => 'http-get:*:image/jpeg:DLNA.ORG_PN=JPEG_LRG',
    );
    return $Items;
  }
}

function select_moviesheet_language($file, $hl = '') {
  if ($hl == '') {
    include_once('googletranslate.php');
    $languages = GoogleTranslate::languages();
    foreach ($languages as $hl => $language) {
      $Items[] = array(
        'id' => build_umsp_url('select_moviesheet_language', array($file, $hl)),
        'dc:title' => $language,
        'upnp:class' => 'object.container',
      );
    }
    return $Items;
  } else {
    putConfigValue('MOVIESHEET_LANGUAGE', $hl);
    return moviesheet_menu($file);
  }
}

function expand_moviesheet($file, $moviesheet) {
  $path_parts = pathinfo($file);
  $moviesheet = str_replace('\\', '/', $moviesheet);
  $moviesheet = str_replace('$M', $path_parts['dirname'], $moviesheet);
  $moviesheet = str_replace('$P', basename(dirname($path_parts['dirname'])), $moviesheet);
  $moviesheet = str_replace('$F', basename($path_parts['dirname']), $moviesheet);
  $moviesheet = str_replace('$N', basename($file, '.' . $path_parts['extension']), $moviesheet);
  $moviesheet = str_replace('$E', '.' . $path_parts['extension'], $moviesheet);
  return $moviesheet;
}

////////////////////////////////////////////////////////////////////////////////

function associate($file, $callback, $callback_args) {
  $imdbid = retrieve_imdbid($file);
  if ($imdbid) {
    $movieInfo = Imdb::getInfo($imdbid);
    $title = Imdb::formatTitle($movieInfo);
    $thumb = Imdb::formatThumbUrl($movieInfo['cover'][0]);
    $Items[] = array(
      'id' => build_umsp_url('video_menu', array($file)),
      'dc:title' => 'Already associated with IMDb ID: ' . $imdbid . ' title: ' . $title,
      'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/tronik-player/img-proxy.php?url=' . urlencode($thumb),
      'upnp:class' => 'object.container',
    );
  } else {
    foreach (extract_imdbids_from_nfos($file) as $imdbid => $nfo) {
      $movieInfo = Imdb::getInfo($imdbid);
      $title = Imdb::formatTitle($movieInfo);
      $thumb = Imdb::formatThumbUrl($movieInfo['cover'][0]);
      $Items[] = array(
        'id' => build_umsp_url('store_imdbid', array($file, $imdbid, $callback, $callback_args)),
        'dc:title' => 'Use found IMDb ID: ' . $imdbid . ' in: "' . $nfo . '" title: ' . $title,
        'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/tronik-player/img-proxy.php?url=' . urlencode($thumb),
        'upnp:class' => 'object.container',
      );
    }
  }
  $movie_name_year = movie_name_year_from_filename($file);
  $query = trim($movie_name_year['name']);
  $Items[] = array(
    'id' => build_umsp_url('associate_imdb', array($file, $query, $callback, $callback_args)),
    'dc:title' => 'Search IMDb for "' . $query . '"',
    'upnp:album_art' => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
    'upnp:class' => 'object.container',
  );
  $query = trim($movie_name_year['name'] . ' ' . $movie_name_year['year']);
  $Items[] = array(
    'id' => build_umsp_url('associate_tmdb', array($file, $query, $callback, $callback_args)),
    'dc:title' => 'Search themoviedb for "' . $query . '"',
    'upnp:album_art' => 'http://lh6.googleusercontent.com/-Ih1Z0O0stWQ/Twl3WdnvUGI/AAAAAAAAAZk/PQLbqmtKoXM/s220/tmdb-logo.png',
    'upnp:class' => 'object.container',
  );
  if (is_single_video_dir(dirname($file))) {
    $movie_name_year_from_dir = movie_name_year_from_filename(dirname($file));
    if ($movie_name_year_from_dir != $movie_name_year) {
      $query = trim($movie_name_year_from_dir['name']);
      $Items[] = array(
        'id' => build_umsp_url('associate_imdb', array($file, $query, $callback, $callback_args)),
        'dc:title' => 'Search IMDb for "' . $query. '"',
        'upnp:album_art' => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
        'upnp:class' => 'object.container',
      );
      $query = trim($movie_name_year_from_dir['name'] . ' ' . $movie_name_year_from_dir['year']);
      $Items[] = array(
        'id' => build_umsp_url('associate_tmdb', array($file, $query, $callback, $callback_args)),
        'dc:title' => 'Search themoviedb for "' . $query. '"',
        'upnp:album_art' => 'http://lh6.googleusercontent.com/-Ih1Z0O0stWQ/Twl3WdnvUGI/AAAAAAAAAZk/PQLbqmtKoXM/s220/tmdb-logo.png',
        'upnp:class' => 'object.container',
      );
    }
  }
  $Items[] = array(
    'id' => build_umsp_url('associate_opensubtitles', array($file, $callback, $callback_args)),
    'dc:title' => 'Search opensubtitles using movie hash',
    'upnp:album_art' => 'http://lh6.googleusercontent.com/-JELgj24qhkU/Tw19nmeylbI/AAAAAAAAAas/oVUHt59Ppbc/s250/opensubtitles-logo.png',
    'upnp:class' => 'object.container',
  );
  $Items[] = array (
    'id' => build_umsp_url('search_button'),
    'dc:title' => 'Type search query',
    'upnp:album_art' => 'http://lh3.googleusercontent.com/-F9qS4ys3A6M/TvGzha58eYI/AAAAAAAAAEw/2b1JcrEneXA/s220/search_icon.png',
    'upnp:class' => 'object.container',
  );

  return $Items;
}

function associate_opensubtitles($file, $callback, $callback_args) {
  include_once('opensubtitles.php');
  $openSubtitles = new OpenSubtitles();
  $movies = $openSubtitles->CheckMovieHash($file);
  foreach ($movies as $movie) {
    if (isset($movie['MovieImdbID'])) {
      $imdbid = 'tt' . $movie['MovieImdbID'];
      $movieInfo = Imdb::getInfo($imdbid);
      $title = Imdb::formatTitle($movieInfo);
      $thumb = Imdb::formatThumbUrl($movieInfo['cover'][0]);
      $Items[] = array(
        'id' => build_umsp_url('store_imdbid', array($file, $imdbid, $callback, $callback_args)),
        'dc:title' => $title,
        'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/tronik-player/img-proxy.php?url=' . urlencode($thumb),
        'upnp:class' => 'object.container',
      );
    }
  }
  return $Items;
}

function associate_imdb($file, $query, $callback, $callback_args, $popular_titles = true) {
  $movies = Imdb::find($query, $popular_titles);
  if ($popular_titles && empty($movies)) {
    return associate_imdb($file, $query, $callback, $callback_args, false);
  }
  foreach ($movies as $movie) {
    $title = Imdb::formatTitle($movie);
    $thumb = Imdb::formatThumbUrl($movie['cover'][0]);
    $Items[] = array(
      'id' => build_umsp_url('store_imdbid', array($file, $movie['id'], $callback, $callback_args)),
      'dc:title' => $title,
      'upnp:album_art' => 'http://127.0.0.1/umsp/plugins/tronik-player/img-proxy.php?url=' . urlencode($thumb),
      'upnp:class' => 'object.container',
    );
  }
  if ($popular_titles) {
    $Items[] = array(
      'id' => build_umsp_url('associate_imdb', array($file, $query, $callback, $callback_args, false)),
      'dc:title' => 'More matches...',
      'upnp:album_art' => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
      'upnp:class' => 'object.container',
    );
  }
  if (!isset($Items) || empty($Items)) {
    $Items[] = array(
      'id' => build_umsp_url('associate', array($file, $callback, $callback_args)),
      'dc:title' => 'No matches for: ' . $query,
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

function associate_tmdb($file, $query, $callback, $callback_args) {
  $movies = Tmdb::find($query);
  foreach ($movies as $movie) {
    $title = Imdb::formatTitle($movie);
    $thumb = $movie['cover'][0];
    $Items[] = array(
      'id' => build_umsp_url('store_imdbid', array($file, $movie['id'], $callback, $callback_args)),
      'dc:title' => $title,
      'upnp:album_art' => $thumb,
      'upnp:class' => 'object.container',
    );
  }
  if (!isset($Items) || empty($Items)) {
    $Items[] = array(
      'id' => build_umsp_url('associate', array($file, $callback, $callback_args)),
      'dc:title' => 'No matches for: ' . $query,
      'upnp:class' => 'object.container',
    );
  }
  return $Items;
}

////////////////////////////////////////////////////////////////////////////////

function advanced_menu($file) {
  $watched_file = $file . '.watched';
  $watched = file_exists($watched_file);
  $Items[] = array(
    'id' => build_umsp_url('mediamark_toggle', array($file, $watched_file, 'advanced_menu', array($file))),
    'dc:title' => ($watched ? 'Unmark' : 'Mark') . ' watched',
    'upnp:album_art' => $watched
        ? 'http://lh6.googleusercontent.com/-yK-srJ6jZwg/T1Ms-4BbzMI/AAAAAAAAAb4/0iyO2JJoTjk/s100/uncheck.png'
        : 'http://lh4.googleusercontent.com/-MeeoUOkfAVA/T1Ms-4hJlxI/AAAAAAAAAb0/AOW4vRep_P4/s100/check.png',
    'upnp:class' => 'object.container',
  );
  $Items[] = array(
    'id' => build_umsp_url('mediainfo', array($file)),
    'res' => 'http://127.0.0.1/umsp/plugins/tronik-player/mediainfo.php?file=' . urlencode($file),
    'dc:title' => 'MediaInfo for "' . basename($file) . '"',
    'upnp:album_art' => 'http://lh5.googleusercontent.com/-M-VtLxyXXcc/Twl56VV-adI/AAAAAAAAAZ8/5bC2mUoVtLM/s220/mediainfo.png',
    'upnp:class' => 'object.item.imageItem',
    'protocolInfo' => 'http-get:*:image/png:DLNA.ORG_PN=PNG_LRG',
  );
  $path_parts = pathinfo($file);
  if ($path_parts['extension'] == 'mp4') {
    $Items[] = array(
      'id' => build_umsp_url('convertToac3', array($file)),
      'dc:title' => 'Convert audio to AC3 (output mkv in same directory)',
      'upnp:album_art' => 'http://lh6.googleusercontent.com/-IxA15GIlaKk/Twl6D54R6pI/AAAAAAAAAaE/zXOswA4TX6Q/s220/speaker.png',
      'upnp:class' => 'object.container',
    );
  }
  $Items[] = array(
    'id' => build_umsp_url('associate', array($file, 'video_menu', array($file))),
    'dc:title' => 'Associate with IMDb ID',
    'upnp:album_art' => 'http://lh3.googleusercontent.com/-Lb2M2n5a1nc/Tvgl4iIoAzI/AAAAAAAAAFc/MD4BA9jxIlA/s200/imdb-logo.png',
    'upnp:class' => 'object.container',
  );
  return $Items;
}

function convertToac3($file, $overwrite = false) {
  $mkv_file = replace_extension($file, 'mkv');
  if (file_exists($mkv_file) && !$overwrite) {
    $Items[] = array(
      'id' => build_umsp_url('convertToac3', array($file, true)),
      'dc:title' => 'Overwrite ' . basename($mkv_file),
      'upnp:class' => 'object.container',
    );
  }
  if (file_exists($mkv_file) && $overwrite) {
    if (!unlink($mkv_file)) {
      $Items[] = array(
        'id' => build_umsp_url('convertToac3', array($file, $overwrite)),
        'dc:title' => 'Could not delete ' . basename($mkv_file),
        'upnp:album_art' => 'http://lh3.googleusercontent.com/-QPLkp80BR2U/Twl0_91OJkI/AAAAAAAAAZI/AyiL5wgsJJk/s220/error.png',
        'upnp:class' => 'object.container',
      );
    }
  }
  if (!file_exists($mkv_file)) {
    if (shell_exec('ffmpeg') == '') {
      $Items[] = array(
        'id' => build_umsp_url('convertToac3', array($file, $overwrite)),
        'dc:title' => 'Required ffmpeg app not found',
        'upnp:album_art' => 'http://lh3.googleusercontent.com/-QPLkp80BR2U/Twl0_91OJkI/AAAAAAAAAZI/AyiL5wgsJJk/s220/error.png',
        'upnp:class' => 'object.container',
      );
    } else {
      execInBackground('ffmpeg -loglevel quiet -i ' . escapeshellarg($file) .
          ' -vcodec copy -scodec copy -acodec ac3 -ab 448k -f matroska ' .
          escapeshellarg($mkv_file));
      $Items[] = array(
        'id' => build_umsp_url('play', array($mkv_file)),
        'res' => 'file://' . $mkv_file,
        'dc:title' => 'Started conversion. Play "' . basename($mkv_file) . '"',
        'upnp:album_art' => get_thumbnail($mkv_file),
        'upnp:class' => 'object.item.videoItem',
        'protocolInfo' => 'http-get:*:*:*',
      );
    }
  }
  return $Items;
}

function execInBackground($cmd) {
  if (substr(php_uname(), 0, 7) == 'Windows'){
    pclose(popen('start /B '. $cmd, 'r'));
  } else {
    exec($cmd . ' > /dev/null &');
  }
}

////////////////////////////////////////////////////////////////////////////////

function dir_glob($dir, $pattern, $flags = 0) {
  return glob(str_replace('[', '[[]', $dir) . '/' . $pattern, $flags);
}

function get_supported_files() {
  return getConfigValue('SUPPORTED_FILES',
      '*.3g2,*.3gp,*.3gp2,*.3gpp,*.60d,' .
      '*.ajp,*.asf,*.asx,*.avchd,*.avi,*.bik,*.bix,*.box,*.cam,*.dat,*.divx,*.dmf,*.dv,*.dvr-ms,' .
      '*.evo,*.flc,*.fli,*.flic,*.flv,*.flx,*.gvi,*.gvp,*.h264,' .
      '*.m1v,*.m2p,*.m2ts,*.m2v,*.m4e,*.m4v,*.mjp,*.mjpeg,*.mjpg,*.mkv,' .
      '*.moov,*.mov,*.movhd,*.movie,*.movx,*.mp4,*.mpe,*.mpeg,*.mpg,*.mpv,*.mpv2,*.mxf,' .
      '*.nsv,*.nut,*.ogg,*.ogm,*.omf,*.ps,*.qt,*.ram,*.rm,*.rmvb,*.swf,*.ts,' .
      '*.vfw,*.vid,*.video,*.viv,*.vivo,*.vob,*.vro,' .
      '*.wm,*.wmv,*.wmx,*.wrap,*.wvx,*.wx,*.x264,*.xvid');
}

function list_videos($dir, $supported_files) {
  $videos = dir_glob($dir, '{' . $supported_files . '}', GLOB_BRACE, GLOB_NOSORT);
  sort($videos);
  return $videos;
}

function is_video_dir($dir, $supported_files) {
  // This should recursively check that there is at least one video file at some level but it's too expensive
  return count(list_videos($dir, get_supported_files()))> 0 || count(dir_glob($dir, '*', GLOB_ONLYDIR)) > 0;
}

function is_single_video_dir($dir) {
  return count(list_videos($dir, get_supported_files())) == 1;
}

function replace_extension($file, $ext) {
  $path_parts = pathinfo($file);
  return $path_parts['dirname'] . '/' . $path_parts['filename'] . '.' . $ext;
}

function movie_name_year_from_filename($file) {
  $path_parts = pathinfo($file);
  $name = strtolower(trim(str_replace('.', ' ', $path_parts['filename'])));
  $year = '';
  if (preg_match("@(?P<name>.+?)\(?(?P<year>[0-9]{4}?)@", $name, $match)) {
    $name = trim($match['name']);
    $year = $match['year'];
  }
  return array('name' => $name, 'year' => $year);
}

function mediamark_toggle($file, $watched_file, $callback = null, $callback_args = null) {
  if (file_exists($watched_file)) {
    unlink($watched_file);
  } else {
    mediamark($file, $watched_file);
  }
  if ($callback != null) {
    return call_user_func_array($callback, $callback_args);
  }
}

function mediamark($file, $watched_file) {
  if (!file_exists($watched_file)) {
    $fp = fopen($watched_file, 'w');
    if ($fp) {
      fclose($fp);
    }
  }
}

function store_imdbid($file, $imdbid, $callback = null, $callback_args = null) {
  $id_file = $file . '.imdbid';
  $fp = fopen($id_file, 'w');
  if ($fp) {
    fwrite($fp, $imdbid);
    fclose($fp);
  }
  if ($callback != null) {
    return call_user_func_array($callback, $callback_args);
  }
}

function retrieve_imdbid($file) {
  $id_file = $file . '.imdbid';
  if (file_exists($id_file)) {
    return file_get_contents($id_file);
  }
  $tgmd_file = $file . '.tgmd';
  if (file_exists($tgmd_file) &&
      preg_match('@<id>(?P<imdbid>tt\d+?)</id>@i', file_get_contents($tgmd_file), $match)) {
    $imdbid = $match['imdbid'];
    store_imdbid($file, $imdbid);
    return $imdbid;
  }
  $nfo_imdbids = array_keys(extract_imdbids_from_nfos($file));
  if (count($nfo_imdbids) == 1) {
    $imdbid = $nfo_imdbids[0];
    store_imdbid($file, $imdbid);
    return $imdbid;
  }
  return '';
}

function extract_imdbids_from_nfos($file) {
  $imdbid_to_nfo = array();
  $nfos = array();
  if (is_single_video_dir(dirname($file))) {
    $nfos = dir_glob(dirname($file), '*.nfo');
  } else {
    $nfo = replace_extension($file, 'nfo');
    if (file_exists($nfo)) {
      $nfos[] = $nfo;
    }
  }
  foreach ($nfos as $nfo) {
    $nfo_contents = file_get_contents($nfo);
    if (preg_match_all('@imdb\.[^\/]+\/title\/(?P<imdbid>tt\d+?)/@i', $nfo_contents, $matches, PREG_SET_ORDER) ||
        preg_match_all('@<id>(?P<imdbid>tt\d+?)</id>@i', $nfo_contents, $matches, PREG_SET_ORDER)) {
      foreach ($matches as $match) {
        $imdbid_to_nfo[$match['imdbid']] = basename($nfo);
      }
    }
  }
  return $imdbid_to_nfo;
}

function get_thumbnail($file) {
  $img = replace_extension($file, 'jpg');
  if (file_exists($file . '.watched')) {
    $watched_img = $_SERVER['DOCUMENT_ROOT'] . '/umsp/plugins/tronik-player/checkMark.png';
    if (file_exists($img)) {
      return 'http://127.0.0.1/umsp/plugins/tronik-player/watched.php?img=' . urlencode($img) . '&watchedimg=' . urlencode($watched_img);
    } else {
      return proxy_local_image($watched_img);
    }
  }
  return proxy_local_image($img);
}

function is_umsp_test() {
  // return $_SERVER['SCRIPT_NAME'] == '/umsp/umsp-test.php';
  return false;
}

function proxy_local_image($img) {
  if (!file_exists($img)) return '';
  if (is_umsp_test()) {
    $img = 'http://127.0.0.1/umsp/plugins/tronik-player/img-proxy.php?url=' . urlencode($img);
  }
  return $img;
}

function proxy_local_image_res($img) {
  if (is_umsp_test()) {
    $img = 'http://127.0.0.1/umsp/plugins/tronik-player/img-proxy.php?url=' . urlencode($img);
  } else {
    $img = 'file://' . $img;
  }
  return $img;
}

function getConfigValueFiles($key, $default_values) {
  $value = getConfigValue($key, '');
  if (file_exists($value)) {
    return $value;
  }
  foreach ($default_values as $default_value) {
    if (file_exists($default_value)) {
      return $default_value;
    }
  }
  return '';
}

function getConfigValue($key, $default_value) {
  $conf_dir = function_exists('_getUMSPConfPath') ? _getUMSPConfPath() : '/conf';
  $config = file_get_contents($conf_dir . '/config');
  if (preg_match("/TRONIKPLAYER_$key='(.+)'/", $config, $matches)) {
    return trim($matches[1]);
  }
  return $default_value;
}

function putConfigValue($key, $value) {
  exec("sudo config_tool -c TRONIKPLAYER_$key='$value' >/dev/null 2>&1");
}

if (isset($_GET['moviesheet'])) {
  header('Content-Type: image/jpeg');
  moviesheet_generate($_GET['file'], $_GET['imdbid'], $_GET['tmdb']);
}

?>

Let me know if it works since I've only tested this under UMSPX.

others,
Please don't even bother requesting support for more subtitle sites. I only added support for subs4free because I am Greek.
TroNik
Developer
 
Posts: 46
Joined: Wed Dec 21, 2011 2:56 am

PreviousNext

Return to UMSP Plugins

Who is online

Users browsing this forum: No registered users and 1 guest