34 lines
748 B
Go
34 lines
748 B
Go
package stream
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"io"
|
|
"net/http"
|
|
"os/exec"
|
|
)
|
|
|
|
func AudioHandler(c *gin.Context) {
|
|
cmd := exec.Command("ffmpeg",
|
|
"-f", "alsa",
|
|
"-i", "hw:0,0",
|
|
"-acodec", "aac",
|
|
"-f", "mp4", "-")
|
|
// ffmpeg -f alsa -i hw:0,0 -acodec aac -f mp4 -
|
|
stdout, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
c.String(http.StatusInternalServerError, "Failed to capture audio: %v", err)
|
|
return
|
|
}
|
|
if err := cmd.Start(); err != nil {
|
|
c.String(http.StatusInternalServerError, "Failed to start ffmpeg: %v", err)
|
|
return
|
|
}
|
|
defer cmd.Wait()
|
|
|
|
c.Header("Content-Type", "audio/mp4")
|
|
if _, err := io.Copy(c.Writer, stdout); err != nil {
|
|
c.String(http.StatusInternalServerError, "Failed to stream audio: %v", err)
|
|
return
|
|
}
|
|
}
|