ok trnscode swp

This commit is contained in:
Olive Vaughn 2026-08-26 02:16:50 -04:00
parent 54668c0d93
commit e9766de877
12 changed files with 221 additions and 243 deletions

View file

@ -1,8 +1,11 @@
package api
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os/exec"
"strconv"
"strings"
@ -27,6 +30,7 @@ func NewRouter(lib *library.Library) http.Handler {
r.Get("/api/search", searchHandler(lib))
r.Get("/api/rescan", rescanHandler(lib))
r.Get("/api/stream/{id}", streamHandler(lib))
r.Get("/api/subtitles/{id}/{index}", subtitleHandler(lib))
return r
}
@ -101,12 +105,15 @@ func detailHandler(lib *library.Library) http.HandlerFunc {
probeResult, err := probe.Run(fullPath)
if err != nil {
writeJSON(w, http.StatusOK, map[string]any{
"item": item,
"probe": nil,
"item": item,
"probe": nil,
"probe_error": err.Error(),
})
return
}
for i := range probeResult.Subtitles {
probeResult.Subtitles[i].URL = fmt.Sprintf("/api/subtitles/%s/%d", id, probeResult.Subtitles[i].Index)
}
writeJSON(w, http.StatusOK, map[string]any{
"item": item,
@ -142,6 +149,75 @@ func streamHandler(lib *library.Library) http.HandlerFunc {
}
}
func subtitleHandler(lib *library.Library) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
index, err := strconv.Atoi(chi.URLParam(r, "index"))
if err != nil || index < 0 {
http.Error(w, "invalid subtitle index", http.StatusBadRequest)
return
}
_, fullPath, ok := lib.Get(id)
if !ok {
http.Error(w, "not found", http.StatusNotFound)
return
}
metadata, err := probe.Run(fullPath)
if err != nil {
http.Error(w, "unable to inspect media", http.StatusInternalServerError)
return
}
var subtitle *probe.SubTrack
for i := range metadata.Subtitles {
if metadata.Subtitles[i].Index == index {
subtitle = &metadata.Subtitles[i]
break
}
}
if subtitle == nil {
http.Error(w, "subtitle track not found", http.StatusNotFound)
return
}
if !isTextSubtitleCodec(subtitle.Codec) {
http.Error(w, fmt.Sprintf("subtitle codec %q requires OCR or burn-in", subtitle.Codec), http.StatusUnsupportedMediaType)
return
}
cmd := exec.CommandContext(r.Context(), "ffmpeg",
"-v", "error",
"-i", fullPath,
"-map", fmt.Sprintf("0:%d", index),
"-c:s", "webvtt",
"-f", "webvtt",
"pipe:1",
)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
http.Error(w, fmt.Sprintf("subtitle conversion failed: %s", strings.TrimSpace(stderr.String())), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/vtt; charset=utf-8")
w.Header().Set("Cache-Control", "public, max-age=86400")
_, _ = w.Write(stdout.Bytes())
}
}
func isTextSubtitleCodec(codec string) bool {
switch strings.ToLower(codec) {
case "ass", "ssa", "subrip", "srt", "text", "webvtt", "mov_text":
return true
default:
return false
}
}
func rescanHandler(lib *library.Library) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
go lib.Scan()

17
internal/api/api_test.go Normal file
View file

@ -0,0 +1,17 @@
package api
import "testing"
func TestIsTextSubtitleCodec(t *testing.T) {
for _, codec := range []string{"ass", "ssa", "subrip", "srt", "text", "webvtt", "mov_text", "SUBRIP"} {
if !isTextSubtitleCodec(codec) {
t.Errorf("expected %q to be treated as text", codec)
}
}
for _, codec := range []string{"hdmv_pgs_subtitle", "dvd_subtitle", "dvb_subtitle", ""} {
if isTextSubtitleCodec(codec) {
t.Errorf("expected %q to require bitmap handling", codec)
}
}
}

View file

@ -61,7 +61,7 @@ func (l *Library) Scan() error {
RelPath: f.RelPath,
Size: f.Size,
ModTime: f.ModTime,
StreamURL: "/vod/" + f.RelPath + "/index.m3u8",
StreamURL: "/vod/" + id + "/media/" + f.RelPath + "/index.m3u8",
fullPath: f.Path,
}
items = append(items, item)

View file

@ -8,13 +8,13 @@ import (
)
type ProbeResult struct {
Duration float64 `json:"duration"`
Width int `json:"width"`
Height int `json:"height"`
VideoCodec string `json:"video_codec"`
Bitrate int64 `json:"bitrate"`
Audio []AudioTrack `json:"audio_tracks"`
Subtitles []SubTrack `json:"subtitle_tracks"`
Duration float64 `json:"duration"`
Width int `json:"width"`
Height int `json:"height"`
VideoCodec string `json:"video_codec"`
Bitrate int64 `json:"bitrate"`
Audio []AudioTrack `json:"audio_tracks"`
Subtitles []SubTrack `json:"subtitle_tracks"`
}
type AudioTrack struct {
@ -28,6 +28,7 @@ type SubTrack struct {
Index int `json:"index"`
Codec string `json:"codec"`
Language string `json:"language"`
URL string `json:"url,omitempty"`
}
type ffprobeOutput struct {