Improve proxied mp4 caching

This commit is contained in:
Zed 2023-05-21 00:47:09 +02:00
parent 93208908e6
commit 401aa26464
3 changed files with 15 additions and 10 deletions

View file

@ -84,8 +84,8 @@ proc parseVideo(js: JsonNode): Video =
views: js{"ext", "mediaStats", "r", "ok", "viewCount"}.getStr($js{"mediaStats", "viewCount"}.getInt),
available: js{"ext_media_availability", "status"}.getStr.toLowerAscii == "available",
title: js{"ext_alt_text"}.getStr,
durationMs: js{"video_info", "duration_millis"}.getInt
# playbackType: mp4
durationMs: js{"video_info", "duration_millis"}.getInt,
playbackType: m3u8
)
with title, js{"additional_media_info", "title"}:
@ -99,6 +99,9 @@ proc parseVideo(js: JsonNode): Video =
contentType = parseEnum[VideoType](v{"content_type"}.getStr("summary"))
url = v{"url"}.getStr
if contentType == mp4:
result.playbackType = mp4
result.variants.add VideoVariant(
contentType: contentType,
bitrate: v{"bitrate"}.getInt,

View file

@ -13,7 +13,7 @@ export httpclient, os, strutils, asyncstreams, base64, re
const
m3u8Mime* = "application/vnd.apple.mpegurl"
mp4Mime* = "video/mp4"
maxAge* = "max-age=604800"
maxAge* = "public, max-age=604800, must-revalidate"
proc safeFetch*(url: string): Future[string] {.async.} =
let client = newAsyncHttpClient()
@ -61,9 +61,12 @@ proc proxyMedia*(req: jester.Request; url: string): Future[HttpCode] {.async.} =
return Http404
var headers = @{
"Accept-Ranges": "bytes",
"Content-Type": res.headers["content-type", 0],
"Cache-Control": maxAge
"accept-ranges": "bytes",
"content-type": $res.headers.getOrDefault("content-type"),
"cache-control": maxAge,
"age": $res.headers.getOrDefault("age"),
"date": $res.headers.getOrDefault("date"),
"last-modified": $res.headers.getOrDefault("last-modified")
}
var tries = 0
@ -74,10 +77,10 @@ proc proxyMedia*(req: jester.Request; url: string): Future[HttpCode] {.async.} =
let contentLength = res.getContentLength
if contentLength.len > 0:
headers.add ("Content-Length", contentLength)
headers.add ("content-length", contentLength)
if res.headers.hasKey("content-range"):
headers.add ("Content-Range", $res.headers.getOrDefault("content-range"))
headers.add ("content-range", $res.headers.getOrDefault("content-range"))
respond(request, Http206, headers)
else:
respond(request, Http200, headers)

View file

@ -101,8 +101,7 @@ proc renderVideo*(video: Video; prefs: Prefs; path: string): VNode =
else: vidUrl
case playbackType
of mp4:
video(poster=thumb, controls="", muted=prefs.muteVideos):
source(src=source, `type`="video/mp4")
video(src=source, poster=thumb, controls="", muted=prefs.muteVideos, preload="metadata")
of m3u8, vmap:
video(poster=thumb, data-url=source, data-autoload="false", muted=prefs.muteVideos)
verbatim "<div class=\"video-overlay\" onclick=\"playVideo(this)\">"