more shit
This commit is contained in:
parent
6ae627522c
commit
dae22a9716
3 changed files with 120 additions and 124 deletions
119
cmd/player.go
119
cmd/player.go
|
@ -1,13 +1,8 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -23,38 +18,24 @@ var (
|
|||
)
|
||||
|
||||
type playerModel struct {
|
||||
entry *feedEntry
|
||||
isPlayingVideo bool
|
||||
// entry *feedEntry
|
||||
// isPlayingVideo bool
|
||||
|
||||
// video information
|
||||
videoDuration time.Duration
|
||||
videoPosition time.Duration
|
||||
// // video information
|
||||
// videoDuration time.Duration
|
||||
// videoPosition time.Duration
|
||||
|
||||
// TODO: make this smarter
|
||||
// finished entries to send messages about
|
||||
finishedEntries []int64
|
||||
|
||||
mpv *MPVPLayer
|
||||
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func (m *playerModel) Init() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *playerModel) playEntry(f feedEntry) tea.Cmd {
|
||||
if m.isPlayingVideo == true {
|
||||
return nil
|
||||
}
|
||||
|
||||
m.entry = &f
|
||||
|
||||
if m.entry != nil && m.isPlayingVideo == false {
|
||||
go m.playVideo(context.Background(), m.entry.Link)
|
||||
} else {
|
||||
// TODO: we are already playing something, we should not allow this
|
||||
// return MsgTick(time.Now())
|
||||
|
||||
}
|
||||
m.mpv = NewMPVPlayer()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -63,28 +44,33 @@ func (m *playerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
switch msg := msg.(type) {
|
||||
case MsgPlayEntry:
|
||||
f := feedEntry(msg)
|
||||
return m, m.playEntry(f)
|
||||
go m.mpv.Queue(f)
|
||||
}
|
||||
|
||||
// send message because it is finished
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
if len(m.finishedEntries) > 0 {
|
||||
watchedEntryID := m.finishedEntries[0]
|
||||
m.finishedEntries = m.finishedEntries[1:]
|
||||
return m, func() tea.Msg { return MsgWatchedEntry(watchedEntryID) }
|
||||
fe := m.mpv.FinishedPlaying()
|
||||
if len(fe) > 0 {
|
||||
var cmds []tea.Cmd
|
||||
for _, e := range fe {
|
||||
cmds = append(cmds, func() tea.Msg { return MsgWatchedEntry(e.ID) })
|
||||
}
|
||||
return m, tea.Batch(cmds...)
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *playerModel) View() string {
|
||||
if m.isPlayingVideo && m.entry != nil {
|
||||
timePos := time.Time{}.Add(m.videoPosition)
|
||||
timeDur := time.Time{}.Add(m.videoDuration)
|
||||
// return "NO DONT ASK"
|
||||
// return fmt.Sprintf("IsPlaying: %v", m.mpv.IsPlaying())
|
||||
|
||||
if m.mpv.IsPlaying() {
|
||||
timePos := time.Time{}.Add(m.mpv.VideoPosition())
|
||||
timeDur := time.Time{}.Add(m.mpv.VideoDuration())
|
||||
|
||||
// truncate name if needed
|
||||
name := m.entry.Name
|
||||
// name := m.entry.Name
|
||||
name := m.mpv.CurrentlyPlaying().Name
|
||||
if len(name) > playerMaxTitleLength {
|
||||
name = name[0:playerMaxTitleLength] + "..."
|
||||
}
|
||||
|
@ -93,65 +79,10 @@ func (m *playerModel) View() string {
|
|||
name,
|
||||
timePos.Format(time.TimeOnly),
|
||||
timeDur.Format(time.TimeOnly),
|
||||
videoPercentageWatched(m.videoPosition, m.videoDuration),
|
||||
videoPercentageWatched(m.mpv.VideoPosition(), m.mpv.VideoDuration()),
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *playerModel) finishedVideo() {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
m.isPlayingVideo = false
|
||||
|
||||
if videoPercentageWatched(m.videoPosition, m.videoDuration) > 90.0 {
|
||||
m.finishedEntries = append(m.finishedEntries, m.entry.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *playerModel) playVideo(ctx context.Context, url string) (bool, error) {
|
||||
m.Lock()
|
||||
m.isPlayingVideo = true
|
||||
m.Unlock()
|
||||
|
||||
defer m.finishedVideo()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "mpv", url)
|
||||
output, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
s := bufio.NewScanner(output)
|
||||
go func(s *bufio.Scanner) {
|
||||
var finishedVideo bool
|
||||
for s.Scan() {
|
||||
t := s.Text()
|
||||
if strings.Contains(t, "End of file") {
|
||||
finishedVideo = true
|
||||
} else if strings.HasPrefix(t, "AV: ") {
|
||||
matches := regexpPosDur.FindStringSubmatch(t)
|
||||
posh, _ := strconv.Atoi(matches[1])
|
||||
posm, _ := strconv.Atoi(matches[2])
|
||||
poss, _ := strconv.Atoi(matches[3])
|
||||
durh, _ := strconv.Atoi(matches[4])
|
||||
durm, _ := strconv.Atoi(matches[5])
|
||||
durs, _ := strconv.Atoi(matches[6])
|
||||
|
||||
m.videoPosition = time.Duration((posh*60*60 + posm*60 + poss) * int(time.Second))
|
||||
m.videoDuration = time.Duration((durh*60*60 + durm*60 + durs) * int(time.Second))
|
||||
}
|
||||
}
|
||||
_ = finishedVideo
|
||||
}(s)
|
||||
|
||||
return false, cmd.Wait()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue