ffmpeg: use one command to stream video/audio

This commit is contained in:
Artem
2024-11-05 18:49:21 +01:00
parent f10e2da534
commit 1f5a56cc5d
8 changed files with 100 additions and 258 deletions

View File

@@ -3,7 +3,6 @@ package config
import (
"fmt"
"os"
"strings"
"gopkg.in/yaml.v3"
)
@@ -43,17 +42,24 @@ const (
StreamSourceHevc = "hevc"
)
type StreamInput string
const (
StreamInputVideo = "v"
StreamInputVideoAudio = "va"
)
type FFmpeg struct {
ExtProcess
FPS int `yaml:"fps"`
Bitrate int `yaml:"bitrate"`
Height int `yaml:"height"`
GOP int `yaml:"gop"`
Codec string `yaml:"codec"`
Commands map[StreamInput]string `yaml:"commands"`
FPS int `yaml:"fps"`
Bitrate int `yaml:"bitrate"`
Height int `yaml:"height"`
GOP int `yaml:"gop"`
Codec string `yaml:"codec"`
}
func (f FFmpeg) FormatArgs() []string {
return strings.Split(fmt.Sprintf(f.Args, f.Height, f.Codec, f.Bitrate*1000, f.FPS, f.GOP), " ")
func (f FFmpeg) FormatCmd(cmd string) string {
return fmt.Sprintf(cmd, f.Height, f.Codec, f.Bitrate*1000, f.FPS, f.GOP)
}
type UStreamer struct {
@@ -68,8 +74,6 @@ func Get() Config {
return c
}
// arecord -D hw:0,0 -f cd -r 44100 -c 2 | /app/ffmpeg -re -init_hw_device rkmpp=hw -filter_hw_device hw -f wav -i pipe:0 -f v4l2 -c:a aac -b:a 128k -ar 44100 -ac 2 -f rtp rtp://127.0.0.1:5006?pkt_size=1200 -i /dev/video0 -vf hwupload,scale_rkrga=h=720:force_original_aspect_ratio=1 -c:v h264_rkmpp -flags +low_delay -b:v 6000000 -framerate 60 -g 10 -f rtp rtp://127.0.0.1:5004?pkt_size=1200
func Init() {
c = Config{
LogLevel: "debug",
@@ -84,10 +88,15 @@ func Init() {
},
},
Video: FFmpeg{
ExtProcess: ExtProcess{
Path: "/app/ffmpeg",
Args: "-hide_banner -loglevel error -re -init_hw_device rkmpp=hw -filter_hw_device hw -i /dev/video0 -vf hwupload,scale_rkrga=h=%d:force_original_aspect_ratio=1 -c:v %s_rkmpp -flags +low_delay -b:v %d -framerate %d -g %d -f rtp rtp://127.0.0.1:5004?pkt_size=1200",
//Args: "-re -i /dev/video0 -c:v h264_rkmpp -b:v %d -framerate %d -bsf:v h264_mp4toannexb -g 10 -f rtp rtp://127.0.0.1:5004?pkt_size=1200",
Commands: map[StreamInput]string{
StreamInputVideoAudio: "/usr/bin/arecord -D hw:0,0 -f dat -r 48000 -c 2 --buffer-size=150 | /app/ffmpeg -init_hw_device rkmpp=hw -filter_hw_device hw" +
" -f wav -i pipe:0 -map 0:a -c:a libopus -b:a 48000 -sample_fmt s16 -ssrc 1 -payload_type 111 -f rtp -max_delay 0 -application lowdelay" +
" -f rtp rtp://127.0.0.1:5006?pkt_size=1200" +
" -i /dev/video0 -map 1:v -vf hwupload,scale_rkrga=h=%d:force_original_aspect_ratio=1 -c:v %s_rkmpp -flags +low_delay -b:v %d -framerate %d -g %d" +
" -f rtp rtp://127.0.0.1:5004?pkt_size=1200",
StreamInputVideo: "/app/ffmpeg -hide_banner -loglevel error -init_hw_device rkmpp=hw -filter_hw_device hw" +
" -i /dev/video0 -vf hwupload,scale_rkrga=h=%d:force_original_aspect_ratio=1 -c:v %s_rkmpp -flags +low_delay -b:v %d -framerate %d -g %d" +
" -f rtp rtp://127.0.0.1:5004?pkt_size=1200",
},
FPS: 60,
Bitrate: 6000,
@@ -95,10 +104,6 @@ func Init() {
GOP: 5,
Codec: "h264",
},
Audio: []string{
"/usr/bin/arecord -D hw:0,0 -f dat -r 48000 -c 2 --buffer-size=60",
"/app/ffmpeg -hide_banner -loglevel error -re -f wav -i pipe:0 -c:a libopus -b:a 48000 -sample_fmt s16 -ssrc 1 -payload_type 111 -f rtp -max_delay 0 -application lowdelay -f rtp rtp://127.0.0.1:5006?pkt_size=1200",
},
WebRtc: WebRtc{
Host: "127.0.0.1",
Port: 5004,