Skip to content

Instantly share code, notes, and snippets.

@flavioribeiro
Created May 3, 2017 23:35
Show Gist options
  • Select an option

  • Save flavioribeiro/45aa88faea62efbd8345af6b0ae3a3e7 to your computer and use it in GitHub Desktop.

Select an option

Save flavioribeiro/45aa88faea62efbd8345af6b0ae3a3e7 to your computer and use it in GitHub Desktop.
location ~ ^/video-media/hlsfmp4/.+$ {
content_by_lua_block {
require "string";
function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
ngx.header["Access-Control-Allow-Headers"] = '*';
ngx.header["Access-Control-Allow-Origin"] = '*';
ngx.header["Access-Control-Allow-Methods"] = 'GET, HEAD, OPTIONS';
hls_manifest = ngx.var.request_uri:gsub("fmp4", "")
hls_content = ngx.location.capture(hls_manifest)
if hls_content.status ~= ngx.HTTP_OK then
ngx.print("failed requesting " .. hls_manifest)
ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)
end
init_segment_header = "#EXT-X-PLAYLIST-TYPE:VOD\n#EXT-X-MAP:URI="
init_segment_uri = "http://" .. ngx.var.host .. ngx.var.request_uri:gsub("hlsfmp4","dash"):gsub("index","init")
if string.match(ngx.var.request_uri, "index") then
if string.match(ngx.var.request_uri, "index%-v1") then
init_segment_uri, _ = init_segment_uri:gsub("a1.m3u8","x3.mp4")
res, _ = hls_content.body:gsub("EXT-%-X%-PLAYLIST%-TYPE:VOD", init_segment_header .. init_segment_uri):gsub("-a1.ts","-x3.m4s"):gsub("hls", "dash")
elseif string.match(ngx.var.request_uri, "index%-a1") then
init_segment_uri, _ = init_segment_uri:gsub("m3u8","mp4")
res, _ = hls_content.body:gsub("EXT-%-X%-PLAYLIST%-TYPE:VOD", init_segment_header .. init_segment_uri):gsub(".ts","-x3.m4s"):gsub("hls", "dash")
end
ngx.print(res)
else
res, _ = hls_content.body:gsub("hls", "hlsfmp4")
ngx.say(res)
uri_splitted = split(ngx.var.request_uri, ",")
audio_track_uri = "http://" .. ngx.var.host ..uri_splitted[1] .. uri_splitted[2] .. "/index-a1-x3.m3u8"
audio_track ='#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="aac",LANGUAGE="en",NAME="English",DEFAULT=YES,AUTOSELECT=YES,URI="' .. audio_track_uri .. '"\n'
ngx.print(audio_track)
end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment