How to read FFMPEG with Go?

huangapple go评论73阅读模式
英文:

How to read FFMPEG with Go?

问题

你好!以下是你要翻译的内容:

如何读取来自ffmpeg发送的UDP数据包?

我编写了一个脚本来监听UDP数据包并将其写入文件,但是生成的文件无效且大小几乎是原来的两倍。

这是我发送数据的方式:

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

这是Go语言的代码。它应该简单地将视频写入一个新的视频文件:

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)
  }
}

谢谢!

更新

我将格式更改为mp4,但是生成的文件仍然无效。

英文:

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.

答案1

得分: 1

服务器上的缓冲区大小太小(512字节)。

英文:

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

huangapple
  • 本文由 发表于 2016年12月2日 07:31:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/40922313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定