streamlink to ffmpeg pipe in go

mail@pastecode.io avatar
unknown
golang
2 years ago
1.2 kB
2
Indexable
Never
package main

import (
	"bufio"
	"fmt"
	"os/exec"
	"strings"
)

func main() {
	// Create a pipe for the streamlink command
	streamlinkPipe, err := exec.Command("streamlink", "--hls-live-restart", "url_of_the_stream", "best").StdoutPipe()
	if err != nil {
		fmt.Println(err)
		return
	}

	// Create a pipe for the ffmpeg command
	ffmpegPipe, err := exec.Command("ffmpeg", "-i", "pipe:0", "-acodec", "copy", "-vcodec", "copy", "-f", "flv", "pipe:1").StdinPipe()
	if err != nil {
		fmt.Println(err)
		return
	}

	// Start the streamlink command
	if err := streamlinkPipe.Start(); err != nil {
		fmt.Println(err)
		return
	}

	// Start the ffmpeg command
	if err := ffmpegPipe.Start(); err != nil {
		fmt.Println(err)
		return
	}

	// Read from the streamlink pipe and write to the ffmpeg pipe
	scanner := bufio.NewScanner(streamlinkPipe)
	for scanner.Scan() {
		line := scanner.Text()
		ffmpegPipe.Write([]byte(line + "\n"))
	}

	// Read from the ffmpeg pipe and send the output to the client
	scanner = bufio.NewScanner(ffmpegPipe)
	for scanner.Scan() {
		line := scanner.Text()
		// Send line to the client
	}
}