Skip to content

Commit

Permalink
expose helper port to mapper in order to mediainfo to get the duration
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioribeiro committed Sep 16, 2019
1 parent 9912780 commit e21b77d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
5 changes: 2 additions & 3 deletions handlers/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
Expand All @@ -18,7 +17,6 @@ func Map(c Config, client *storage.Client) http.Handler {
logger := c.Logger()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
fmt.Println("URL:", r.URL)
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
Expand All @@ -37,7 +35,8 @@ func Map(c Config, client *storage.Client) http.Handler {
m, err := mapper.Map(r.Context(), vodmodule.MapOptions{
Prefix: prefix,
Filter: filter,
ProxyEndpoint: fmt.Sprintf("http://127.0.0.1%s%s", c.Listen, c.Proxy.Endpoint),
ProxyEndpoint: c.Proxy.Endpoint,
ProxyListen: c.Listen,
ChapterBreaks: chapterBreaks,
})
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions vodmodule/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type MapOptions struct {
// Optional and specific to cbsinteractive case
ChapterBreaks string

// Optional used to build url and fetch duration with mediainfo
ProxyListen string

// Optional regexp that is used to filter the list of objects.
Filter *regexp.Regexp
}
Expand All @@ -48,7 +51,7 @@ func (m *Mapper) Map(ctx context.Context, opts MapOptions) (Mapping, error) {
var err error
r := Mapping{}
if opts.ChapterBreaks != "" {
r.Durations, _ = m.chapterBreaksToDurations(ctx, opts.ChapterBreaks, opts.ProxyEndpoint, opts.Prefix)
r.Durations, _ = m.chapterBreaksToDurations(ctx, opts.ChapterBreaks, opts.ProxyListen, opts.ProxyEndpoint, opts.Prefix)
}
r.Sequences, err = m.getSequences(ctx, opts.Prefix, opts.Filter, opts.ProxyEndpoint, r.Durations)
return r, err
Expand Down Expand Up @@ -105,18 +108,16 @@ func (m *Mapper) getSequences(ctx context.Context, prefix string, filter *regexp
return nil, err
}

func (m *Mapper) chapterBreaksToDurations(ctx context.Context, chapterBreaks string, endpoint string, prefix string) ([]int, error) {
func (m *Mapper) chapterBreaksToDurations(ctx context.Context, chapterBreaks string, proxyListen string, endpoint string, prefix string) ([]int, error) {
var err error
var obj *storage.ObjectAttrs

previousTimestamp := 0
totalDurations := 0
splittedChapterBreaks := strings.Split(chapterBreaks, ",")
result := make([]int, 0) // is there something better than this?
for i := range splittedChapterBreaks {
chapterBreakInMs := m.convertChapterBreakInMs(splittedChapterBreaks[i])
result = append(result, chapterBreakInMs-previousTimestamp)
totalDurations = totalDurations + chapterBreakInMs
previousTimestamp = chapterBreakInMs
}

Expand All @@ -126,10 +127,10 @@ func (m *Mapper) chapterBreaksToDurations(ctx context.Context, chapterBreaks str
Delimiter: "/",
})
obj, _ = iter.Next() // ignoring error for now
fileURL := fmt.Sprintf("%s/%s", endpoint, obj.Name)
fileURL := fmt.Sprintf("http://127.0.0.1%s%s/%s", proxyListen, endpoint, obj.Name)
mi, _ := mediainfo.New(fileURL, logger, "sample_file")

result = append(result, int(mi.General.Duration.Val)-totalDurations) // last piece should have all the content
result = append(result, int(mi.General.Duration.Val)-previousTimestamp) // last piece should have all the content

return result, err
}
Expand Down

0 comments on commit e21b77d

Please sign in to comment.