I need to create a simple application where I have to stream a video and convert the video to hls and save it in aws s3 while the video is streaming (I cannot wait till the entire video is downloaded and also I cannot wait till ffmpeg fnishes the conversion to upload it to S3) the below is the code of what I attempted
import (
"bytes"
"io"
"net/http"
"os"
"os/exec"
"github.com/VinukaThejana/go-utils/logger"
)
func main() {
res, err := http.Get("http://localhost/video.mp4")
if err != nil {
logger.Errorf(err)
}
payload, err := io.ReadAll(res.Body)
if err != nil {
logger.Errorf(err)
}
cmd := exec.Command("ffmpeg", "-i", "pipe:0", "-c:v", "libx264", "-preset", "ultrafast", "-tune", "zerolatency", "-f", "flv", "pipe:1")
buffer := bytes.NewBuffer(make([]byte, 5*1024*1024))
cmd.Stderr = os.Stderr
cmd.Stdout = buffer
stdin, err := cmd.StdinPipe()
if err != nil {
logger.Errorf(err)
}
err = cmd.Start()
if err != nil {
logger.Errorf(err)
}
_, err = stdin.Write(payload)
if err != nil {
logger.Errorf(err)
}
err = stdin.Close()
if err != nil {
logger.Errorf(err)
}
err = cmd.Wait()
if err != nil {
logger.Errorf(err)
}
output, err := os.Create("output.mp4")
if err != nil {
logger.Errorf(err)
}
defer output.Close()
_, err = output.Write(buffer.Bytes())
if err != nil {
logger.Errorf(err)
}
}
(the ffmpeg command given above is not correct for converting the video to hls I just put it there just to get things working)
The thing that I don’t understand it how am I able to get the video stream as it being downloaded and save it in s3 whilst converting it with ffmpeg in real time
VinukaKodituwakku is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.