Playing .ts streams from m3u without often reloading.
#1
Hello.

I made plugin with code below. WARNING CONTAINS ADULT CONTENT! And it works like I want. But it is any way to play these .ts streams under KODI without reloading stream after 10-30 seconds afrer it start? Or it always must be done like that? Also I tried to google some info and I seen some m3u examples, but doing it like this way did not help. Some streams from this string list, plays only few seonds and after that it loads very slowly again. Others can play almost 45 seconds, but later it also disconnect and must be loaded again. And I did this code like I know KODI plugins development basics from some tutorial code. And it must be done as m3u open, because - as far as I know - XBMC can only open direct mp4 files or rtmp url without adding it to playlist other must be loaded like that to see anything. Please give me some hints and example code, how it can be done better withour often buffering. If it possible. Thanks in advance.

Code:
import sys, re, os, urllib, urllib2, xbmcplugin, xbmcgui, xbmcaddon, string

xxx_tv_playlist_text = """
Brazzers=http://hyper.de1-eu.com:8000/live/samsam/samsam/2777.ts
Eroxxx HD=http://hyper.de1-eu.com:8000/live/samsam/samsam/2776.ts
Frenchlover TV=http://hyper.de1-eu.com:8000/live/samsam/samsam/3875.ts
Hustler=http://hyper.de1-eu.com:8000/live/samsam/samsam/2770.ts
Hustler HD=http://hyper.de1-eu.com:8000/live/samsam/samsam/2769.ts
Playboy=http://hyper.de1-eu.com:8000/live/samsam/samsam/2773.ts
Platinum TV=http://hyper.de1-eu.com:8000/live/samsam/samsam/2767.ts
Privee TV=http://hyper.de1-eu.com:8000/live/samsam/samsam/2766.ts
Redlight HD=http://hyper.de1-eu.com:8000/live/samsam/samsam/2765.ts
SCT HQ=http://hyper.de1-eu.com:8000/live/samsam/samsam/2763.ts
SuperOne=http://hyper.de1-eu.com:8000/live/samsam/samsam/2761.ts
Venus=http://hyper.de1-eu.com:8000/live/samsam/samsam/2771.ts
"""

def makeLink(params, baseUrl=sys.argv[0]):
    return baseUrl + '?' + urllib.urlencode(dict([k.encode('utf-8'),unicode(v).encode('utf-8')] for k,v in params.items()))

def addMenuItem(caption, link, icon=None, thumbnail=None, folder=False):
    listItem = xbmcgui.ListItem(unicode(caption), iconImage=icon, thumbnailImage=thumbnail)
    listItem.setInfo(type="Video", infoLabels={ "Title": caption })
    return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=link, listitem=listItem, isFolder=folder)

def endListing():
    xbmcplugin.endOfDirectory(int(sys.argv[1]))

def parseParameters(inputString=sys.argv[2]):
    parameters = {}
    p1 = inputString.find('?')
    if p1 >= 0:
        splitParameters = inputString[p1 + 1:].split('&')
        for nameValuePair in splitParameters:
            if (len(nameValuePair) > 0):
                pair = nameValuePair.split('=')
                key = pair[0]
                value = urllib.unquote(urllib.unquote_plus(pair[1])).decode('utf-8')
                parameters[key] = value
    return parameters

def playMedia(title, thumbnail, link, mediaType='Video'):
    li = xbmcgui.ListItem(label=title, iconImage=thumbnail, thumbnailImage=thumbnail, path=link)
    li.setInfo(type=mediaType, infoLabels={ "Title": title })
    xbmc.Player().play(item=link, listitem=li)

def openRtmp(params):
    anurl = params['anurl']
    thumb = params['thumb']
    title = params['title']
    pos = anurl.find('rtmp')
    if pos == 0:
       anurl = anurl + ' live=true'
       playMedia(title, thumb, anurl)
    else:
       addon = xbmcaddon.Addon()
       path = addon.getAddonInfo('path')
       playlistfilename = path + '//' + title + '.m3u'
       file = open(playlistfilename, 'w')
       file.write('#EXTM3U' + '\n')
       if anurl.endswith('.ts'):
          file.write('#EXT-X-TARGETDURATION:600' + '\n')
          file.write('#EXT-X-ALLOW-CACHE:YES' + '\n')
          file.write('#EXT-X-VERSION:3' + '\n')
          file.write('#EXT-X-MEDIA-SEQUENCE:600' + '\n')
          for num in range(1, 100):
              file.write('#EXTINF:-1,' + title + '\n')
              file.write(anurl + '\n')
       else:
           file.write('#EXTINF:-1,' + title + '\n')
           file.write(anurl + '\n')
       file.close()
       playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
       playlist.clear()
       playlist.add(playlistfilename)
       xbmc.Player().play(playlist)
       os.remove(playlistfilename)

def buildmenu():
  xbmcplugin.addSortMethod(handle=int(sys.argv[1]), sortMethod=xbmcplugin.SORT_METHOD_LABEL)
  lines = xxx_tv_playlist_text.splitlines()
  for val in lines:
      pair = val.split('=')
      if len(pair) > 1:
         title = pair[0]
         anurl = pair[1]
         thumb = 'DefaultVideo.png'
         title = title.replace("_", " ")
         title = title.upper()
         title = title.strip()
         url = anurl
         url = url.replace("rtmp://$OPT:rtmp-raw=", "")
         url = url.replace("//", "/")
         url = url.replace(":/", "://")
         url = url.replace(chr(0x0D), "")
         url = url.replace(chr(0x0A), "")
         params = {'play':1}
         params['title'] = title
         params['thumb'] = 'DefaultVideo.png'
         params['anurl'] = url
         link = makeLink(params)
         addMenuItem(params['title'], link, params['thumb'], '', False)
  endListing()

parameters = parseParameters()
if 'play' in parameters:
   openRtmp(parameters)
else:
   buildmenu()

Sorry for my bad English and long message. Best regards.
Reply

Logout Mark Read Team Forum Stats Members Help
Playing .ts streams from m3u without often reloading.0