如何使用Go读取FFMPEG?

How can you read UDP sent from ffmpeg?

I wrote a script to listen to UDP packets and write them into file, but the file is invalid and almost twice the size.

This is how I send the data:

ffmpeg -i video.mp4 -c:a aac -ar 48000 -ab 196k \
 -ac 2 -strict -2 -c:v libx264 -vb 500k -r 25 -s 320x240 \
 -ss 00.000 -f mp4 -movflags frag_keyframe+empty_moov \ udp://127.0.0.1:1936

This is the code in Go. It should simply write the video into a new video file:

package main

import (
  "net"
  "os"
)

func main(){
  var buf []byte = make([]byte, 512)
  addr, _ := net.ResolveUDPAddr("udp", ":1936")
  conn, _ := net.ListenUDP("udp", addr)
  defer conn.Close()

  os.Create("new_video.mp4")
  f, _ := os.OpenFile("new_video.mp4", syscall.O_WRONLY, 0644)
  defer f.Close()


  for {
    n, _ := conn.Read(buf)
    f.Write(buf[0:n])
    buf = make([]byte, 512)
  }
}

Thanks

Updated

I changed the format to mp4 but the file is still invalid.

The buffer size on the server was too small (512byte)