Skip to content

Commit

Permalink
feat: added middleware for dynamic-video, links to dynamic-video, vir…
Browse files Browse the repository at this point in the history
…tualized quote lists
  • Loading branch information
maxwiseman committed Aug 4, 2024
1 parent f778d52 commit ae41e3a
Show file tree
Hide file tree
Showing 16 changed files with 2,386 additions and 4,898 deletions.
1 change: 1 addition & 0 deletions apps/backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.mp4
*.mkv
sample_data
cache
29 changes: 19 additions & 10 deletions apps/backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,39 @@ import (
// "net/http"
)

const basePath = "/Volumes/NVME Drive/TV Shows/Top Gear (2002 - 2015)/"

func main() {
router := gin.Default()
router.GET("/quote/:sId/:epId/:qId", vidRequestHandler)
router.GET("/quote/*path", vidRequestHandler)
router.Run("localhost:8080")
}

func trimVideo(qId int) {
func trimVideo(qId int, qIdx int, filePath string) {
fmt.Println("Transcoding video...")
ffmpeg.Input("./sample_data/india.mkv").Output("./subs.srt").OverWriteOutput().Run()
ffmpeg.Input(basePath + filePath).Output("./subs.srt").OverWriteOutput().Run()
subs, _ := gosrt.ReadFile("./subs.srt")
sub := subs[qId]
ffmpeg.Input("./sample_data/india.mkv", ffmpeg.KwArgs{"ss": sub.Start.Seconds() - 30}).Output(strconv.Itoa(qId)+".mp4", ffmpeg.KwArgs{"t": (sub.End - sub.Start).Seconds() + 60, "preset": "ultrafast", "c:v": "libx264", "crf": "30"}).OverWriteOutput().Run()
sub := subs[qIdx]
ffmpeg.Input(basePath+filePath, ffmpeg.KwArgs{"ss": sub.Start.Seconds() - 30}).Output("./cache/"+strconv.Itoa(qId)+".mp4", ffmpeg.KwArgs{"t": (sub.End - sub.Start).Seconds() + 60, "preset": "ultrafast", "c:v": "libx264", "crf": "30"}).OverWriteOutput().Run()
}

func vidRequestHandler(ctx *gin.Context) {
quoteId, _ := strconv.Atoi(ctx.Param("qId"))
if ctx.Query("secret") != "thisisasecret" {
ctx.String(401, "401 - Not authorized")
return
}
filePath := ctx.Params.ByName("path")
println(filePath)
quoteIndex, _ := strconv.Atoi(ctx.Query("qIdx"))
quoteId, _ := strconv.Atoi(ctx.Query("qId"))
quoteId -= 1
fileName := strconv.Itoa(quoteId) + ".mp4"
if _, err := os.Stat("./" + fileName); errors.Is(err, os.ErrNotExist) {
trimVideo(quoteId)
cachedFileName := strconv.Itoa(quoteId) + ".mp4"
if _, err := os.Stat("./cache/" + cachedFileName); errors.Is(err, os.ErrNotExist) {
trimVideo(quoteId, quoteIndex, filePath)
}
ctx.Header("Content-Description", "File Transfer")
ctx.Header("Content-Transfer-Encoding", "binary")
ctx.Header("Content-Type", "video/mp4")
ctx.Header("Cache-Control", "public, max-age=3600")
ctx.File("./" + fileName)
ctx.File("./cache/" + cachedFileName)
}
Loading

0 comments on commit ae41e3a

Please sign in to comment.