Skip to content

Instantly share code, notes, and snippets.

@ChrisK2
Created November 10, 2014 18:28
Show Gist options
  • Select an option

  • Save ChrisK2/8701184fe3ea7701c9cc to your computer and use it in GitHub Desktop.

Select an option

Save ChrisK2/8701184fe3ea7701c9cc to your computer and use it in GitHub Desktop.

Revisions

  1. ChrisK2 created this gist Nov 10, 2014.
    64 changes: 64 additions & 0 deletions ls_hook.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    local utils = require 'mp.utils'
    local msg = require 'mp.msg'

    local ls = {
    path = "livestreamer",
    }

    mp.add_hook("on_load", 9, function ()

    local function exec(args)
    local ret = utils.subprocess({args = args})
    return ret.status, ret.stdout
    end

    local url = mp.get_property("stream-open-filename")

    if (url:find("http://www.twitch.tv") == 1) or (url:find("https://www.twitch.tv") == 1)
    then

    local es, json = exec({
    ls.path, "-j", "--stream-priority", "hls,rtmp,http", url, "best"
    })

    if (es < 0) or (json == nil) or (json == "") then
    msg.warn("livestreamer failed, trying to play URL directly ...")
    return
    end

    local json, err = utils.parse_json(json)

    if (json == nil) then
    msg.error("failed to parse JSON data: " .. err)
    return
    end

    msg.info("livestreamer succeeded!")

    local streamurl = ""

    if not (json.url == nil) then
    -- normal video
    streamurl = json.url
    else
    msg.error("No URL found in JSON data.")
    return
    end

    msg.debug("streamurl: " .. streamurl)

    mp.set_property("stream-open-filename", streamurl)

    -- original URL since livestreamer doesn't give us anything better
    mp.set_property("file-local-options/media-title", url)


    -- for rtmp
    --[[
    if not (json.play_path == nil) then
    mp.set_property("file-local-options/stream-lavf-o",
    "rtmp_tcurl=\""..streamurl..
    "\",rtmp_playpath=\""..json.play_path.."\"")
    end ]]--
    end
    end)