1. package main

  2. import (
  3.         "fmt"
  4.         "io"
  5.         "io/ioutil"
  6.         "log"
  7.         "os/exec"
  8. )


  9. func tsProducer(writer *io.PipeWriter) {
  10.         //将本地的ts文件 写入 writer
  11.         for i:=0; i<21;i++ {
  12.                 tsName := fmt.Sprintf("sofia.pts.mp4-%05d.ts", i)
  13.                 content, err := ioutil.ReadFile(tsName)
  14.                 log.Print("ts produce loop:", tsName)

  15.                 if err != nil {
  16.                         log.Fatal(err)
  17.                 }
  18.                 
  19.                 n , err:= writer.Write(content)
  20.                 if err != nil {
  21.                         log.Print("write to pipe failed:", err.Error())
  22.                 }

  23.                 log.Printf("write to pipe content len:%d, success\n", n)
  24.         }
  25. }


  26. func main() {
  27.         rp, wp := io.Pipe()
  28.         go tsProducer(wp)

  29.         for {
  30.                 log.Print("main loop")
  31.                 cmd := exec.Command("ffmpeg", "-re", "-i", "pipe:0", "-vcodec", "copy", "-acodec", "copy", "-f", "flv", "rtmp://127.0.0.1/live/pipe")
  32.                 cmd.Stdin = rp

  33.         

  34.                 log.Print("cmd run")
  35.                 err := cmd.Run()
  36.                 log.Printf("Command finished with error: %v", err)
  37.         }
  38. }


12-26 12:44