Skip to content

Commit

Permalink
image api
Browse files Browse the repository at this point in the history
  • Loading branch information
mjh1 committed Nov 7, 2023
1 parent 39daca5 commit 28f66c9
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
6 changes: 6 additions & 0 deletions api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ func NewCatalystAPIRouter(cli config.Cli, vodEngine *pipeline.Coordinator, bal b
for _, path := range [...]string{"/asset/hls/:playbackID/*file", "/webrtc/:playbackID"} {
router.OPTIONS(path, withLogging(withCORS(handlers.PreflightOptionsHandler())))
}
image := middleware.LogAndMetrics(metrics.Metrics.ImageRequestDurationSec)(
withCORS(
handlers.NewImageHandler(cli.PublicBucketURLs).Handle,
),
)
router.GET("/image/hls/:playbackID/*secs", image)

// Handling incoming playback redirection requests
redirectHandler := withLogging(withCORS(geoHandlers.RedirectHandler()))
Expand Down
1 change: 1 addition & 0 deletions config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Cli struct {
APIServer string
SourceOutput string
PrivateBucketURLs []*url.URL
PublicBucketURLs []*url.URL
ExternalTranscoder string
VodPipelineStrategy string
MetricsDBConnectionString string
Expand Down
84 changes: 84 additions & 0 deletions handlers/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package handlers

import (
"github.com/julienschmidt/httprouter"
ffmpeg "github.com/u2takey/ffmpeg-go"
"log"
"net/http"
"net/url"
"os"
"path"
)

type ImageHandler struct {
PublicBucketURLs []*url.URL
}

func NewImageHandler(urls []*url.URL) *ImageHandler {
return &ImageHandler{
PublicBucketURLs: urls,
}

Check warning on line 20 in handlers/image.go

View check run for this annotation

Codecov / codecov/patch

handlers/image.go#L17-L20

Added lines #L17 - L20 were not covered by tests
}

func (p *ImageHandler) Handle(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
// ffmpeg -skip_frame nokey -i ~/BigBuckBunny.mp4 -ss 00:07:50 -vframes 1 -vf 'scale=320:240:force_original_aspect_ratio=decrease' -y keyframe.jpg

//playbackID := params.ByName("playbackID")
//file, err := playback.OsFetch(p.PublicBucketURLs, playbackID, "video", "")
//if err != nil {
// log.Println(err)
// return
//}
//defer file.Body.Close()

tmpDir, err := os.MkdirTemp(os.TempDir(), "image-api-*")
if err != nil {
log.Println(err)
return
//return fmt.Errorf("temp file creation failed: %w", err)
}
defer os.RemoveAll(tmpDir)

//fileBytes, err := io.ReadAll(file.Body)
//if err != nil {
// log.Println(err)
// return
//}
//inputFile := path.Join(tmpDir, "video.mp4")
outputFile := path.Join(tmpDir, "out.jpg")
//err = os.WriteFile(inputFile, fileBytes, 0644)
//if err != nil {
// log.Println(err)
// return
//}
inputFile := "https://link.storjshare.io/raw/jw55734peq3f3qdqxkd5ggi66l3a/catalyst-vod-monster/hls/6ff92coiaa1zctw9/index.m3u8"

log.Println("start ffmpeg")
err = ffmpeg.Input(inputFile, ffmpeg.KwArgs{
"skip_frame": "nokey",
}).
Output(
outputFile,
ffmpeg.KwArgs{
"ss": "00:00:10",
"vframes": "1",
"vf": "scale=320:240:force_original_aspect_ratio=decrease",
},
).OverWriteOutput().ErrorToStdOut().Run()
log.Println("ffmpeg finished")
if err != nil {
log.Println(err)
return
// TODO
}

Check warning on line 73 in handlers/image.go

View check run for this annotation

Codecov / codecov/patch

handlers/image.go#L23-L73

Added lines #L23 - L73 were not covered by tests

bs, err := os.ReadFile(outputFile)
if err != nil {
log.Println(err)
}

Check warning on line 78 in handlers/image.go

View check run for this annotation

Codecov / codecov/patch

handlers/image.go#L75-L78

Added lines #L75 - L78 were not covered by tests

log.Println("got here")

w.WriteHeader(http.StatusOK)
w.Write(bs)

Check warning on line 83 in handlers/image.go

View check run for this annotation

Codecov / codecov/patch

handlers/image.go#L80-L83

Added lines #L80 - L83 were not covered by tests
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func main() {
fs.StringVar(&cli.APIToken, "api-token", "IAmAuthorized", "Auth header value for API access")
fs.StringVar(&cli.SourceOutput, "source-output", "", "URL for the video source segments used if source_segments is not defined in the upload request")
config.URLSliceVarFlag(fs, &cli.PrivateBucketURLs, "private-bucket", "", "URL for the private media bucket")
config.URLSliceVarFlag(fs, &cli.PublicBucketURLs, "public-bucket", "", "URL for the public media bucket")
fs.StringVar(&cli.ExternalTranscoder, "external-transcoder", "", "URL for the external transcoder to be used by the pipeline coordinator. Only 1 implementation today for AWS MediaConvert which should be in the format: mediaconvert://key-id:key-secret@endpoint-host?region=aws-region&role=iam-role&s3_aux_bucket=s3://bucket")
fs.StringVar(&cli.VodPipelineStrategy, "vod-pipeline-strategy", string(pipeline.StrategyCatalystFfmpegDominance), "Which strategy to use for the VOD pipeline")
fs.StringVar(&cli.MetricsDBConnectionString, "metrics-db-connection-string", "", "Connection string to use for the metrics Postgres DB. Takes the form: host=X port=X user=X password=X dbname=X")
Expand Down
1 change: 1 addition & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type CatalystAPIMetrics struct {
UploadVODRequestDurationSec *prometheus.SummaryVec
TranscodeSegmentDurationSec prometheus.Histogram
PlaybackRequestDurationSec *prometheus.SummaryVec
ImageRequestDurationSec *prometheus.SummaryVec
CDNRedirectCount *prometheus.CounterVec
CDNRedirectWebRTC406 *prometheus.CounterVec

Expand Down
4 changes: 2 additions & 2 deletions playback/playback.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Response struct {
}

func Handle(buckets []*url.URL, req Request) (*Response, error) {
f, err := osFetch(buckets, req.PlaybackID, req.File, req.Range)
f, err := OsFetch(buckets, req.PlaybackID, req.File, req.Range)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func appendAccessKey(uri, gatingParam, gatingParamName string) (string, error) {
return variantURI.String(), nil
}

func osFetch(buckets []*url.URL, playbackID, file, byteRange string) (*drivers.FileInfoReader, error) {
func OsFetch(buckets []*url.URL, playbackID, file, byteRange string) (*drivers.FileInfoReader, error) {
if len(buckets) < 1 {
return nil, errors.New("playback failed, no private buckets configured")
}
Expand Down

0 comments on commit 28f66c9

Please sign in to comment.