Skip to content
This repository was archived by the owner on Feb 10, 2023. It is now read-only.

internal/cmd/cmd.go: Synchronize streams to end of first silence #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,29 @@ func (s *Store) GetCommand(multi ChannelMatcher) Command {
}
}

func (s *Store) GetVideoOffset(url string) (string) {
var skip int

skip = 0;
for i := 10; i < 120; i += 20 {
ffmpeg := exec.Command("ffmpeg", "-ss", strconv.Itoa(skip), "-t", strconv.Itoa(i), "-i", url, "-map", "0:m:language:eng", "-af", "silencedetect=noise=-18dB:d=0.5", "-f","null","-")
out, err := ffmpeg.CombinedOutput()
if err != nil {
fmt.Fprintf(s.logger, "error detecting synchronization: %s\n", err)
return "0";
}

match := regexp.MustCompile(".* silence_end: (\\d+\\.\\d+)").FindStringSubmatch(string(out))
if match != nil {
return strings.Split(match[0], ": ")[1]
}
skip = i - 1;
}

fmt.Fprintf(s.logger, "error detecting synchronization: No silence_end found\n")
return "0";
}

func (s *Store) RunCommand(cc CommandContext) error {
url, err := cc.URL()
if err != nil {
Expand Down Expand Up @@ -184,6 +207,11 @@ func (s *Store) RunCommand(cc CommandContext) error {
s.logger.Error("failed to convert metadata to JSON:", err)
}
for i := range tmpCommand {
if (strings.Contains(tmpCommand[i], "$seek")) {
offset := s.GetVideoOffset(url)
tmpCommand[i] = strings.ReplaceAll(tmpCommand[i], "$seek", offset);
}

tmpCommand[i] = strings.ReplaceAll(tmpCommand[i], "$url", url)
tmpCommand[i] = strings.ReplaceAll(tmpCommand[i], "$json", string(metadataJson))
tmpCommand[i] = strings.ReplaceAll(tmpCommand[i], "$session", cc.MetaData.Session)
Expand Down