英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论