✨ Watch video
This commit is contained in:
parent
1878224040
commit
2a19f3c8e9
4 changed files with 55 additions and 2 deletions
45
media/watch.go
Normal file
45
media/watch.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package media
|
||||
|
||||
import (
|
||||
"github.com/creack/pty"
|
||||
"golang.org/x/term"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func Watch(path string) {
|
||||
cmd := exec.Command("mpv", "--osd-fractions", "--term-osd=force", path)
|
||||
ptmx, err := pty.Start(cmd)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Make sure to close the pty at the end.
|
||||
defer func() { ptmx.Close() }() // Best effort.
|
||||
|
||||
// Handle pty size.
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, syscall.SIGWINCH)
|
||||
go func() {
|
||||
for range ch {
|
||||
pty.InheritSize(os.Stdin, ptmx)
|
||||
}
|
||||
}()
|
||||
ch <- syscall.SIGWINCH // Initial resize.
|
||||
defer func() { signal.Stop(ch); close(ch) }() // Cleanup signals when done.
|
||||
|
||||
// Set stdin in raw mode.
|
||||
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() { term.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.
|
||||
|
||||
// Copy stdin to the pty and the pty to stdout.
|
||||
// NOTE: The goroutine will keep reading until the next keystroke before returning.
|
||||
go func() { io.Copy(ptmx, os.Stdin) }()
|
||||
io.Copy(os.Stdout, ptmx)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue