Updated scripts, add HTTP server to serve compressed ASS

This commit is contained in:
DataHoarder 2024-03-08 18:20:07 +01:00
parent 232062ce19
commit cb9833927d
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
4 changed files with 179 additions and 1 deletions

124
http/server.go Normal file
View file

@ -0,0 +1,124 @@
package main
import (
"flag"
"fmt"
"net/http"
"os"
"path"
"slices"
"strconv"
"strings"
"time"
)
func main() {
servePath := flag.String("path", ".", "Path to serve")
flag.Parse()
dirList, err := os.ReadDir(*servePath)
if err != nil {
panic(err)
}
files := make(map[string]string)
for _, e := range dirList {
if !e.IsDir() {
if path.Ext(e.Name()) == ".swf" {
files["/"+e.Name()] = path.Join(*servePath, e.Name())
files["/"+e.Name()+".swf2ass.mkv"] = path.Join(*servePath, e.Name()+".swf2ass.mkv")
files["/"+e.Name()+".swf2ass.mkv.zstd"] = path.Join(*servePath, e.Name()+".swf2ass.mkv.zstd")
files["/"+e.Name()+".swf2ass.mkv.br"] = path.Join(*servePath, e.Name()+".swf2ass.mkv.br")
files["/"+e.Name()+".swf2ass.mkv.gzip"] = path.Join(*servePath, e.Name()+".swf2ass.mkv.gzip")
}
}
}
server := &http.Server{
Addr: "0.0.0.0:8008",
ReadTimeout: time.Second * 2,
Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path == "/" {
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
var output string
output += "<html><body><table><tr><th align=\"right\">NAME</th><th>Size</th><th>Links</th></tr>"
for _, e := range dirList {
if path.Ext(e.Name()) == ".swf" {
stat, err := os.Stat(path.Join(*servePath, e.Name()+".swf2ass.mkv"))
if err != nil {
continue
}
output += fmt.Sprintf("<tr><th align=\"right\">%s</th><td>%d MiB</td><td><a href=\"/%s.swf2ass.mkv\">MKV</a> <a href=\"/%s.swf2ass.mkv.zstd\">Zstandard</a> <a href=\"/%s.swf2ass.mkv.br\">Brotli</a> <a href=\"/%s.swf2ass.mkv.gzip\">gzip</a> <a href=\"/%s\">SWF</a></td></tr>", e.Name(), stat.Size()/(1024*1024), e.Name(), e.Name(), e.Name(), e.Name(), e.Name())
}
}
output += "</table></body></html>"
writer.Header().Set("Content-Length", strconv.FormatUint(uint64(len([]byte(output))), 10))
writer.WriteHeader(http.StatusOK)
writer.Write([]byte(output))
return
}
if fpath, ok := files[request.URL.Path]; !ok {
writer.WriteHeader(http.StatusNotFound)
return
} else {
encodings := strings.Split(request.Header.Get("Accept-Encoding"), ",")
for i := range encodings {
//drop preference
e := strings.Split(encodings[i], ";")
encodings[i] = strings.TrimSpace(e[0])
}
writer.Header().Set("Content-Type", "application/octet-stream")
if path.Ext(request.URL.Path) == ".mkv" {
writer.Header().Set("Content-Type", "video/x-matroska")
ua := request.UserAgent()
//some players allow decoding gzip even if it's not on accept-encoding
// || strings.Contains(ua, "LibVLC/")
if (strings.Contains(ua, "libmpv") || strings.Contains(ua, "Lavf/")) && !slices.Contains(encodings, "gzip") {
encodings = append(encodings, "gzip")
}
if slices.Contains(encodings, "zstd") {
writer.Header().Set("Content-Encoding", "zstd")
fpath += ".zstd"
request.Header.Del("Range")
} else if slices.Contains(encodings, "br") {
writer.Header().Set("Content-Encoding", "br")
fpath += ".br"
request.Header.Del("Range")
} else if slices.Contains(encodings, "gzip") {
writer.Header().Set("Content-Encoding", "gzip")
fpath += ".gz"
request.Header.Del("Range")
} else {
//serve line-compressed mkv instead
fpath = strings.TrimSuffix(fpath, ".mkv") + ".zlib.mkv"
}
}
stat, err := os.Stat(fpath)
if err != nil {
panic(err)
}
f, err := os.Open(fpath)
if err != nil {
panic(err)
}
defer f.Close()
writer.Header().Set("Content-Length", strconv.FormatUint(uint64(stat.Size()), 10))
http.ServeContent(writer, request, path.Base(request.URL.Path), stat.ModTime(), f)
}
}),
}
err = server.ListenAndServe()
if err != nil {
panic(err)
}
}

27
swf2ass-pack.sh Executable file
View file

@ -0,0 +1,27 @@
#!/bin/bash
INPUT="${1}"
OUTPUT="${INPUT}.swf2ass.mkv"
OUTPUT_ZLIB="${INPUT}.swf2ass.zlib.mkv"
OUTPUT_CLEAN="${INPUT}.swf2ass.clean.mkv"
./swf2ass-zlib.sh "${INPUT}"
if [[ ! -f "${OUTPUT}" ]]; then
echo "Output not found!"
exit 1
fi
#mkclean --remux --keep-cues --live "${OUTPUT}" "${OUTPUT_CLEAN}"
mkclean --remux --keep-cues "${OUTPUT}" "${OUTPUT_CLEAN}"
rm -v "${OUTPUT}"
mv -v "${OUTPUT_CLEAN}" "${OUTPUT}"
zstd -f -k -19 -T0 "${OUTPUT}" -o "${OUTPUT}.zst"
brotli --keep --no-copy-stat --best --verbose --lgwin=24 --stdout "${OUTPUT}" > "${OUTPUT}.br"
#gzip --stdout --no-name --best "${OUTPUT}" > "${OUTPUT}.gz"
zopfli --gzip --i10 -c "${OUTPUT}" > "${OUTPUT}.gz"

27
swf2ass-zlib.sh Executable file
View file

@ -0,0 +1,27 @@
#!/bin/bash
INPUT="${1}"
go run git.gammaspectra.live/WeebDataHoarder/swf2ass-go -input "${INPUT}" -output "${INPUT}.ass" -audio "${INPUT}.mp3"
ffmpeg -y \
-f lavfi -i "color=size=$(grep PlayResX ${INPUT}.ass | head -n 1 | awk '{ print $2 }')x$(grep PlayResY ${INPUT}.ass | head -n 1 | awk '{ print $2 }'):rate=$(grep '?dummy' ${INPUT}.ass | head -n 1 | awk -F: '{ print $3 }'):color=black" \
-i "${INPUT}.mp3" \
-map 0:v -map 1:a \
-vf "drawtext=box=1:boxcolor=black@0.5:boxborderw=20:x=(w-text_w)/2:y=(h-text_h)/2:fontcolor=white:fontsize=32:text='Enable ASS subtitles'" \
-c:v libx264 -pix_fmt yuv420p -crf 1 -tune stillimage -preset slow -x264-params keyint=240 \
-c:a copy \
-shortest "${INPUT}.video.mkv"
mkvmerge --title "${INPUT}" -o "${INPUT}.swf2ass.mkv" --clusters-in-meta-seek \
--attachment-mime-type "application/x-shockwave-flash" --attachment-description "SWF Source" --attach-file "${INPUT}" \
--language 0:zxx --track-name 0:"bogus video" "${INPUT}.video.mkv" \
--cues 0:all --forced-track 0:1 --default-track 0:1 --compression 0:none --language 0:zxx --track-name 0:"Vector from ${INPUT}" "${INPUT}.ass"
mkvmerge --title "${INPUT}" -o "${INPUT}.swf2ass.zlib.mkv" --clusters-in-meta-seek \
--attachment-mime-type "application/x-shockwave-flash" --attachment-description "SWF Source" --attach-file "${INPUT}" \
--language 0:zxx --track-name 0:"bogus video" "${INPUT}.video.mkv" \
--cues 0:all --forced-track 0:1 --default-track 0:1 --compression 0:zlib --language 0:zxx --track-name 0:"Vector from ${INPUT}" "${INPUT}.ass"
rm "${INPUT}.ass" "${INPUT}.video.mkv" "${INPUT}.mp3"

View file

@ -16,7 +16,7 @@ ffmpeg -y \
mkvmerge --title "${INPUT}" -o "${INPUT}.swf2ass.mkv" --clusters-in-meta-seek \
--attachment-mime-type "application/x-shockwave-flash" --attachment-description "SWF Source" --attach-file "${INPUT}" \
--language 0:zxx --track-name 0:"bogus video" "${INPUT}.video.mkv" \
--cues 0:all --forced-track 0:1 --default-track 0:1 --compression 0:zlib --language 0:zxx --track-name 0:"Vector from ${INPUT}" "${INPUT}.ass"
--cues 0:all --forced-track 0:1 --default-track 0:1 --compression 0:none --language 0:zxx --track-name 0:"Vector from ${INPUT}" "${INPUT}.ass"
rm "${INPUT}.ass" "${INPUT}.video.mkv" "${INPUT}.mp3"