streaming

Media streaming and broadcast systems in Go
Log | Files | Refs | README | LICENSE

example_test.go (981B)


      1 package mpegts_test
      2 
      3 import (
      4 	"fmt"
      5 	"log"
      6 	"os"
      7 
      8 	"github.com/untangledco/streaming/mpegts"
      9 )
     10 
     11 // The most common usage of mpegts involves the use of Scanner which
     12 // steps through the packets of a transport stream. In this example we
     13 // decode then re-encode every packet from the standard input to the
     14 // standard output.
     15 // If a packet contains a clock reference, we print that out to the standard
     16 // error for diagnostics.
     17 // This provides similar functionality to the following ffprobe command:
     18 //
     19 //	ffprobe -show_packets -
     20 func Example() {
     21 	sc := mpegts.NewScanner(os.Stdin)
     22 	var i int
     23 	for sc.Scan() {
     24 		i++
     25 		packet := sc.Packet()
     26 		if packet.Adaptation != nil && packet.Adaptation.PCR != nil {
     27 			ticks := packet.Adaptation.PCR.Ticks()
     28 			fmt.Fprintf(os.Stderr, "packet %d\t%d\n", i, ticks)
     29 		}
     30 		if err := mpegts.Encode(os.Stdout, packet); err != nil {
     31 			log.Printf("encode packet %d: %v", i, err)
     32 		}
     33 	}
     34 	if sc.Err() != nil {
     35 		log.Fatalf("scan: %v", sc.Err())
     36 	}
     37 }