commit c21d893e35cf8d4d9dbcd2d26159b88ce5016440
parent 6b0812c3f63be957cd5fe011f6f5faa6bdd6244c
Author: Oliver Lowe <o@olowe.co>
Date: Fri, 14 Jun 2024 15:11:21 +1000
mpegts: provide example package usage
Diffstat:
1 file changed, 37 insertions(+), 0 deletions(-)
diff --git a/mpegts/example_test.go b/mpegts/example_test.go
@@ -0,0 +1,37 @@
+package mpegts_test
+
+import (
+ "fmt"
+ "log"
+ "os"
+
+ "github.com/untangledco/streaming/mpegts"
+)
+
+// The most common usage of mpegts involves the use of Scanner which
+// steps through the packets of a transport stream. In this example we
+// decode then re-encode every packet from the standard input to the
+// standard output.
+// If a packet contains a clock reference, we print that out to the standard
+// error for diagnostics.
+// This provides similar functionality to the following ffprobe command:
+//
+// ffprobe -show_packets -
+func Example() {
+ sc := mpegts.NewScanner(os.Stdin)
+ var i int
+ for sc.Scan() {
+ i++
+ packet := sc.Packet()
+ if packet.Adaptation != nil && packet.Adaptation.PCR != nil {
+ ticks := packet.Adaptation.PCR.Ticks()
+ fmt.Fprintf(os.Stderr, "packet %d\t%d\n", i, ticks)
+ }
+ if err := mpegts.Encode(os.Stdout, packet); err != nil {
+ log.Printf("encode packet %d: %v", i, err)
+ }
+ }
+ if sc.Err() != nil {
+ log.Fatalf("scan: %v", sc.Err())
+ }
+}