Help me finish my plugin! :)

DRNU.bundle.zip (19 KB)



Despite being an absolute amateur, I’ve been tring to code a small plugin for the Danish public TV station, DR. They have a nice VOD service complete with an entire API (www.dr.dk/NU/api)



I’ve managed to do a semi-working plugin, but I’m stuck when it comes to getting the videos to play.



I’ll appreciate it if someone could take a look at the attached bundle.



Basically, I end up with a list of episodes for each TV program in a series of programs. For each episode the API returns a json formatted list of different videos for the same episode (different qualities). As a start, I just would like to pick the video with the best quality and play it (rtmp).



This is an example of the list of videos:



{<br />
  "resourceId": 862927,<br />
  "name": "21 SØNDAG",<br />
  "mediaType": "Video",<br />
  "restrictedToDenmark": false,<br />
  "downloadable": false,<br />
  "geofilterId": 0,<br />
  "publish": null,<br />
  "expire": "2011-03-01T20:00:00",<br />
  "links": [<br />
    {<br />
      "qualityId": 1975961,<br />
      "uri": "rtmp:\/\/vod.dr.dk\/cms\/mp4:CMS\/Resources\/dr.dk\/NETTV\/DR1\/2011\/01\/48f34ed3-a8f6-439f-9585-0b6053ade7ed\/2af19f43ed5b429f8285153f90a7533a_1000.mp4?ID=862927",<br />
      "linkType": "Streaming",<br />
      "fileType": "mp4",<br />
      "bitrateKbps": 1935,<br />
      "width": 640,<br />
      "height": 360<br />
    },<br />
    {<br />
      "qualityId": 1975962,<br />
      "uri": "mms:\/\/wms.dr.dk\/storage\/auto\/cms\/Resources\/dr.dk\/NETTV\/DR1\/2011\/01\/48f34ed3-a8f6-439f-9585-0b6053ade7ed\/2af19f43ed5b429f8285153f90a7533a_3wmv.wmv?ID=862927",<br />
      "linkType": "Streaming",<br />
      "fileType": "wmv",<br />
      "bitrateKbps": 840,<br />
      "width": 512,<br />
      "height": 288<br />
    },<br />
    {<br />
      "qualityId": 1975963,<br />
      "uri": "rtmp:\/\/vod.dr.dk\/cms\/mp4:CMS\/Resources\/dr.dk\/NETTV\/DR1\/2011\/01\/48f34ed3-a8f6-439f-9585-0b6053ade7ed\/2af19f43ed5b429f8285153f90a7533a_500.mp4?ID=862927",<br />
      "linkType": "Streaming",<br />
      "fileType": "mp4",<br />
      "bitrateKbps": 1310,<br />
      "width": 640,<br />
      "height": 360<br />
    },<br />
    {<br />
      "qualityId": 1975964,<br />
      "uri": "rtmp:\/\/vod.dr.dk\/cms\/mp4:CMS\/Resources\/dr.dk\/NETTV\/DR1\/2011\/01\/48f34ed3-a8f6-439f-9585-0b6053ade7ed\/2af19f43ed5b429f8285153f90a7533a_250.mp4?ID=862927",<br />
      "linkType": "Streaming",<br />
      "fileType": "mp4",<br />
      "bitrateKbps": 1186,<br />
      "width": 320,<br />
      "height": 180<br />
    }<br />
  ],<br />
  "chapters": [<br />
    <br />
  ]<br />
}



Any help is appreciated! Thanks! :)

Not too hard is it :slight_smile:



Have you read the wiki page about using rmtp and getting them into Plex, it’s your best resource: http://wiki.plexapp.com/index.php/Online_videoplayers



Note that rmtp can be a little tricky. Some work, some don’t, and if they have authentication they probably won’t. You’ll have to drop back to flash at that point I’m afraid.



Jonny

Thanks a lot for the quick reply - appreciate it!



I had not read the wiki, but had collected some similar info by looking at other plugins. But better to have it in that well-written format :slight_smile:



I think I’ll be able to do that part but first I need some code to pick the right video url in the json list of available videos (the example list I included in the post). That part I’m too unexperienced with coding to do myself, sadly… :stuck_out_tongue: (I guess that this has noting to do with Plex per se but with Python skills, which I’m also lacking).



:slight_smile:

Have a look at the IMDB trailers plugin. It uses JSON to get the meta-data. That should show you how to parse that content and extract what you want.

Thanks, I had a look (great plugin, BTW) but although it did inspire me in some ways, I’m still not sure how to pick one of the several possible video urls in the above example…



To cut it short: Could you or someone else please provide me with the Python code needed to pick the video url (“uri”) with the highest bitrate in the above example… I’m really lost here :wink:

I’m at work so don’t have time to actually write you the code but I can tell you what I’d do. That, plus some python documentation, should get you there.


  1. Create a dict.
  2. Loop through the 'link’s list obtained from the JSON object
  3. For each entry add an element to the dict with key = ‘bitrateKbps’ and value = ‘uri’
  4. After the loop put the dict keyset into a list and sort that list.
  5. The last element will then be the highest bit rate value and can be used as a key to get the URI from the dict.



    Make sense?



Ahh, not really at this point. But I'll have a look at it in the coming days. If you - or others - feel like it (and have the time), I'll appreciate actual code to build upon :)

Thanks again so far!



Not exactly what Jonny was explaining to do, but should be comparable:



<br />
videourl = [0,'feed']                  #initialize videourl<br />
for item in content['links']:<br />
    feed = item['uri']<br />
    bitrate = item['bitrateKbps']<br />
    if bitrate > videourl[0]:   		#find the highest "bitrate" and set it's "uri" to link<br />
        videourl[0] = bitrate<br />
        videourl[1] = feed       		<br />
    link = videourl[1]

I haven’t tried this but again, should hopefully be enough to get you going



<br />
content = JSON.ObjectFromURL(contentUrl)<br />
map = dict()<br />
for video in content['links']:<br />
    bitrate = int(video['bitrateKbps'])<br />
    uri = video['uri']<br />
    map[bitrate] = uri<br />
bestUri = map[sorted(map)[-1]]<br />
<br />




Jonny

OK, thanks a lot guys! I take your code and work on it :smiley:

I think I’ve managed to get to the stage where the right video is now chosen and put into the RTMPVideoItem function. But now Plex states that it cannot determine the input format. The error code in Plex.log:



ERROR: Open - error probing input format, http://127.0.0.1:32400/video/drnu/:/function/GetVideos?function_args=Y2VyZWFsMQozCmRpY3QKZGljdApQTVMuT2JqZWN0cy5JdGVtSW5mb1JlY29yZAoyCnM1MgpodHRwOi8vd3d3LmRyLmRrL2hhbmRsZXJzL0dldFJlc291cmNlLmFzaHg%40aWQ9ODAzMzQ0czIKaWRyMgpzNgpzZW5kZXI0CnMyMApBbWlnbzogMTcuIG9rdC4gMjAxMHM5Cml0ZW1UaXRsZXM1CkRSIE5VczYKdGl0bGUxczEzClByb2dyYW1zZXJpZXJzNgp0aXRsZTJzMzkKL3ZpZGVvL2RybnUvOi9yZXNvdXJjZXMvYXJ0LWRlZmF1bHQucG5nczMKYXJ0cjEKcjAK<br />
13:23:50 T:2962608128 M:5192839168   ERROR: OpenDemuxStream - Error creating demuxer




When I use the elements (url, clip) in the www.plexapp.com/player player, it plays nicely, fx:

[http://www.plexapp.c...0.mp4?ID=861360](http://www.plexapp.com/player/player.php?url=rtmp://vod.dr.dk/cms&clip=mp4:CMS/Resources/dr.dk/NETTV/DR2/2011/01/973311ca-cac4-4f78-bfa7-5d3d52bcdbd6/fb9e4923b30a471e93b09b57440a22d9_1000.mp4?ID=861360)

I attach the updated bundle. As always, I'll appreciate it if you, Jonny, or someone else could take a look!

BTW, I just realized that this plugin uses framework version 1 - is there any major advantages to be gained if I upgrade to version 2?

FYI- I haven’t tested any of this either…

And I’m definitely not as good at explaining as Jonny, but I hope this makes sense.



but I made some changes:


def ProgramMenu(sender,id):<br />
    dir=MediaContainer(viewGroup="List", title1="DR NU", title2="Programserier")<br />
	JSONObject = JSON.ObjectFromURL(APIURL % "programseries/" + id + "/videos")<br />
	for item in JSONObject:<br />
		key=APIURL % "videos/" + str(item["id"])<br />
		thumb=APIURL % "videos/" + str(item["id"]) + "/images/600x600.jpg"<br />
		title=item["title"] + ": " + item["formattedBroadcastTime"]<br />
		subtitle=item["description"]<br />
		dir.Append(Function(VideoItem(GetVideos, title=title,subtitle=subtitle, thumb=thumb), id=item["videoResourceUrl"])) 	#CHANGED<br />
	return dir<br />
<br />
def GetVideos(sender,id):<br />
	content = JSON.ObjectFromURL(id)<br />
	map = dict()<br />
	for video in content["links"]:<br />
		quality=int(video["qualityId"])<br />
		uri = video["uri"]<br />
		map[quality] = uri<br />
	bestUri = map[sorted(map)[0]]<br />
	tempclip = bestUri.split(":")         			#CHANGED<br />
	clip = 'mp4:' + tempclip[1]           			#CHANGED<br />
	player = tempclip[0].replace('/mp4', '')       	#CHANGED<br />
    return Redirect(RTMPVideoItem(player, clip)) 	#CHANGED

<br />

I think you were trying to use the Redirect object here:
[http://dev.plexapp.c...direct#Redirect](http://dev.plexapp.com/docs/Objects.html?highlight=redirect#Redirect)

Thank you so much, both of you!



Now I have it working, at least in terms of core functionality.



Thanks again for the nice advice and quick replies! Underscores what a great community this is! :smiley:

One thing I noticed. Won’t


<br />
bestUri = map[sorted(map)[0]]<br />



give you the worst (ie smallest) bit rate? Index -1 into the sorted list should give you the last element with the highest bitrate.

Unless I misunderstand how sorted works.

Jonny

Yeah, but I switched from using bitrate to the qualityId number, the smallest of which always refers to the best quality (the highest bitrate sometimes was a mms stream).

Thanks!

Ah, missed that - see, you know more than me know :slight_smile:


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.