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

View file

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

View file

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