Skip to content

Commit

Permalink
feat(withny): write thumbnail and dump MetaData
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkness4 committed Aug 20, 2024
1 parent f0327f8 commit f5d11fa
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/download/download_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ Available format options:
`,
Destination: &downloadParams.OutFormat,
},
&cli.BoolFlag{
Name: "write-chat",
Value: false,
Category: "Streaming:",
Usage: "Save live chat into a json file.",
Destination: &downloadParams.WriteChat,
},
&cli.BoolFlag{
Name: "write-metadata-json",
Value: false,
Category: "Streaming:",
Usage: "Dump output stream MetaData into a json file.",
Destination: &downloadParams.WriteMetaDataJSON,
},
&cli.BoolFlag{
Name: "write-thumbnail",
Value: false,
Category: "Streaming:",
Usage: "Download thumbnail into a file.",
Destination: &downloadParams.WriteThumbnail,
},
&cli.IntFlag{
Name: "max-packet-loss",
Value: 20,
Expand Down
6 changes: 6 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ defaultParams:
outFormat: '{{ .ChannelID }} {{ .ChannelName }}/{{ .Date }} {{ .Title }}.{{ .Ext }}'
## Allow a maximum of packet loss before aborting stream download. (default: 20)
packetLossMax: 20
## Save live chat into a json file. (default: false)
writeChat: false
## Dump output MetaData into a json file. (default: false)
writeMetaDataJson: false
## Download thumbnail into a file. (default: false)
writeThumbnail: false
## Wait until the broadcast goes live, then start recording. (default: true)
waitForLive: true
## How many seconds between checks to see if broadcast is live. (default: 5s)
Expand Down
54 changes: 54 additions & 0 deletions withny/channel_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
package withny

import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"os"
"strings"
"time"
Expand Down Expand Up @@ -149,6 +152,18 @@ func (w *ChannelWatcher) Process(ctx context.Context, meta api.MetaData) error {
log.Err(err).Msg("notify failed")
}

fnameInfo, err := PrepareFile(w.params.OutFormat, meta, w.params.Labels, "info.json")
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return err
}
fnameThumb, err := PrepareFile(w.params.OutFormat, meta, w.params.Labels, "png")
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return err
}
fnameStream, err := PrepareFile(w.params.OutFormat, meta, w.params.Labels, "ts")
if err != nil {
span.RecordError(err)
Expand Down Expand Up @@ -199,6 +214,45 @@ func (w *ChannelWatcher) Process(ctx context.Context, meta api.MetaData) error {
".combined.m4a",
)

if w.params.WriteMetaDataJSON {
w.log.Info().Str("fnameInfo", fnameInfo).Msg("writing info json")
func() {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(meta); err != nil {
w.log.Error().Err(err).Msg("failed to encode meta in info json")
return
}
if err := os.WriteFile(fnameInfo, buf.Bytes(), 0o755); err != nil {
w.log.Error().Err(err).Msg("failed to write meta in info json")
return
}
}()
}

if w.params.WriteThumbnail {
w.log.Info().Str("fnameThumb", fnameThumb).Msg("writing thunnail")
func() {
url := meta.Stream.ThumbnailURL
resp, err := w.Get(url)
if err != nil {
w.log.Error().Err(err).Msg("failed to fetch thumbnail")
return
}
defer resp.Body.Close()
out, err := os.Create(fnameThumb)
if err != nil {
w.log.Error().Err(err).Msg("failed to open thumbnail file")
return
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
w.log.Error().Err(err).Msg("failed to download thumbnail file")
return
}
}()
}

span.AddEvent("downloading")
state.DefaultState.SetChannelState(
w.channelID,
Expand Down
21 changes: 21 additions & 0 deletions withny/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type Params struct {
QualityConstraint api.PlaylistConstraint `yaml:"quality,omitempty"`
PacketLossMax int `yaml:"packetLossMax,omitempty"`
OutFormat string `yaml:"outFormat,omitempty"`
WriteChat bool `yaml:"writeChat,omitempty"`
WriteMetaDataJSON bool `yaml:"writeMetaDataJson,omitempty"`
WriteThumbnail bool `yaml:"writeThumbnail,omitempty"`
WaitForLive bool `yaml:"waitForLive,omitempty"`
WaitPollInterval time.Duration `yaml:"waitPollInterval,omitempty"`
Remux bool `yaml:"remux,omitempty"`
Expand All @@ -35,6 +38,9 @@ type OptionalParams struct {
QualityConstraint *api.PlaylistConstraint `yaml:"quality,omitempty"`
PacketLossMax *int `yaml:"packetLossMax,omitempty"`
OutFormat *string `yaml:"outFormat,omitempty"`
WriteChat *bool `yaml:"writeChat,omitempty"`
WriteMetaDataJSON *bool `yaml:"writeMetaDataJson,omitempty"`
WriteThumbnail *bool `yaml:"writeThumbnail,omitempty"`
WaitForLive *bool `yaml:"waitForLive,omitempty"`
WaitPollInterval *time.Duration `yaml:"waitPollInterval,omitempty"`
Remux *bool `yaml:"remux,omitempty"`
Expand All @@ -53,6 +59,9 @@ var DefaultParams = Params{
QualityConstraint: api.PlaylistConstraint{},
PacketLossMax: 20,
OutFormat: "{{ .Date }} {{ .Title }} ({{ .ChannelName }}).{{ .Ext }}",
WriteChat: false,
WriteMetaDataJSON: false,
WriteThumbnail: false,
WaitForLive: true,
WaitPollInterval: 5 * time.Second,
Remux: true,
Expand All @@ -77,6 +86,15 @@ func (override *OptionalParams) Override(params *Params) {
if override.OutFormat != nil {
params.OutFormat = *override.OutFormat
}
if override.WriteChat != nil {
params.WriteChat = *override.WriteChat
}
if override.WriteMetaDataJSON != nil {
params.WriteMetaDataJSON = *override.WriteMetaDataJSON
}
if override.WriteThumbnail != nil {
params.WriteThumbnail = *override.WriteThumbnail
}
if override.WaitForLive != nil {
params.WaitForLive = *override.WaitForLive
}
Expand Down Expand Up @@ -124,6 +142,9 @@ func (p *Params) Clone() *Params {
QualityConstraint: p.QualityConstraint,
PacketLossMax: p.PacketLossMax,
OutFormat: p.OutFormat,
WriteChat: p.WriteChat,
WriteMetaDataJSON: p.WriteMetaDataJSON,
WriteThumbnail: p.WriteThumbnail,
WaitForLive: p.WaitForLive,
WaitPollInterval: p.WaitPollInterval,
Remux: p.Remux,
Expand Down

0 comments on commit f5d11fa

Please sign in to comment.