more shit
This commit is contained in:
parent
6ae627522c
commit
dae22a9716
3 changed files with 120 additions and 124 deletions
105
cmd/mpv.go
105
cmd/mpv.go
|
@ -13,12 +13,14 @@ import (
|
||||||
|
|
||||||
type MPVPLayer struct {
|
type MPVPLayer struct {
|
||||||
entries []*feedEntry
|
entries []*feedEntry
|
||||||
|
finishedEntries []*feedEntry
|
||||||
|
|
||||||
c *mpvipc.Connection
|
c *mpvipc.Connection
|
||||||
f *os.File
|
f *os.File
|
||||||
|
|
||||||
VideoPosition time.Duration
|
videoPosition time.Duration
|
||||||
VideoDuration time.Duration
|
videoDuration time.Duration
|
||||||
|
isPlaying bool
|
||||||
|
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
@ -32,52 +34,103 @@ func (mpv *MPVPLayer) Exit() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mpv *MPVPLayer) Queue(e feedEntry) error {
|
func (mpv *MPVPLayer) Queue(e feedEntry) error {
|
||||||
|
mpv.Lock()
|
||||||
|
defer mpv.Unlock()
|
||||||
|
|
||||||
// TODO(eyJhb): implement checking
|
// TODO(eyJhb): implement checking
|
||||||
mpv.entries = append(mpv.entries, &e)
|
mpv.entries = append(mpv.entries, &e)
|
||||||
|
|
||||||
if len(mpv.entries) == 1 {
|
if len(mpv.entries) == 1 {
|
||||||
return mpv.Play(e.Link)
|
return mpv.play(e.Link)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mpv *MPVPLayer) Play(url string) error {
|
func (mpv *MPVPLayer) FinishedPlaying() []*feedEntry {
|
||||||
|
mpv.Lock()
|
||||||
|
defer mpv.Unlock()
|
||||||
|
|
||||||
|
f := mpv.finishedEntries
|
||||||
|
mpv.finishedEntries = nil
|
||||||
|
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mpv *MPVPLayer) CurrentlyPlaying() *feedEntry {
|
||||||
|
mpv.Lock()
|
||||||
|
defer mpv.Unlock()
|
||||||
|
|
||||||
|
if !mpv.isPlaying {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return mpv.entries[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mpv *MPVPLayer) play(url string) error {
|
||||||
if err := mpv.ensurePlayer(); err != nil {
|
if err := mpv.ensurePlayer(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := mpv.c.Call("loadfile", url)
|
_, err := mpv.c.Call("loadfile", url)
|
||||||
fmt.Println(t)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
mpv.isPlaying = true
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mpv *MPVPLayer) Stop() error {
|
func (mpv *MPVPLayer) Stop() error {
|
||||||
|
mpv.Lock()
|
||||||
|
defer mpv.Unlock()
|
||||||
|
if mpv.c == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
err := mpv.c.Set("pause", true)
|
err := mpv.c.Set("pause", true)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mpv *MPVPLayer) IsPlaying() (bool, error) {
|
func (mpv *MPVPLayer) VideoDuration() time.Duration {
|
||||||
if mpv.c == nil || mpv.c.IsClosed() {
|
mpv.RLock()
|
||||||
return false, nil
|
defer mpv.RUnlock()
|
||||||
|
|
||||||
|
return mpv.videoDuration
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := mpv.c.Get("path")
|
func (mpv *MPVPLayer) VideoPosition() time.Duration {
|
||||||
if err != nil {
|
mpv.RLock()
|
||||||
return false, err
|
defer mpv.RUnlock()
|
||||||
|
|
||||||
|
return mpv.videoPosition
|
||||||
}
|
}
|
||||||
|
|
||||||
return path != "", nil
|
func (mpv *MPVPLayer) IsPlaying() bool {
|
||||||
}
|
|
||||||
|
|
||||||
func (mpv *MPVPLayer) finishedVideo() (bool, error) {
|
|
||||||
mpv.Lock()
|
mpv.Lock()
|
||||||
defer mpv.Unlock()
|
defer mpv.Unlock()
|
||||||
|
|
||||||
|
if mpv.c == nil || mpv.c.IsClosed() {
|
||||||
|
if mpv.isPlaying == true {
|
||||||
|
mpv.finishedVideo()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return mpv.isPlaying
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mpv *MPVPLayer) finishedVideo() (bool, error) {
|
||||||
|
if len(mpv.entries) > 0 {
|
||||||
|
mpv.finishedEntries = append(mpv.finishedEntries, mpv.entries[0])
|
||||||
mpv.entries = mpv.entries[1:]
|
mpv.entries = mpv.entries[1:]
|
||||||
|
}
|
||||||
|
mpv.isPlaying = false
|
||||||
|
|
||||||
if len(mpv.entries) == 1 {
|
if len(mpv.entries) == 1 {
|
||||||
if err := mpv.Play(mpv.entries[0].Link); err != nil {
|
if err := mpv.play(mpv.entries[0].Link); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,7 +167,6 @@ func (mpv *MPVPLayer) ensurePlayer() error {
|
||||||
"mpv",
|
"mpv",
|
||||||
"--keep-open=yes",
|
"--keep-open=yes",
|
||||||
"--idle",
|
"--idle",
|
||||||
// fmt.Sprintf("--input-ipc-server=%d", mpv.f.Fd()),
|
|
||||||
fmt.Sprintf("--input-ipc-server=%s", mpv.f.Name()),
|
fmt.Sprintf("--input-ipc-server=%s", mpv.f.Name()),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -144,23 +196,28 @@ func (mpv *MPVPLayer) ensurePlayer() error {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for event := range events {
|
for event := range events {
|
||||||
fmt.Println("event", event)
|
|
||||||
mpv.Lock()
|
|
||||||
if event.ID == 1 {
|
if event.ID == 1 {
|
||||||
if event.Data != nil {
|
if event.Data != nil {
|
||||||
mpv.VideoDuration = time.Duration(event.Data.(float64)) * time.Second
|
mpv.Lock()
|
||||||
|
mpv.videoDuration = time.Duration(event.Data.(float64)) * time.Second
|
||||||
|
mpv.Unlock()
|
||||||
}
|
}
|
||||||
} else if event.ID == 2 {
|
} else if event.ID == 2 {
|
||||||
if event.Data != nil {
|
if event.Data != nil {
|
||||||
mpv.VideoPosition = time.Duration(event.Data.(float64)) * time.Second
|
mpv.Lock()
|
||||||
|
mpv.videoPosition = time.Duration(event.Data.(float64)) * time.Second
|
||||||
|
mpv.Unlock()
|
||||||
}
|
}
|
||||||
} else if event.ID == 3 {
|
} else if event.ID == 3 {
|
||||||
if event.Data != nil {
|
if event.Data != nil {
|
||||||
|
if event.Data.(bool) == true {
|
||||||
|
mpv.Lock()
|
||||||
mpv.finishedVideo()
|
mpv.finishedVideo()
|
||||||
}
|
|
||||||
}
|
|
||||||
mpv.Unlock()
|
mpv.Unlock()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
119
cmd/player.go
119
cmd/player.go
|
@ -1,13 +1,8 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -23,38 +18,24 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type playerModel struct {
|
type playerModel struct {
|
||||||
entry *feedEntry
|
// entry *feedEntry
|
||||||
isPlayingVideo bool
|
// isPlayingVideo bool
|
||||||
|
|
||||||
// video information
|
// // video information
|
||||||
videoDuration time.Duration
|
// videoDuration time.Duration
|
||||||
videoPosition time.Duration
|
// videoPosition time.Duration
|
||||||
|
|
||||||
// TODO: make this smarter
|
// TODO: make this smarter
|
||||||
// finished entries to send messages about
|
// finished entries to send messages about
|
||||||
finishedEntries []int64
|
finishedEntries []int64
|
||||||
|
|
||||||
|
mpv *MPVPLayer
|
||||||
|
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *playerModel) Init() tea.Cmd {
|
func (m *playerModel) Init() tea.Cmd {
|
||||||
return nil
|
m.mpv = NewMPVPlayer()
|
||||||
}
|
|
||||||
|
|
||||||
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())
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -63,28 +44,33 @@ func (m *playerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
case MsgPlayEntry:
|
case MsgPlayEntry:
|
||||||
f := feedEntry(msg)
|
f := feedEntry(msg)
|
||||||
return m, m.playEntry(f)
|
go m.mpv.Queue(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// send message because it is finished
|
// send message because it is finished
|
||||||
m.Lock()
|
fe := m.mpv.FinishedPlaying()
|
||||||
defer m.Unlock()
|
if len(fe) > 0 {
|
||||||
if len(m.finishedEntries) > 0 {
|
var cmds []tea.Cmd
|
||||||
watchedEntryID := m.finishedEntries[0]
|
for _, e := range fe {
|
||||||
m.finishedEntries = m.finishedEntries[1:]
|
cmds = append(cmds, func() tea.Msg { return MsgWatchedEntry(e.ID) })
|
||||||
return m, func() tea.Msg { return MsgWatchedEntry(watchedEntryID) }
|
}
|
||||||
|
return m, tea.Batch(cmds...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *playerModel) View() string {
|
func (m *playerModel) View() string {
|
||||||
if m.isPlayingVideo && m.entry != nil {
|
// return "NO DONT ASK"
|
||||||
timePos := time.Time{}.Add(m.videoPosition)
|
// return fmt.Sprintf("IsPlaying: %v", m.mpv.IsPlaying())
|
||||||
timeDur := time.Time{}.Add(m.videoDuration)
|
|
||||||
|
if m.mpv.IsPlaying() {
|
||||||
|
timePos := time.Time{}.Add(m.mpv.VideoPosition())
|
||||||
|
timeDur := time.Time{}.Add(m.mpv.VideoDuration())
|
||||||
|
|
||||||
// truncate name if needed
|
// truncate name if needed
|
||||||
name := m.entry.Name
|
// name := m.entry.Name
|
||||||
|
name := m.mpv.CurrentlyPlaying().Name
|
||||||
if len(name) > playerMaxTitleLength {
|
if len(name) > playerMaxTitleLength {
|
||||||
name = name[0:playerMaxTitleLength] + "..."
|
name = name[0:playerMaxTitleLength] + "..."
|
||||||
}
|
}
|
||||||
|
@ -93,65 +79,10 @@ func (m *playerModel) View() string {
|
||||||
name,
|
name,
|
||||||
timePos.Format(time.TimeOnly),
|
timePos.Format(time.TimeOnly),
|
||||||
timeDur.Format(time.TimeOnly),
|
timeDur.Format(time.TimeOnly),
|
||||||
videoPercentageWatched(m.videoPosition, m.videoDuration),
|
videoPercentageWatched(m.mpv.VideoPosition(), m.mpv.VideoDuration()),
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
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()
|
|
||||||
}
|
|
||||||
|
|
14
main.go
14
main.go
|
@ -10,8 +10,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(test())
|
// fmt.Println(test())
|
||||||
return
|
// return
|
||||||
|
|
||||||
// init miniflux
|
// init miniflux
|
||||||
client := miniflux.New("https://miniflux.fricloud.dk/v1/", "F_61V4HkhhJjCF_hI12oDAYhgMY2769KAvOkqPsubqc=")
|
client := miniflux.New("https://miniflux.fricloud.dk/v1/", "F_61V4HkhhJjCF_hI12oDAYhgMY2769KAvOkqPsubqc=")
|
||||||
|
@ -35,14 +35,22 @@ func main() {
|
||||||
func test() error {
|
func test() error {
|
||||||
p := cmd.NewMPVPlayer()
|
p := cmd.NewMPVPlayer()
|
||||||
fmt.Println(p.IsPlaying())
|
fmt.Println(p.IsPlaying())
|
||||||
err := p.Play("https://www.youtube.com/watch?v=gCYcHz2k5x0")
|
|
||||||
|
// err := p.Play("https://www.youtube.com/watch?v=gCYcHz2k5x0")
|
||||||
|
var err error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
|
fmt.Println("First is playing")
|
||||||
fmt.Println(p.IsPlaying())
|
fmt.Println(p.IsPlaying())
|
||||||
|
|
||||||
|
for {
|
||||||
|
fmt.Println("2nd is playing")
|
||||||
|
fmt.Println(p.IsPlaying())
|
||||||
|
}
|
||||||
|
|
||||||
// err = p.Play("https://www.youtube.com/watch?v=3d3ceC_EuC0")
|
// err = p.Play("https://www.youtube.com/watch?v=3d3ceC_EuC0")
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// return err
|
// return err
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue