Skip to content

Instantly share code, notes, and snippets.

@IzumiSy
Created December 25, 2021 14:18
Show Gist options
  • Save IzumiSy/73d175d57853496d668469d6cb55f44d to your computer and use it in GitHub Desktop.
Save IzumiSy/73d175d57853496d668469d6cb55f44d to your computer and use it in GitHub Desktop.
Streaming RTMP to mp4 file
package main
import (
"fmt"
"log"
"os"
uuid "github.com/google/uuid"
"github.com/nareix/joy4/av/avutil"
"github.com/nareix/joy4/format/mp4"
"github.com/nareix/joy4/format/rtmp"
)
func main() {
server := &rtmp.Server{}
server.HandlePublish = func(conn *rtmp.Conn) {
defer func() {
conn.Close()
log.Println("Connection closed")
}()
log.Println("HandlePublish")
id := uuid.New()
log.Printf("ID: %s", id)
file, err := os.Create(fmt.Sprintf("result-%s", id))
if err != nil {
log.Fatal(err)
}
defer file.Close()
mux := mp4.NewMuxer(file)
if err := avutil.CopyFile(mux, conn); err != nil {
log.Fatalf("Error: %s", err.Error())
}
}
log.Println("Server running...")
log.Fatal(server.ListenAndServe())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment